Class: ServiceClient::Response

Inherits:
Object
  • Object
show all
Includes:
Errors
Defined in:
lib/service_client/response.rb

Overview

The Response class encapsulates the HTTP response received by the ServiceClient gem.

Constant Summary

Constants included from Errors

Errors::ERRORS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Errors

#raise_error

Constructor Details

#initialize(response) ⇒ Response

Initializes a new instance of Response with the given HTTP response object.



19
20
21
22
23
# File 'lib/service_client/response.rb', line 19

def initialize(response)
  @code = response.code
  @data = response.parsed_response
  @headers = response.headers
end

Instance Attribute Details

#codeInteger (readonly)

The HTTP status code of the response.



11
12
13
# File 'lib/service_client/response.rb', line 11

def code
  @code
end

#dataHash, ... (readonly)

The parsed body of the response.



11
12
13
# File 'lib/service_client/response.rb', line 11

def data
  @data
end

#headersHash (readonly)

The HTTP headers of the response.



11
12
13
# File 'lib/service_client/response.rb', line 11

def headers
  @headers
end

Class Method Details

.call(response) ⇒ ServiceClient::Response

Initializes a new instance of Response and calls it to determine success or failure.

and object if the response was unsuccessful.

Raises:

  • (ServiceClient::Error)

    Raises an error with the response code



45
46
47
# File 'lib/service_client/response.rb', line 45

def call(response)
  new(response).call
end

Instance Method Details

#callServiceClient::Response

Determines whether the response was successful or not.

Raises:

  • (ServiceClient::Error)

    Raises an error with the response code and object if the response was unsuccessful.



29
30
31
32
33
34
35
36
# File 'lib/service_client/response.rb', line 29

def call
  case code
  when 200..299
    self
  else
    raise_error(code.to_s, self)
  end
end