class ReactiveShipping::Shipwire

Constants

CARRIERS
NO_RATES_MESSAGE
REQUIRED_OPTIONS
SCHEMA_URL
SUCCESS
SUCCESS_MESSAGE
URL
WAREHOUSES

Public Instance Methods

find_rates(origin, destination, packages, options = {}) click to toggle source
# File lib/reactive_shipping/carriers/shipwire.rb, line 25
def find_rates(origin, destination, packages, options = {})
  requires!(options, :items)
  commit(origin, destination, options)
end
valid_credentials?() click to toggle source
# File lib/reactive_shipping/carriers/shipwire.rb, line 30
def valid_credentials?
  location = self.class.default_location
  find_rates(location, location, Package.new(100, [5, 15, 30]),
             :items => [{ :sku => '', :quantity => 1 }]
  )
rescue ReactiveShipping::ResponseError
  true
rescue ActiveUtils::ResponseError => e
  e.response.code != '401'
end

Private Instance Methods

add_address(xml, destination) click to toggle source
# File lib/reactive_shipping/carriers/shipwire.rb, line 73
def add_address(xml, destination)
  xml.AddressInfo :type => 'Ship' do
    if destination.name.present?
      xml.Name do
        xml.Full destination.name
      end
    end
    xml.Address1 destination.address1
    xml.Address2 destination.address2 unless destination.address2.blank?
    xml.Address3 destination.address3 unless destination.address3.blank?
    xml.Company destination.company unless destination.company.blank?
    xml.City destination.city
    xml.State destination.state unless destination.state.blank?
    xml.Country destination.country_code
    xml.Zip destination.zip  unless destination.zip.blank?
  end
end
add_credentials(xml) click to toggle source
# File lib/reactive_shipping/carriers/shipwire.rb, line 57
def add_credentials(xml)
  xml.EmailAddress @options[:login]
  xml.Password @options[:password]
end
add_item(xml, item, index) click to toggle source

Code is limited to 12 characters

# File lib/reactive_shipping/carriers/shipwire.rb, line 92
def add_item(xml, item, index)
  xml.Item :num => index do
    xml.Code item[:sku]
    xml.Quantity item[:quantity]
  end
end
add_order(xml, destination, options) click to toggle source
# File lib/reactive_shipping/carriers/shipwire.rb, line 62
def add_order(xml, destination, options)
  xml.Order :id => options[:order_id] do
    xml.Warehouse options[:warehouse] || '00'

    add_address(xml, destination)
    Array(options[:items]).each_with_index do |line_item, index|
      add_item(xml, line_item, index)
    end
  end
end
build_rate_estimates(response, origin, destination) click to toggle source
# File lib/reactive_shipping/carriers/shipwire.rb, line 112
def build_rate_estimates(response, origin, destination)
  response["rates"].collect do |quote|
    RateEstimate.new(origin, destination, carrier_for(quote["service"]), quote["service"],
                     :service_code    => quote["method"],
                     :total_price     => quote["cost"],
                     :currency        => quote["currency"],
                     :delivery_range  => [timestamp_from_business_day(quote["delivery_min"]),
                                          timestamp_from_business_day(quote["delivery_max"])]
    )
  end
end
build_request(destination, options) click to toggle source
# File lib/reactive_shipping/carriers/shipwire.rb, line 47
def build_request(destination, options)
  Nokogiri::XML::Builder.new do |xml|
    xml.doc.create_internal_subset('RateRequest', nil, SCHEMA_URL)
    xml.RateRequest do
      add_credentials(xml)
      add_order(xml, destination, options)
    end
  end.to_xml
end
carrier_for(service) click to toggle source
# File lib/reactive_shipping/carriers/shipwire.rb, line 124
def carrier_for(service)
  CARRIERS.dup.find { |carrier| service.to_s =~ /^#{carrier}/i } || service.to_s.split(" ").first
end
commit(origin, destination, options) click to toggle source
# File lib/reactive_shipping/carriers/shipwire.rb, line 99
def commit(origin, destination, options)
  request = build_request(destination, options)
  save_request(request)

  response = parse( ssl_post(URL, "RateRequestXML=#{CGI.escape(request)}") )

  RateResponse.new(response["success"], response["message"], response,
                   :xml     => response,
                   :rates   => build_rate_estimates(response, origin, destination),
                   :request => last_request
  )
end
parse(xml) click to toggle source
# File lib/reactive_shipping/carriers/shipwire.rb, line 128
def parse(xml)
  response = {}
  response["rates"] = []

  document = Nokogiri.XML(xml)

  response["status"] = parse_child_text(document.root, "Status")

  document.root.xpath("Order/Quotes/Quote").each do |e|
    rate = {}
    rate["method"]    = e["method"]
    rate["warehouse"] = parse_child_text(e, "Warehouse")
    rate["service"]   = parse_child_text(e, "Service")
    rate["cost"]      = parse_child_text(e, "Cost")
    rate["currency"]  = parse_child_attribute(e, "Cost", "currency")
    if delivery_estimate = e.at("DeliveryEstimate")
      rate["delivery_min"]  = parse_child_text(delivery_estimate, "Minimum").to_i
      rate["delivery_max"]  = parse_child_text(delivery_estimate, "Maximum").to_i
    end
    response["rates"] << rate
  end

  if response["status"] == SUCCESS && response["rates"].any?
    response["success"] = true
    response["message"] = SUCCESS_MESSAGE
  elsif response["status"] == SUCCESS && response["rates"].empty?
    response["success"] = false
    response["message"] = NO_RATES_MESSAGE
  else
    response["success"] = false
    response["message"] = parse_child_text(document.root, "ErrorMessage")
  end

  response
rescue NoMethodError => e
  raise ReactiveShipping::ResponseContentError.new(e, xml)
end
parse_child_attribute(parent, name, attribute) click to toggle source
# File lib/reactive_shipping/carriers/shipwire.rb, line 172
def parse_child_attribute(parent, name, attribute)
  if element = parent.at(name)
    element[attribute]
  end
end
parse_child_text(parent, name) click to toggle source
# File lib/reactive_shipping/carriers/shipwire.rb, line 166
def parse_child_text(parent, name)
  if element = parent.at(name)
    element.text
  end
end
requirements() click to toggle source
# File lib/reactive_shipping/carriers/shipwire.rb, line 43
def requirements
  REQUIRED_OPTIONS
end