Class: UPS::TimeInTransitPlus

Inherits:
Object
  • Object
show all
Defined in:
lib/ups_time_in_transit_plus.rb

Overview

Provides a simple api to to ups’s time in transit service.

Constant Summary collapse

XPCI_VERSION =
'1.0002'
DEFAULT_CUTOFF_TIME =
18
DEFAULT_TIMEOUT =
30
DEFAULT_RETRY_COUNT =
3
DEFAULT_COUNTRY_CODE =
'US'
DEFAULT_UNIT_OF_MEASUREMENT =
'LBS'

Instance Method Summary collapse

Constructor Details

#initialize(access_options) ⇒ TimeInTransitPlus

Creates a TimeInTransit instance based on the given hash of access options The following access options are available and are required unless a default value is specified:

:url

The ups api url to use

:access_license_number

Your ups license number

:user_id

Your ups user id

password

Your ups password

:order_cutoff_time

Your own arbitrary cutoff time that is some time before the actual ups cutoff time. Requests made after this time will use the following day as the send date (or the following monday if the request is made on a weekend or on a friday after this time.)

:sender_city

The city you are shipping from

:sender_state

The state you are shipping from

:sender_zip

The zip code you are shipping from

:sender_country_code

The country you are shipping from (defaults to ‘US’)

:retry_count

The number of times you would like to retry when a connection fails (defaults to 3)

:timeout

The number of seconds you would like to wait for a response before giving up (defaults to 30)



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/ups_time_in_transit_plus.rb', line 60

def initialize(access_options)
  @order_cutoff_time = access_options[:order_cutoff_time] || DEFAULT_CUTOFF_TIME
  @url = access_options[:url]
  @timeout = access_options[:timeout] || DEFAULT_TIMEOUT 
  @retry_count = access_options[:retry_count] || DEFAULT_CUTOFF_TIME 

  @access_xml = generate_xml({
    :AccessRequest => {
      :AccessLicenseNumber => access_options[:access_license_number],
      :UserId => access_options[:user_id],
      :Password => access_options[:password]
    }
  })

  @transit_from_attributes = {
    :AddressArtifactFormat => {
      :PoliticalDivision2 => access_options[:sender_city],
      :PoliticalDivision1 => access_options[:sender_state],
      :CountryCode => access_options[:sender_country_code] || DEFAULT_COUNTRY_CODE,
      :PostcodePrimaryLow => access_options[:sender_zip]
    }
  }
end

Instance Method Details

#request(options) ⇒ Object

Requests time in transit information based on the given hash of options:

:total_packages

the number of packages in the shipment (defaults to 1)

:unit_of_measurement

the unit of measurement to use (defaults to ‘LBS’)

:weight

the weight of the shipment in the given units

:city

the city you are shipping to

:state

the state you are shipping to

:zip

the zip code you are shipping to

:country_code

the country you are shipping to (defaults to ‘US’)

An error will be raised if the request is unsuccessful.



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/ups_time_in_transit_plus.rb', line 109

def request(options)
  
  # build our request xml
  pickup_date = options[:pickup_date] || calculate_pickup_date
  options[:pickup_date] = pickup_date.strftime('%Y%m%d')
  xml = @access_xml + generate_xml(build_transit_attributes(options))

  # attempt the request in a timeout
  delivery_dates = {}
  attempts = 0
  begin 
    Timeout.timeout(@timeout) do
      response = send_request(@url, xml)
      delivery_dates = response_to_map(response)
    end

  # We can only attempt to recover from Timeout errors, all other errors
  # should be raised back to the user
  rescue Timeout::Error => error
    if(attempts < @retry_count)
      attempts += 1
      retry

    else
      raise error
    end
  end

  delivery_dates
end