Class: ConnectClient::Http::EmHttp
- Inherits:
-
Object
- Object
- ConnectClient::Http::EmHttp
- Defined in:
- lib/connect_client/http/event_endpoint.rb
Instance Method Summary collapse
- #create_response(http_reponse, events) ⇒ Object
-
#initialize(base_url, headers) ⇒ EmHttp
constructor
A new instance of EmHttp.
- #push_events(path, body, events) ⇒ Object
- #push_events_using_deferred(path, body, events) ⇒ Object
- #push_events_using_synchrony(path, body, events) ⇒ Object
Constructor Details
#initialize(base_url, headers) ⇒ EmHttp
Returns a new instance of EmHttp.
72 73 74 75 76 77 78 |
# File 'lib/connect_client/http/event_endpoint.rb', line 72 def initialize(base_url, headers) require 'em-http-request' require_relative 'deferred_http_response' @headers = headers @base_url = base_url.chomp('/') end |
Instance Method Details
#create_response(http_reponse, events) ⇒ Object
119 120 121 122 123 124 125 126 127 |
# File 'lib/connect_client/http/event_endpoint.rb', line 119 def create_response(http_reponse, events) status = http_reponse.response_header.status content_type = http_reponse.response_header['Content-Type'] if (http_reponse.error.to_s.empty?) ConnectClient::EventPushResponse.new status, content_type, http_reponse.response, events else ConnectClient::EventPushResponse.new status, content_type, http_reponse.error, events end end |
#push_events(path, body, events) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/connect_client/http/event_endpoint.rb', line 80 def push_events(path, body, events) raise AsyncHttpError unless defined?(EventMachine) && EventMachine.reactor_running? use_syncrony = defined?(EM::Synchrony) if use_syncrony push_events_using_synchrony(path, body, events) else push_events_using_deferred(path, body, events) end end |
#push_events_using_deferred(path, body, events) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/connect_client/http/event_endpoint.rb', line 92 def push_events_using_deferred(path, body, events) deferred = DeferredHttpResponse.new url_string = "#{@base_url}#{path}".chomp('/') http = EventMachine::HttpRequest.new(url_string).post(:body => body, :head => @headers) http_callback = Proc.new do begin response = create_response http, events deferred.succeed response rescue => error deferred.fail error end end http.callback &http_callback http.errback &http_callback deferred end |
#push_events_using_synchrony(path, body, events) ⇒ Object
111 112 113 114 115 116 117 |
# File 'lib/connect_client/http/event_endpoint.rb', line 111 def push_events_using_synchrony(path, body, events) url_string = "#{@base_url}#{path}".chomp('/') http = EventMachine::HttpRequest.new(url_string). post(:body => body, :head => @headers) create_response http, events end |