lib/friendly_shipping/services/usps/parse_rate_response.rb
# Parse a response from USPS' rating API
#
# @param [FriendlyShipping::Request] request The request that was used to obtain this Response
# @param [FriendlyShipping::Response] response The response that USPS returned
# @param [Physical::Shipment] shipment The shipment object we're trying to get results for
# @param [FriendlyShipping::Services::Usps::RateEstimateOptions] options The options we sent with this request
# @return [Result<ApiResult<Array<FriendlyShipping::Rate>>>] When successfully parsing, an array of rates in a Success Monad.
Parse a response from USPS' rating API
Parse a response from USPS ' rating API
def call(request:, response:, shipment:, options:)
            parsing_result = ParseXMLResponse.call(
              request: request,
              response: response,
              expected_root_tag: 'RateV4Response'
            )
            parsing_result.fmap do |xml|
              rates_by_package = rates_from_response_node(xml, shipment, options)
              rates = SHIPPING_METHODS.map do |shipping_method|
                matching_rates = rates_by_package.map do |package, package_rates|
                  package_options = options.options_for_package(package)
                  ChoosePackageRate.call(shipping_method, package_rates, package_options)
                end.compact 
                next if matching_rates.empty?
                FriendlyShipping::Rate.new(
                  amounts: matching_rates.map(&:amounts).reduce({}, :merge),
                  shipping_method: shipping_method,
                  data: matching_rates.first.data
                )
              end.compact
              ApiResult.new(
                rates,
                original_request: request,
                original_response: response
              )
            end
          end
def call ( request : , response : , shipment : , options : ) parsing_result = ParseXMLResponse . call ( request : request , response : response , expected_root_tag : 'RateV4Response' ) parsing_result . fmap do | xml | rates_by_package = rates_from_response_node ( xml , shipment , options ) rates = SHIPPING_METHODS . map do | shipping_method | matching_rates = rates_by_package . map do | package , package_rates | package_options = options . options_for_package ( package ) ChoosePackageRate . call ( shipping_method , package_rates , package_options ) end . compact next if matching_rates . empty? FriendlyShipping :: Rate . new ( amounts : matching_rates . map ( & :amounts ) . reduce ( { } , :merge ) , shipping_method : shipping_method , data : matching_rates . first . data ) end . compact ApiResult . new ( rates , original_request : request , original_response : response ) end end
Parse a response from USPS' rating API
Parse a response from USPS ' rating API
# Filter out error responses and directly return a failure # Get all the possible rates for each package # For every package ... # ... choose the rate that fits this package best. # Some shipping rates are not available for every shipping method. # in this case, go to the next shipping method. # return one rate for all packages with the amount keys being the package IDs.
When successfully parsing, an array of rates in a Success Monad.
When successfully parsing an array of rates in a Success Monad .
Result<ApiResult<Array<FriendlyShipping::Rate>>>
request
<not_specific>
response
<not_specific>
shipment
<not_specific>
options
<not_specific>
<not_specific>
options The options we sent with this request
options The options we sent with this request