Class: Fedex::UpsInfo

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

Overview

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

Constant Summary collapse

TEST_URL =
"https://wwwcie.ups.com/ups.app/xml"
PRODUCTION_URL =

UPS Production URL

"https://onlinetools.ups.com/ups.app/xml"
XPCI_VERSION =
'1.0002'
DEFAULT_CUTOFF_TIME =
14
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) ⇒ UpsInfo

Returns a new instance of UpsInfo.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ups/UpsInfo.rb', line 25

def initialize(access_options)
  @order_cutoff_time = access_options[:order_cutoff_time] || DEFAULT_CUTOFF_TIME      
  @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]
    }
  }
  
   @rate_from_attributes = {
    :Address => {
      :City => access_options[:sender_city],
      :StateProvinceCode => access_options[:sender_state],
      :CountryCode => access_options[:sender_country_code] || DEFAULT_COUNTRY_CODE,
      :PostalCode => access_options[:sender_zip]
     }
    }
  
end

Instance Method Details

#getPrice(options) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/ups/UpsInfo.rb', line 58

def getPrice (options)
   
   @url = options[:mode] == "production" ? PRODUCTION_URL : TEST_URL + '/Rate'
   
   #@url = options[:url] + '/Rate'
   # build our request xml 
   
   xml = @access_xml + generate_xml(build_price_attributes(options))
   #puts xml
   # attempt the request in a timeout
   delivery_price = 0
   
   begin 
     Timeout.timeout(@timeout) do
       response = send_request(@url, xml)
       delivery_price = response_to_price(response)
   end
   delivery_price
 end
end

#getTransitTime(options) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/ups/UpsInfo.rb', line 79

def getTransitTime(options)
  
  @url = options[:mode] == "production" ? PRODUCTION_URL : TEST_URL + '/TimeInTransit'
  #@url = options[:url] + '/TimeInTransit'
  # build our request xml
  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