Sha256: 621c184e2aab217cbbc303087f94db927e22b98f8494015d31d7ea69dd6a72a3

Contents?: true

Size: 1.58 KB

Versions: 34

Compression:

Stored size: 1.58 KB

Contents

# frozen_string_literal: true

require 'json'

module FriendlyShipping
  module Services
    class ShipEngine
      class ParseCarrierResponse
        class << self
          def call(request:, response:)
            parsed_json = JSON.parse(response.body)
            carriers = parsed_json['carriers'].map do |carrier_data|
              carrier = FriendlyShipping::Carrier.new(
                id: carrier_data['carrier_id'],
                name: carrier_data['friendly_name'],
                code: carrier_data['carrier_code'],
                balance: carrier_data['balance'],
                data: carrier_data
              )

              carrier_data['services'].each do |method_hash|
                shipping_method = parse_shipping_method(carrier, method_hash)
                carrier.shipping_methods << shipping_method
              end

              carrier
            end

            ApiResult.new(
              carriers,
              original_request: request,
              original_response: response
            )
          end

          private

          def parse_shipping_method(carrier, shipping_method_data)
            FriendlyShipping::ShippingMethod.new(
              carrier: carrier,
              name: shipping_method_data["name"],
              service_code: shipping_method_data["service_code"],
              domestic: shipping_method_data["domestic"],
              international: shipping_method_data["international"],
              multi_package: shipping_method_data["is_multi_package_supported"]
            )
          end
        end
      end
    end
  end
end

Version data entries

34 entries across 34 versions & 1 rubygems

Version Path
friendly_shipping-0.8.1 lib/friendly_shipping/services/ship_engine/parse_carrier_response.rb
friendly_shipping-0.8.0 lib/friendly_shipping/services/ship_engine/parse_carrier_response.rb
friendly_shipping-0.7.3 lib/friendly_shipping/services/ship_engine/parse_carrier_response.rb
friendly_shipping-0.7.2 lib/friendly_shipping/services/ship_engine/parse_carrier_response.rb
friendly_shipping-0.7.1 lib/friendly_shipping/services/ship_engine/parse_carrier_response.rb
friendly_shipping-0.7.0 lib/friendly_shipping/services/ship_engine/parse_carrier_response.rb
friendly_shipping-0.6.5 lib/friendly_shipping/services/ship_engine/parse_carrier_response.rb
friendly_shipping-0.6.4 lib/friendly_shipping/services/ship_engine/parse_carrier_response.rb
friendly_shipping-0.6.3 lib/friendly_shipping/services/ship_engine/parse_carrier_response.rb
friendly_shipping-0.6.2 lib/friendly_shipping/services/ship_engine/parse_carrier_response.rb
friendly_shipping-0.6.1 lib/friendly_shipping/services/ship_engine/parse_carrier_response.rb
friendly_shipping-0.6.0 lib/friendly_shipping/services/ship_engine/parse_carrier_response.rb
friendly_shipping-0.5.3 lib/friendly_shipping/services/ship_engine/parse_carrier_response.rb
friendly_shipping-0.5.2 lib/friendly_shipping/services/ship_engine/parse_carrier_response.rb
friendly_shipping-0.5.1 lib/friendly_shipping/services/ship_engine/parse_carrier_response.rb
friendly_shipping-0.5 lib/friendly_shipping/services/ship_engine/parse_carrier_response.rb
friendly_shipping-0.4.14 lib/friendly_shipping/services/ship_engine/parse_carrier_response.rb
friendly_shipping-0.4.13 lib/friendly_shipping/services/ship_engine/parse_carrier_response.rb
friendly_shipping-0.4.12 lib/friendly_shipping/services/ship_engine/parse_carrier_response.rb
friendly_shipping-0.4.11 lib/friendly_shipping/services/ship_engine/parse_carrier_response.rb