Class: Sockudo::Request
- Inherits:
-
Object
- Object
- Sockudo::Request
- Defined in:
- lib/sockudo/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, extra_headers = {}) ⇒ Request
constructor
A new instance of Request.
- #send_async ⇒ Object
- #send_sync ⇒ Object
Constructor Details
#initialize(client, verb, uri, params, body = nil, extra_headers = {}) ⇒ Request
Returns a new instance of Request.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/sockudo/request.rb', line 12 def initialize(client, verb, uri, params, body = nil, extra_headers = {}) @client = client @verb = verb @uri = uri @head = { 'X-Pusher-Library' => "sockudo-http-ruby #{Sockudo::VERSION}" } @head.merge!(extra_headers) if extra_headers && !extra_headers.empty? @body = body params = params.each_with_object({}) { |(key, value), result| result[key.to_s] = value } if body params['body_md5'] = Digest::MD5.hexdigest(body) @head['Content-Type'] = 'application/json' end @params = signed_params(params) end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
10 11 12 |
# File 'lib/sockudo/request.rb', line 10 def body @body end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
10 11 12 |
# File 'lib/sockudo/request.rb', line 10 def params @params end |
Instance Method Details
#send_async ⇒ Object
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 83 84 85 86 |
# File 'lib/sockudo/request.rb', line 48 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 }) when :delete http_client.delete({ query: @params, head: @head }) else raise 'Unsupported verb' end http.callback do df.succeed(handle_response(http.response_header.status, http.response.chomp)) rescue StandardError => e df.fail(e) end http.errback do |_e| = "Network error connecting to sockudo (#{http.error})" Sockudo.logger.debug() df.fail(Error.new()) end df else http = @client.sync_http_client http.request_async(@verb, @uri, @params, @body, @head) end end |
#send_sync ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/sockudo/request.rb', line 31 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 = Sockudo::HTTPError.new("#{e.} (#{e.class})") error.original_error = e raise error end body = response.body&.chomp handle_response(response.code.to_i, body) end |