Module: Gnarly::Client::EMHttp

Defined in:
lib/gnarly/client/emhttp.rb

Instance Method Summary collapse

Instance Method Details

#http(request, options = {}, &block) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/gnarly/client/emhttp.rb', line 11

def http(request, options={}, &block)
  if block
    method, url, headers, body = request
    opts = {}
    opts[:head] = headers if headers
    case url
    when Array
      url, query = url
      opts[:query] = query
    end
    http = EventMachine::HttpRequest.new(url).__send__(method, opts)
    http.stream &options[:stream] if options[:stream]
    http.callback {
      headers = Client.symbolize_headers http.response_header.to_hash
      block.call [http.response_header.status, headers, http.response]
    }
  else
    raise :async_only
  end
end