Class: YouTubeIt::Upload::RemoteFile

Inherits:
Object
  • Object
show all
Defined in:
lib/youtube_it/request/remote_file.rb

Instance Method Summary collapse

Constructor Details

#initialize(url, opts) ⇒ RemoteFile

Returns a new instance of RemoteFile.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/youtube_it/request/remote_file.rb', line 9

def initialize(url, opts)
  @pos = 0
  @url = url
  @uri = URI(@url)
  
  @content_length = opts[:content_length]

  @fiber = Fiber.new do |first|

    Net::HTTP.start(@uri.host, @uri.port) do |http|
      request = Net::HTTP::Get.new @uri.request_uri
      http.request request do |response|
        response.read_body do |chunk|
          @pos += chunk.bytesize
          Fiber.yield chunk
        end
      end
    end
  end

end

Instance Method Details

#filenameObject



46
47
48
# File 'lib/youtube_it/request/remote_file.rb', line 46

def filename
  File.basename(@url)
end

#headObject



39
40
41
42
43
44
# File 'lib/youtube_it/request/remote_file.rb', line 39

def head
  @head_result || Net::HTTP.start(@uri.host, @uri.port) do |http|
    @head_result = http.request(Net::HTTP::Head.new(@uri.request_uri))
  end
  @head_result
end

#lengthObject



54
55
56
57
# File 'lib/youtube_it/request/remote_file.rb', line 54

def length
  @content_length ||= head.content_length
  return @content_length
end

#pathObject



50
51
52
# File 'lib/youtube_it/request/remote_file.rb', line 50

def path
  @url
end

#ping?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/youtube_it/request/remote_file.rb', line 31

def ping?

end

#posObject



35
36
37
# File 'lib/youtube_it/request/remote_file.rb', line 35

def pos
  @pos
end

#read(buf_size = 524288) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/youtube_it/request/remote_file.rb', line 59

def read(buf_size = 524288)
  buf = ""
  while (buf.bytesize < buf_size.to_i) && @fiber.alive?
    _chunk = @fiber.resume
    buf << _chunk if _chunk.is_a? String
  end
  buf
end