Class: FriendlyShipping::Services::ShipEngine::ParseRatesResponse

Inherits:
Object
  • Object
show all
Extended by:
Dry::Monads::Result::Mixin
Defined in:
lib/friendly_shipping/services/ship_engine/parse_rates_response.rb

Overview

Parses the rates API response.

Class Method Summary collapse

Class Method Details

.call(request:, response:, options:) ⇒ Success<ApiResult<Array<Rate>>>, Failure<ApiResult<String>>

Returns the parsed rates or error.

Parameters:

  • request (Request)

    the request to attach to the API result

  • response (Response)

    the response to parse

  • options (RatesOptions)

    the options to use when parsing

Returns:



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/friendly_shipping/services/ship_engine/parse_rates_response.rb', line 15

def call(request:, response:, options:)
  parsed_json = JSON.parse(response.body)
  rates = build_rates(parsed_json, options)
  if rates.any?
    Success(
      ApiResult.new(
        rates,
        original_request: request,
        original_response: response
      )
    )
  else
    error = parsed_json.dig("rate_response", "errors", 0, "message") || "Unknown error"
    Failure(
      ApiResult.new(
        error,
        original_request: request,
        original_response: response
      )
    )
  end
end