Class: MP4::Source::Url
Constant Summary
Constants included from Base
Instance Attribute Summary collapse
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Class Method Summary collapse
Instance Method Summary collapse
- #eof? ⇒ Boolean
- #fetch(from, to) ⇒ Object
- #fetch_ranges(ranges) ⇒ Object
-
#initialize(url) ⇒ Url
constructor
A new instance of Url.
- #open! ⇒ Object
- #pos ⇒ Object
- #read(n) ⇒ Object
- #reset_seek ⇒ Object
- #set_seek(n) ⇒ Object
- #uri ⇒ Object
Methods included from Base
Constructor Details
#initialize(url) ⇒ Url
Returns a new instance of Url.
18 19 20 21 22 23 |
# File 'lib/mp4/source/url.rb', line 18 def initialize(url) @url = url @position = 0 @eof = false @file_size = fetch_file_size end |
Instance Attribute Details
#url ⇒ Object (readonly)
Returns the value of attribute url.
8 9 10 |
# File 'lib/mp4/source/url.rb', line 8 def url @url end |
Class Method Details
.from_uri(uri) ⇒ Object
10 11 12 13 14 15 16 |
# File 'lib/mp4/source/url.rb', line 10 def self.from_uri(uri) unless uri.start_with?('http://') || uri.start_with?('https://') raise ArgumentError, "expected http(s):// URI, got #{uri.inspect}" end new(uri) end |
Instance Method Details
#eof? ⇒ Boolean
70 71 72 |
# File 'lib/mp4/source/url.rb', line 70 def eof? @position >= @file_size end |
#fetch(from, to) ⇒ Object
45 46 47 48 49 50 51 52 53 54 |
# File 'lib/mp4/source/url.rb', line 45 def fetch(from, to) range = "bytes=#{from}-#{to}" response = http_get(range) if response.is_a?(Net::HTTPSuccess) response.body else raise "Unable to fetch range" end end |
#fetch_ranges(ranges) ⇒ Object
78 79 80 81 82 83 84 85 86 87 |
# File 'lib/mp4/source/url.rb', line 78 def fetch_ranges(ranges) range_header = "bytes=#{ranges.join(',')}" response = http_get(range_header) if response.is_a?(Net::HTTPSuccess) parse_multipart_response(response.body, response['Content-Type']) else raise "Unable to fetch ranges" end end |
#open! ⇒ Object
66 67 68 |
# File 'lib/mp4/source/url.rb', line 66 def open! self end |
#pos ⇒ Object
74 75 76 |
# File 'lib/mp4/source/url.rb', line 74 def pos @position end |
#read(n) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/mp4/source/url.rb', line 29 def read(n) return '' if eof? range_header = "bytes=#{@position}-#{@position + n - 1}" response = http_get(range_header) if response.is_a?(Net::HTTPSuccess) data = response.body @position += data.size data else @eof = true '' end end |
#reset_seek ⇒ Object
61 62 63 64 |
# File 'lib/mp4/source/url.rb', line 61 def reset_seek @position = 0 @eof = false end |
#set_seek(n) ⇒ Object
56 57 58 59 |
# File 'lib/mp4/source/url.rb', line 56 def set_seek(n) @position = n @eof = false if n < @file_size end |
#uri ⇒ Object
25 26 27 |
# File 'lib/mp4/source/url.rb', line 25 def uri @url end |