Class: Sockudo::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/sockudo/request.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#bodyObject (readonly)

Returns the value of attribute body.



10
11
12
# File 'lib/sockudo/request.rb', line 10

def body
  @body
end

#paramsObject (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_asyncObject



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|
      message = "Network error connecting to sockudo (#{http.error})"
      Sockudo.logger.debug(message)
      df.fail(Error.new(message))
    end

    df
  else
    http = @client.sync_http_client

    http.request_async(@verb, @uri, @params, @body, @head)
  end
end

#send_syncObject



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.message} (#{e.class})")
    error.original_error = e
    raise error
  end

  body = response.body&.chomp

  handle_response(response.code.to_i, body)
end