class EventMachine::Zipper::Chunks

Attributes

file_path[R]
q[R]

Public Class Methods

new(file_path) click to toggle source
# File lib/em-zipper/chunks.rb, line 8
def initialize(file_path)
  @file_path = file_path
  @q         = EM::Queue.new
end

Public Instance Methods

chop!() click to toggle source
# File lib/em-zipper/chunks.rb, line 13
def chop!
  _chunk_method = file_path.match(/\Ahttps?:\/\//) ? :chunk_http : :chunk_file
  __send__ _chunk_method

  self
end

Private Instance Methods

chunk_file() click to toggle source
# File lib/em-zipper/chunks.rb, line 23
def chunk_file
  file = File.open file_path

  fr = proc do
    begin
      q.push file.read_nonblock(16384)
      EM.next_tick(&fr)
    rescue EOFError
      file.close
      succeed(q)
    end
  end

  EM.next_tick(&fr)
end
chunk_http() click to toggle source
# File lib/em-zipper/chunks.rb, line 39
def chunk_http
  http = EventMachine::HttpRequest.new(file_path).get
  http.errback  { fail }
  http.callback { succeed(q) }
  http.stream   { |chunk| q.push chunk }
end