Sha256: 60a8915a7302045ceb7bba49505dbf279d5d7e7c3e3aec1bd0d8e498c8114954
Contents?: true
Size: 1.57 KB
Versions: 3
Compression:
Stored size: 1.57 KB
Contents
# frozen_string_literal: true require 'json' require 'money' module FriendlyShipping module Services class ShipEngine class ParseRateEstimateResponse class << self def call(response:, carriers:, request:) parsed_json = JSON.parse(response.body) parsed_json.map do |rate| carrier = carriers.detect { |c| c.id == rate['carrier_id'] } next unless carrier shipping_method = carrier.shipping_methods.detect { |sm| sm.service_code == rate['service_code'] } next unless shipping_method amounts = get_amounts(rate) FriendlyShipping::Rate.new( shipping_method: shipping_method, amounts: amounts, remote_service_id: rate['rate_id'], delivery_date: Time.parse(rate['estimated_delivery_date']), warnings: rate['warning_messages'], errors: rate['error_messages'], original_request: request, original_response: response ) end.compact end private def get_amounts(rate_hash) [:shipping, :other, :insurance, :confirmation].map do |name| currency = Money::Currency.new(rate_hash["#{name}_amount"]["currency"]) amount = rate_hash["#{name}_amount"]["amount"] * currency.subunit_to_unit [ name, Money.new(amount, currency) ] end.to_h end end end end end end
Version data entries
3 entries across 3 versions & 1 rubygems