Method: ActiveShipping::USPS#rates_from_response_node

Defined in:
lib/active_shipping/carriers/usps.rb

#rates_from_response_node(response_node, packages, options = {}) ⇒ Object (protected)



443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
# File 'lib/active_shipping/carriers/usps.rb', line 443

def rates_from_response_node(response_node, packages, options = {})
  rate_hash = {}
  return false unless (root_node = response_node.at_xpath('/IntlRateV2Response | /RateV4Response'))

  commercial_type = commercial_type(options)
  service_node, service_code_node, service_name_node, rate_node = if root_node.name == 'RateV4Response'
    %w(Postage CLASSID MailService) << DOMESTIC_RATE_FIELD[commercial_type]
  else
    %w(Service ID SvcDescription)   << INTERNATIONAL_RATE_FIELD[commercial_type]
  end

  root_node.xpath('Package').each do |package_node|
    this_package = packages[package_node['ID'].to_i]

    package_node.xpath(service_node).each do |service_response_node|
      service_name = service_response_node.at(service_name_node).text

      service_name.gsub!(SERVICE_NAME_SUBSTITUTIONS, '')

      # aggregate specific package rates into a service-centric RateEstimate
      # first package with a given service name will initialize these;
      # later packages with same service will add to them
      this_service = rate_hash[service_name] ||= {}
      this_service[:service_code] ||= service_response_node.attributes[service_code_node].value
      package_rates = this_service[:package_rates] ||= []
      this_package_rate = {:package => this_package,
                           :rate => Package.cents_from(rate_value(rate_node, service_response_node, commercial_type))}

      package_rates << this_package_rate if package_valid_for_service(this_package, service_response_node)
    end
  end
  rate_hash
end