Class: YouTubeIt::Upload::RemoteFile
- Inherits:
-
Object
- Object
- YouTubeIt::Upload::RemoteFile
- Defined in:
- lib/youtube_it/request/remote_file.rb
Instance Method Summary collapse
- #filename ⇒ Object
- #head ⇒ Object
-
#initialize(url) ⇒ RemoteFile
constructor
A new instance of RemoteFile.
- #length ⇒ Object
- #path ⇒ Object
- #ping? ⇒ Boolean
- #pos ⇒ Object
- #read(buf_size = 524288) ⇒ Object
Constructor Details
#initialize(url) ⇒ 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 |
# File 'lib/youtube_it/request/remote_file.rb', line 9 def initialize( url) @pos = 0 @url = url @uri = URI(@url) @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
#filename ⇒ Object
44 45 46 |
# File 'lib/youtube_it/request/remote_file.rb', line 44 def filename File.basename(@url) end |
#head ⇒ Object
37 38 39 40 41 42 |
# File 'lib/youtube_it/request/remote_file.rb', line 37 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 |
#length ⇒ Object
52 53 54 55 |
# File 'lib/youtube_it/request/remote_file.rb', line 52 def length @content_length ||= head.content_length return @content_length end |
#path ⇒ Object
48 49 50 |
# File 'lib/youtube_it/request/remote_file.rb', line 48 def path @url end |
#ping? ⇒ Boolean
29 30 31 |
# File 'lib/youtube_it/request/remote_file.rb', line 29 def ping? end |
#pos ⇒ Object
33 34 35 |
# File 'lib/youtube_it/request/remote_file.rb', line 33 def pos @pos end |
#read(buf_size = 524288) ⇒ Object
57 58 59 60 61 62 63 64 |
# File 'lib/youtube_it/request/remote_file.rb', line 57 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 |