Class: FriendlyShipping::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/friendly_shipping/response.rb

Overview

Represents an HTTP response received from a carrier API.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(status:, body:, headers:) ⇒ Response

Returns a new instance of Response.

Parameters:

  • status (Integer)

    the HTTP response status code

  • body (String)

    the HTTP response body

  • headers (Hash)

    the HTTP response headers



18
19
20
21
22
# File 'lib/friendly_shipping/response.rb', line 18

def initialize(status:, body:, headers:)
  @status = status
  @body = body
  @headers = headers || {}
end

Instance Attribute Details

#bodyString (readonly)

Returns the HTTP response body.

Returns:

  • (String)

    the HTTP response body



10
11
12
# File 'lib/friendly_shipping/response.rb', line 10

def body
  @body
end

#headersHash (readonly)

Returns the HTTP response headers.

Returns:

  • (Hash)

    the HTTP response headers



13
14
15
# File 'lib/friendly_shipping/response.rb', line 13

def headers
  @headers
end

#statusInteger (readonly) Also known as: code

Returns the HTTP response status code.

Returns:

  • (Integer)

    the HTTP response status code



7
8
9
# File 'lib/friendly_shipping/response.rb', line 7

def status
  @status
end

Class Method Details

.new_from_rest_client_response(response) ⇒ Response

Constructs a new FriendlyShipping::Response from a RestClient::Response object.

Parameters:

  • response (RestClient::Response)

    the response to use

Returns:



29
30
31
# File 'lib/friendly_shipping/response.rb', line 29

def self.new_from_rest_client_response(response)
  new(status: response&.code, body: response&.body, headers: response&.headers)
end

Instance Method Details

#==(other) ⇒ Boolean Also known as: eql?

Returns true if the given object shares the same class and attributes with this response.

Parameters:

  • other (Object)

Returns:

  • (Boolean)


36
37
38
39
# File 'lib/friendly_shipping/response.rb', line 36

def ==(other)
  other.class == self.class &&
    other.attributes == attributes
end

#attributesArray (protected)

Returns the status, body, and headers from this response.

Returns:

  • (Array)


53
54
55
# File 'lib/friendly_shipping/response.rb', line 53

def attributes
  [status, body, headers]
end

#hashHash

Returns this response's attributes as a hash.

Returns:

  • (Hash)


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

def hash
  attributes.hash
end