Sha256: 74c140e8b387d70e04523b43f1d7959faf22b54ded1d98194d83e19c65a36c3b

Contents?: true

Size: 1.35 KB

Versions: 25

Compression:

Stored size: 1.35 KB

Contents

# frozen_string_literal: true

require 'json'
require 'data_uri'

module FriendlyShipping
  module Services
    class ShipEngine
      class ParseLabelResponse
        def self.call(request:, response:)
          parsed_json = JSON.parse(response.body)

          label_uri_string = parsed_json['label_download']['href']
          label_data = nil
          label_url = nil
          if label_uri_string.starts_with?('data')
            label_data = URI::Data.new(label_uri_string).data
          else
            label_url = label_uri_string
          end

          currency = parsed_json.dig('shipment_cost', 'currency')
          cents = parsed_json.dig('shipment_cost', 'amount') * 100
          shipment_cost = Money.new(cents, currency)

          label = FriendlyShipping::Label.new(
            id: parsed_json['label_id'],
            shipment_id: parsed_json['shipment_id'],
            tracking_number: parsed_json['tracking_number'],
            service_code: parsed_json['service_code'],
            label_href: label_url,
            label_data: label_data,
            label_format: parsed_json['label_format'].to_sym,
            shipment_cost: shipment_cost,
            cost: shipment_cost,
            data: parsed_json
          )

          ApiResult.new([label], original_request: request, original_response: response)
        end
      end
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
friendly_shipping-0.6.5 lib/friendly_shipping/services/ship_engine/parse_label_response.rb
friendly_shipping-0.6.4 lib/friendly_shipping/services/ship_engine/parse_label_response.rb
friendly_shipping-0.6.3 lib/friendly_shipping/services/ship_engine/parse_label_response.rb
friendly_shipping-0.6.2 lib/friendly_shipping/services/ship_engine/parse_label_response.rb
friendly_shipping-0.6.1 lib/friendly_shipping/services/ship_engine/parse_label_response.rb
friendly_shipping-0.6.0 lib/friendly_shipping/services/ship_engine/parse_label_response.rb
friendly_shipping-0.5.3 lib/friendly_shipping/services/ship_engine/parse_label_response.rb
friendly_shipping-0.5.2 lib/friendly_shipping/services/ship_engine/parse_label_response.rb
friendly_shipping-0.5.1 lib/friendly_shipping/services/ship_engine/parse_label_response.rb
friendly_shipping-0.5 lib/friendly_shipping/services/ship_engine/parse_label_response.rb
friendly_shipping-0.4.14 lib/friendly_shipping/services/ship_engine/parse_label_response.rb
friendly_shipping-0.4.13 lib/friendly_shipping/services/ship_engine/parse_label_response.rb
friendly_shipping-0.4.12 lib/friendly_shipping/services/ship_engine/parse_label_response.rb
friendly_shipping-0.4.11 lib/friendly_shipping/services/ship_engine/parse_label_response.rb
friendly_shipping-0.4.10 lib/friendly_shipping/services/ship_engine/parse_label_response.rb
friendly_shipping-0.4.9 lib/friendly_shipping/services/ship_engine/parse_label_response.rb
friendly_shipping-0.4.8 lib/friendly_shipping/services/ship_engine/parse_label_response.rb
friendly_shipping-0.4.7 lib/friendly_shipping/services/ship_engine/parse_label_response.rb
friendly_shipping-0.4.6 lib/friendly_shipping/services/ship_engine/parse_label_response.rb
friendly_shipping-0.4.5 lib/friendly_shipping/services/ship_engine/parse_label_response.rb