Class: FriendlyShipping::ApiResult

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

Overview

Wraps an API result (a response body, for example) along with the original request and response objects.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, original_request: nil, original_response: nil) ⇒ ApiResult

Returns a new instance of ApiResult. The original request and response are only attached to this object if debugging is enabled. See Request#debug

Parameters:

  • data (Object)

    the API result (typically the response body)

  • original_request (Request) (defaults to: nil)

    the original API request

  • original_response (Response) (defaults to: nil)

    the original API response



27
28
29
30
31
32
33
34
35
# File 'lib/friendly_shipping/api_result.rb', line 27

def initialize(data, original_request: nil, original_response: nil)
  @data = data

  # We do not want to attach debugging information in every single response to save memory in production
  return unless original_request&.debug

  @original_request = original_request
  @original_response = original_response
end

Instance Attribute Details

#dataObject (readonly) Also known as: failure

The API failure (typically an exception). This is here to maintain backwards compatibility with the deprecated FriendlyShipping::ApiResult class.



8
9
10
# File 'lib/friendly_shipping/api_result.rb', line 8

def data
  @data
end

#original_requestRequest (readonly)

Returns the original API request (if debugging is enabled).

Returns:

  • (Request)

    the original API request (if debugging is enabled)



16
17
18
# File 'lib/friendly_shipping/api_result.rb', line 16

def original_request
  @original_request
end

#original_responseResponse (readonly)

Returns the original API response (if debugging is enabled).

Returns:

  • (Response)

    the original API response (if debugging is enabled)



19
20
21
# File 'lib/friendly_shipping/api_result.rb', line 19

def original_response
  @original_response
end

Instance Method Details

#to_s#to_s

Returns a string representation of the data.

Returns:

  • (#to_s)

    a string representation of the data



38
39
40
# File 'lib/friendly_shipping/api_result.rb', line 38

def to_s
  data.to_s
end