Sha256: bb47a616c6d60ab0792ea8a25356a3e0f7b1fcb4fe02f1a0dcfb873825ebed93

Contents?: true

Size: 1.76 KB

Versions: 1

Compression:

Stored size: 1.76 KB

Contents

# frozen_string_literal: true

require 'friendly_shipping/rate'
require 'friendly_shipping/api_result'
require 'friendly_shipping/services/tforce_freight/shipping_methods'

module FriendlyShipping
  module Services
    class TForceFreight
      # Parses a pickup response into an `ApiResult`.
      class ParsePickupResponse
        class << self
          # @param request [Request] the original request
          # @param response [RestClient::Response] the response to parse
          # @return [ApiResult<Hash>] the parsed result
          def call(request:, response:)
            json = JSON.parse(response.body)

            response_status_code = json.dig("responseStatus", "code")
            response_status_description = json.dig("responseStatus", "description")
            transaction_id = json.dig("transactionReference", "transactionId")
            confirmation_number = json.dig("transactionReference", "confirmationNumber")
            email_sent = json.dig("transactionReference", "emailSent")
            origin_is_rural = json.dig("transactionReference", "originIsRural")
            destination_is_rural = json.dig("transactionReference", "destinationIsRural")

            FriendlyShipping::ApiResult.new(
              {
                response_status_code: response_status_code,
                response_status_description: response_status_description,
                transaction_id: transaction_id,
                confirmation_number: confirmation_number,
                email_sent: email_sent,
                origin_is_rural: origin_is_rural,
                destination_is_rural: destination_is_rural
              },
              original_request: request,
              original_response: response
            )
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
friendly_shipping-0.9.0 lib/friendly_shipping/services/tforce_freight/parse_pickup_response.rb