Class: Pusher::Request
- Inherits:
-
Object
- Object
- Pusher::Request
- Defined in:
- lib/pusher/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/pusher/request.rb', line 9 def initialize(client, verb, uri, params, body = nil) @client, @verb, @uri = client, verb, uri @head = { 'X-Pusher-Library' => 'pusher-http-ruby ' + Pusher::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/pusher/request.rb', line 7 def body @body end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
7 8 9 |
# File 'lib/pusher/request.rb', line 7 def params @params end |
Instance Method Details
#send_async ⇒ Object
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 80 81 82 |
# File 'lib/pusher/request.rb', line 46 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 pusher (#{http.error})" Pusher.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 42 43 44 |
# File 'lib/pusher/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 = Pusher::HTTPError.new("#{e.} (#{e.class})") error.original_error = e raise error end body = response.body ? response.body.chomp : nil code = response.code.to_i http.reset_all unless [200, 202, 400, 401, 403, 404, 407, 413].include?(code) return handle_response(code, body) end |