Class: Keen::HTTP::Async

Inherits:
Object
  • Object
show all
Defined in:
lib/keen/http.rb

Instance Method Summary collapse

Constructor Details

#initialize(base_url, options = {}) ⇒ Async

Returns a new instance of Async.



56
57
58
59
60
61
62
63
64
65
# File 'lib/keen/http.rb', line 56

def initialize(base_url, options={})
  if defined?(EventMachine) && EventMachine.reactor_running?
    require 'em-http-request'
  else
    raise Error, "An EventMachine loop must be running to use publish_async calls"
  end

  @base_url = base_url
  @proxy_url, @proxy_type = options.values_at(:proxy_url, :proxy_type)
end

Instance Method Details

#post(options) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/keen/http.rb', line 67

def post(options)
  path, headers, body = options.values_at(
    :path, :headers, :body)
  uri = "#{@base_url}#{path}"
  if @proxy_url
    proxy_uri = URI.parse(@proxy_url)
    connection_options = {:proxy =>
                              {:host => proxy_uri.host,
                               :port => proxy_uri.port,
                               :authorization => [proxy_uri.user, proxy_uri.password],
                               :type => @proxy_type || "http"}}
    http_client = EventMachine::HttpRequest.new(uri, connection_options)
  else
    http_client = EventMachine::HttpRequest.new(uri)
  end
  http_client.post(
    :body => body,
    :head => headers
  )

end