Class: MP4::Source::Url

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/mp4/source/url.rb

Constant Summary

Constants included from Base

Base::REQUIRED_METHODS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Base

included

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

#urlObject (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

Returns:

  • (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

#posObject



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_seekObject



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

#uriObject



25
26
27
# File 'lib/mp4/source/url.rb', line 25

def uri
  @url
end