Class: Eventflit::Request
- Inherits:
-
Object
- Object
- Eventflit::Request
- Defined in:
- lib/eventflit/request.rb
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
Instance Method Summary collapse
-
#initialize(client, verb, uri, params, body = nil) ⇒ Request
constructor
A new instance of Request.
- #send_async ⇒ Object
- #send_sync ⇒ Object
Constructor Details
#initialize(client, verb, uri, params, body = nil) ⇒ Request
Returns a new instance of Request.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/eventflit/request.rb', line 9 def initialize(client, verb, uri, params, body = nil) @client, @verb, @uri = client, verb, uri @head = { 'X-Eventflit-Library' => 'eventflit-http-ruby ' + Eventflit::VERSION } @body = body if body params[:body_md5] = Digest::MD5.hexdigest(body) @head['Content-Type'] = 'application/json' end request = Pusher::Signature::Request.new(verb.to_s.upcase, uri.path, params) request.sign(client.authentication_token) @params = request.signed_params end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
7 8 9 |
# File 'lib/eventflit/request.rb', line 7 def body @body end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
7 8 9 |
# File 'lib/eventflit/request.rb', line 7 def params @params end |
Instance Method Details
#send_async ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/eventflit/request.rb', line 43 def send_async if defined?(EventMachine) && EventMachine.reactor_running? http_client = @client.em_http_client(@uri) df = EM::DefaultDeferrable.new http = case @verb when :post http_client.post({ :query => @params, :body => @body, :head => @head }) when :get http_client.get({ :query => @params, :head => @head }) else raise "Unsupported verb" end http.callback { begin df.succeed(handle_response(http.response_header.status, http.response.chomp)) rescue => e df.fail(e) end } http.errback { |e| = "Network error connecting to eventflit (#{http.error})" Eventflit.logger.debug() df.fail(Error.new()) } return df else http = @client.sync_http_client return http.request_async(@verb, @uri, @params, @body, @head) end end |
#send_sync ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/eventflit/request.rb', line 26 def send_sync http = @client.sync_http_client begin response = http.request(@verb, @uri, @params, @body, @head) rescue HTTPClient::BadResponseError, HTTPClient::TimeoutError, SocketError, Errno::ECONNREFUSED => e error = Eventflit::HTTPError.new("#{e.} (#{e.class})") error.original_error = e raise error end body = response.body ? response.body.chomp : nil return handle_response(response.code.to_i, body) end |