Class: PhoenixRails::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/phoenix_rails/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.



9
10
11
12
13
14
15
# File 'lib/phoenix_rails/request.rb', line 9

def initialize(client, verb, uri, params, body = nil)
  @client, @verb, @uri = client, verb, uri
  @head = { 'Content-Type' => 'application/json'}
  @body = body

  @head[:Authorization] = "Bearer #{@client.generate_authentication_token}"
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



7
8
9
# File 'lib/phoenix_rails/request.rb', line 7

def body
  @body
end

#paramsObject (readonly)

Returns the value of attribute params.



7
8
9
# File 'lib/phoenix_rails/request.rb', line 7

def params
  @params
end

Instance Method Details

#sendObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/phoenix_rails/request.rb', line 17

def send
  http = @client.http_client

  begin
    response = http.request(@verb, @uri, @params, @body, @head)
  rescue HTTPClient::BadResponseError, HTTPClient::TimeoutError,
    SocketError, Errno::ECONNREFUSED => e
    error = PhoenixRails::HTTPError.new("#{e.message} (#{e.class})")
    error.original_error = e
    raise error
  end

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

  handle_response(response.code.to_i, body)
end