Class: Telecaster::Multi

Inherits:
Object
  • Object
show all
Includes:
EM::Deferrable
Defined in:
lib/telecaster/multi.rb

Instance Method Summary collapse

Constructor Details

#initializeMulti

Returns a new instance of Multi.



8
9
10
11
# File 'lib/telecaster/multi.rb', line 8

def initialize
  @requests  = {}
  @responses = {}
end

Instance Method Details

#add(host, request) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/telecaster/multi.rb', line 13

def add(host, request)
  start_time = Time.now.to_f
  @requests[host] = request
  
  request.callback do |http|
    response = {
      'host'      => host,
      'status'    => http.response_header.status,
      'duration'  => ((Time.now.to_f - start_time) * 1000).round
    }
    content_type = (http.response_header['CONTENT_TYPE'] || '').split(/\s*;\s*/).first
    
    if content_type == TYPE_JSON
      response['data'] = Yajl::Parser.parse(http.response)
    end
    add_response(host, response)
  end
  
  request.errback do |http|
    add_response(host, {'host' => host, 'status' => nil})
  end
end