Class: RealPush::Request

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, verb, uri, params = {}, body = nil) ⇒ Request

Returns a new instance of Request.

Raises:



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/realpush/request.rb', line 5

def initialize(client, verb, uri, params={}, body = nil)
  @client, @verb, @uri, @params = client, verb, uri, params

  raise ConfigurationError, "Invalid verb ('#{verb}')" unless %w{GET POST}.include? verb.to_s.upcase
  raise ConfigurationError, "Invalid client #{client.inspect}" unless client.is_a? Client
  raise ConfigurationError, "Invalid uri #{uri.inspect}" unless uri.is_a? URI

  @head = {}

  @body = body
  if body
    @params[:body_md5] = Digest::MD5.hexdigest(body)
    @head['Content-Type'] = 'application/json'
  end

  sign_params
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



3
4
5
# File 'lib/realpush/request.rb', line 3

def body
  @body
end

#paramsObject (readonly)

Returns the value of attribute params.



3
4
5
# File 'lib/realpush/request.rb', line 3

def params
  @params
end

Instance Method Details

#send_asyncObject



56
57
58
59
# File 'lib/realpush/request.rb', line 56

def send_async
  http = @client.sync_http_client
  http.request_async(@verb, @uri, @params, @body, @head)
end

#send_syncObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/realpush/request.rb', line 39

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
    raise RealPush::HTTPError, "#{e.message} (#{e.class})"
  end

  body = response.body ? response.body.chomp : nil

  return handle_response(response.code.to_i, body)
end