Class: Shipping::FedEx

Inherits:
Base show all
Defined in:
lib/shipping/fedex.rb

Instance Attribute Summary

Attributes inherited from Base

#address, #address2, #city, #company, #country, #currency_code, #data, #declared_value, #description, #dropoff_type, #email, #eta, #fedex_account, #fedex_meter, #fedex_package_weight_limit_in_lbs, #fedex_url, #image_type, #insured_value, #label_type, #measure_height, #measure_length, #measure_units, #measure_width, #name, #package_total, #packaging_type, #pay_type, #phone, #plain_response, #required, #response, #sender_address, #sender_city, #sender_company, #sender_country, #sender_email, #sender_name, #sender_phone, #sender_state, #sender_zip, #service_type, #services, #ship_date, #state, #time_in_transit, #transaction_type, #ups_license_number, #ups_password, #ups_shipper_number, #ups_tool, #ups_url, #ups_user, #weight, #weight_units, #zip

Instance Method Summary collapse

Methods inherited from Base

#fedex, #initialize, plain, state_from_zip, #ups

Constructor Details

This class inherits a constructor from Shipping::Base

Instance Method Details

#available_services(force = nil) ⇒ Object



404
405
406
407
408
409
410
# File 'lib/shipping/fedex.rb', line 404

def available_services(force=nil)
 if @services.empty? || force
   get_available_services
 else
   @services
  end
end

#discount_priceObject

Gets the discount price of the shipping (with discounts taken into consideration). “base price” doesn’t include surcharges like fuel cost, so I don’t think it is the correct price to get



21
22
23
24
25
26
# File 'lib/shipping/fedex.rb', line 21

def discount_price
	get_price
	return REXML::XPath.first(@response, "//FDXRateReply/EstimatedCharges/DiscountedCharges/NetCharge").text.to_f
rescue ShippingError
	raise ShippingError, get_error
end

#express_service_availabilityObject

still not sure what the best way to handle this transaction’s return data would be. Possibly a hash of the form => delivery_estimate?



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/shipping/fedex.rb', line 29

def express_service_availability
	@data = String.new
	b = Builder::XmlMarkup.new :target => @data
	b.instruct!
	b.FDXServiceAvailabilityRequest('xmlns:api' => 'http://www.fedex.com/fsmapi', 'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance', 'xsi:noNamespaceSchemaLocation' => 'FDXServiceAvailabilityRequest.xsd') { |b|
		b.RequestHeader { |b|
			b.AccountNumber @fedex_account
			b.MeterNumber @fedex_meter
		}
		b.OriginAddress { |b|
			b.PostalCode @sender_zip
			b.CountryCode @sender_country_code || "US"
		}
		b.DestinationAddress { |b|
			b.PostalCode @zip
			b.CountryCode @country || "US"
		}
		b.ShipDate @ship_date unless @ship_date.blank?
		b.PackageCount @package_total || '1'
	}

	get_response @fedex_url
end

#labelObject

require ‘fileutils’

fedex = Shipping::FedEx.new :name => 'John Doe', ... , :sender_zip => 97202
label = fedex.label
puts label.tracking_number
FileUtils.cp label.image.path, '/path/to/my/images/directory/'

There are several types of labels that can be returned by changing @image_type. PNG is selected by default.



98
99
100
101
102
103
104
105
106
107
108
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/shipping/fedex.rb', line 98

def label     
	@required =  [:phone,        :email,        :address,        :city,        :state,        :zip        ]
	@required += [:sender_phone, :sender_email, :sender_address, :sender_city, :sender_state, :sender_zip ]
	@required += [:fedex_account, :fedex_url, :fedex_meter]

	@transaction_type ||= 'ship_ground'
	@weight = (@weight.to_f*10).round/10.0
	@declared_value = (@declared_value.to_f*100).round/100.0 unless @declared_value.blank?
	state = STATES.has_value?(@state.downcase) ? STATES.index(@state.downcase).upcase : @state.upcase
	sender_state = STATES.has_value?(@sender_state.downcase) ? STATES.index(@sender_state.downcase).upcase : @sender_state.upcase

	@data = String.new
	b = Builder::XmlMarkup.new :target => @data
	b.instruct!
	b.FDXShipRequest('xmlns:api' => 'http://www.fedex.com/fsmapi', 'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance', 'xsi:noNamespaceSchemaLocation' => 'FDXShipRequest.xsd') { |b|
		b.RequestHeader { |b|
			b.AccountNumber @fedex_account
			b.MeterNumber @fedex_meter
			b.CarrierCode TransactionTypes[@transaction_type][1]
		}
		b.ShipDate((Time.now).strftime("%Y-%m-%d"))
		b.ShipTime((Time.now).strftime("%H:%M:%S"))
		b.DropoffType @dropoff_type || 'REGULARPICKUP'
		b.Service ServiceTypes[@service_type] || ServiceTypes['ground_service'] # default to ground service
		b.Packaging PackageTypes[@packaging_type] || 'YOURPACKAGING'
		b.WeightUnits @weight_units || 'LBS' # or KGS
		b.Weight @weight
		b.CurrencyCode @currency_code || 'USD'
		b.Origin { |b|
			b.Contact { |b|
				if @sender_name.to_s.size > 2
					b.PersonName @sender_name
					b.CompanyName @sender_company unless @sender_company.blank?
				elsif @sender_company.to_s.size > 2
					b.PersonName @sender_name unless @sender_name.blank?
					b.CompanyName @sender_company
				else
					raise ShippingError, "Either the sender_name or the sender_company value must be bigger than 2 characters."
				end
				b.Department @sender_department unless @sender_department.blank?
				b.PhoneNumber @sender_phone.gsub(/[^\d]/,"")
				b.PagerNumber @sender_pager.gsub(/[^\d]/,"") if @sender_pager.class == String
				b.FaxNumber @sender_fax.gsub(/[^\d]/,"") if @sender_fax.class == String
				b.tag! :"E-MailAddress", @sender_email
			}
			b.Address { |b|
				b.Line1 @sender_address
				b.Line2 @sender_address2 unless @sender_address2.blank?
				b.City @sender_city
				b.StateOrProvinceCode sender_state
				b.PostalCode @sender_zip
				b.CountryCode @sender_country || 'US'
			}
		}
		b.Destination { |b|
			b.Contact { |b|
				if @name.to_s.size > 2
					b.PersonName @name
					b.CompanyName @company unless @company.blank?
				elsif @company.to_s.size > 2
					b.PersonName @name unless @name.blank?
					b.CompanyName @company
				else
					raise ShippingError, "Either the name or the company value must be bigger than 2 characters."
				end
				b.Department @department unless @department.blank?
				b.PhoneNumber @phone.gsub(/[^\d]/,"")
				b.PagerNumber @pager.gsub(/[^\d]/,"") if @pager.class == String
				b.FaxNumber @fax.gsub(/[^\d]/,"") if @fax.class == String
				b.tag! :"E-MailAddress", @email
			}
			b.Address { |b|
				b.Line1 @address
				b.Line2 @address2 unless @address2.blank?
				b.City @city
				b.StateOrProvinceCode state
				b.PostalCode @zip
				b.CountryCode @country || 'US'
			}
		}
		b.Payment { |b|
			b.PayorType PaymentTypes[@pay_type] || 'SENDER'
			b.Payor { |b|
				b.AccountNumber @payor_account_number
				b.CountryCode @payor_country_code unless @payor_country_code.blank?
			} unless @payor_account_number.blank?
		}
		b.RMA { |b|
			b.Number @rma_number
		} unless @rma_number.blank?
		b.SpecialServices { |b|
			b.EMailNotification { |b|
				b.ShipAlertOptionalMessage @message
				b.Shipper { |b|
					b.ShipAlert @shipper_ship_alert ? 'true' : 'false'
					b.LanguageCode @shipper_language || 'EN' # FR also available
				}
				b.Recipient { |b|
					b.ShipAlert @recipient_ship_alert ? 'true' : 'false'
					b.LanguageCode @recipient_language || 'EN' # FR also available
				}
				b.Other { |b|
					b.tag! :"E-MailAddress", @other_email
					b.ShipAlert @other_ship_alert ? 'true' : 'false'
					b.LanguageCode @other_language || 'EN' # FR also available
				} unless @other_email.blank?
			}
		} unless @message.blank?
		b.Label { |b|
			b.Type @label_type || '2DCOMMON'
			b.ImageType @image_type || 'PNG'
		}
	}
	get_response @fedex_url

	begin  
		response = Hash.new       
		response[:tracking_number] = REXML::XPath.first(@response, "//FDXShipReply/Tracking/TrackingNumber").text
		response[:encoded_image] = REXML::XPath.first(@response, "//FDXShipReply/Labels/OutboundLabel").text
		response[:image] = Tempfile.new("shipping_label")
		response[:image].write Base64.decode64( response[:encoded_image] )
		response[:image].rewind
	rescue
		raise ShippingError, get_error
	end

	# allows for things like fedex.label.url
	def response.method_missing(name, *args)
		has_key?(name) ? self[name] : super
	end

	# don't allow people to edit the response
	response.freeze
end

#priceObject

Gets the list price the regular consumer would have to pay. Discount price is what the person with this particular account number will pay



12
13
14
15
16
17
# File 'lib/shipping/fedex.rb', line 12

def price
	get_price
	return REXML::XPath.first(@response, "//FDXRateReply/EstimatedCharges/ListCharges/NetCharge").text.to_f
rescue ShippingError
	raise ShippingError, get_error
end

#registerObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/shipping/fedex.rb', line 53

def register
	@required = [:name, :company, :phone, :email, :address, :city, :state, :zip]
	@required += [:fedex_account, :fedex_url]

	state = STATES.has_value?(@state.downcase) ? STATES.index(@state.downcase).upcase : @state.upcase rescue nil

	@data = String.new
	b = Builder::XmlMarkup.new :target => @data
	b.instruct!
	b.FDXSubscriptionRequest('xmlns:api' => 'http://www.fedex.com/fsmapi', 'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance', 'xsi:noNamespaceSchemaLocation' => 'FDXSubscriptionRequest.xsd') { |b|
		b.RequestHeader { |b|
			b.CustomerTransactionIdentifier @transaction_identifier if @transaction_identifier # optional
			b.AccountNumber @fedex_account
		}
		b.Contact { |b|
			b.PersonName @name
			b.CompanyName @company
			b.Department @department if @department
			b.PhoneNumber @phone.to_s.gsub(/[^\d]/,"")
			b.tag! :"E-MailAddress", @email
		}
		b.Address { |b|
			b.Line1 @address
			b.Line2 @address2 if @address2
			b.City @city
			b.StateOrProvinceCode state
			b.PostalCode @zip
			b.CountryCode @country || 'US'
		}
	}

	get_response @fedex_url

	return REXML::XPath.first(@response, "//FDXSubscriptionReply/MeterNumber").text
end

#return_labelObject

require ‘fileutils’

fedex = Shipping::FedEx.new :name => 'John Doe', ... , :sender_zip => 97202
label = fedex.email_label
puts label.url
puts label.tracking_number


239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
# File 'lib/shipping/fedex.rb', line 239

def return_label
	@required =  [:phone,        :email,        :address,        :city,        :state,        :zip        ]
	@required += [:sender_phone, :sender_email, :sender_address, :sender_city, :sender_state, :sender_zip ]
	@required += [:fedex_account, :fedex_url, :fedex_meter, :weight                                       ]

	@transaction_type ||= 'ship_ground'
	@weight = (@weight.to_f*10).round/10.0
	@declared_value = (@declared_value.to_f*100).round/100.0 unless @declared_value.blank?

	state = STATES.has_value?(@state.downcase) ? STATES.index(@state.downcase).upcase : @state.upcase rescue nil
	sender_state = STATES.has_value?(@sender_state.downcase) ? STATES.index(@sender_state.downcase).upcase : @sender_state.upcase rescue nil

	@data = String.new
	b = Builder::XmlMarkup.new :target => @data
	b.instruct!
	b.FDXEmailLabelRequest('xmlns:api' => 'http://www.fedex.com/fsmapi', 'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance', 'xsi:noNamespaceSchemaLocation' => 'FDXEmailLabelRequest.xsd') { |b|
		b.RequestHeader { |b|
			b.AccountNumber @fedex_account
			b.MeterNumber @fedex_meter
			b.CarrierCode TransactionTypes[@transaction_type][1]
		}
		b.URLExpirationDate((Time.now + 3600*24).strftime("%Y-%m-%d"))
		b.tag! :"URLNotificationE-MailAddress", @sender_email
		b.MerchantPhoneNumber @sender_phone.gsub(/[^\d]/,"")
		b.Service ServiceTypes[@service_type] || ServiceTypes['ground_service'] # default to ground service
		b.Packaging PackageTypes[@packaging_type] || 'YOURPACKAGING'
		b.WeightUnits @weight_units || 'LBS' # or KGS
		b.CurrencyCode @currency_code || 'USD'
		b.Origin { |b|
			b.Contact { |b|
				if @sender_name.to_s.size > 2
					b.PersonName @sender_name
					b.CompanyName @sender_company unless @sender_company.blank?
				elsif @sender_company.to_s.size > 2
					b.PersonName @sender_name unless @sender_name.blank?
					b.CompanyName @sender_company
				else
					raise ShippingError, "Either the sender_name or the sender_company value must be bigger than 2 characters."
				end
				b.Department @sender_department unless @sender_department.blank?
				b.PhoneNumber @sender_phone.gsub(/[^\d]/,"")
				b.PagerNumber @sender_pager.gsub(/[^\d]/,"") if @sender_pager.class == String
				b.FaxNumber @sender_fax.gsub(/[^\d]/,"") if @sender_fax.class == String
				b.tag! :"E-MailAddress", @sender_email
			}
			b.Address { |b|
				b.Line1 @sender_address
				b.Line2 @sender_address2 unless @sender_address2.blank?
				b.City @sender_city
				b.StateOrProvinceCode sender_state
				b.PostalCode @sender_zip
				b.CountryCode @sender_country || 'US'
			}
		}
		b.Destination { |b|
			b.Contact { |b|
				if @name.to_s.size > 2
					b.PersonName @name
					b.CompanyName @company unless @company.blank?
				elsif @company.to_s.size > 2
					b.PersonName @name unless @name.blank?
					b.CompanyName @company
				else
					raise ShippingError, "Either the name or the company value must be bigger than 2 characters."
				end
				b.Department @department unless @department.blank?
				b.PhoneNumber @phone.gsub(/[^\d]/,"")
				b.PagerNumber @pager.gsub(/[^\d]/,"") if @pager.class == String
				b.FaxNumber @fax.gsub(/[^\d]/,"") if @fax.class == String
				b.tag! :"E-MailAddress", @email
			}
			b.Address { |b|
				b.Line1 @address
				b.Line2 @address2 unless @address2.blank?
				b.City @city
				b.StateOrProvinceCode state
				b.PostalCode @zip
				b.CountryCode @country || 'US'
			}
		}
		
		b.Payment { |b|
			b.PayorType PaymentTypes[@pay_type] || 'SENDER'
			b.Payor { |b|
				b.AccountNumber @payor_account_number
				b.CountryCode @payor_country_code unless @payor_country_code.blank?
			} unless @payor_account_number.blank?
		}
		
		b.RMA { |b|
			b.Number @rma_number
		} unless @rma_number.blank?
		
		b.Package { |b|
			b.Weight @weight
			b.DeclaredValue @declared_value || '99.00'
			b.ReferenceInfo { |b|
				b.CustomerReference @customer_reference
			} unless @customer_reference.blank?
			b.ItemDescription @description || "Shipment"
		}

		b.SpecialServices { |b|
			b.EMailNotification { |b|
				b.ShipAlertOptionalMessage @message
				b.Shipper { |b|
					b.ShipAlert @shipper_ship_alert ? 'true' : 'false'
					b.LanguageCode @shipper_language || 'EN' # FR also available
				}
				b.Recipient { |b|
					b.ShipAlert @recipient_ship_alert ? 'true' : 'false'
					b.LanguageCode @recipient_language || 'EN' # FR also available
				}
				b.Other { |b|
					b.tag! :"E-MailAddress", @other_email
					b.ShipAlert @other_ship_alert ? 'true' : 'false'
					b.LanguageCode @other_language || 'EN' # FR also available
				} unless @other_email.blank?
			}
		} unless @message.blank?
	}

	get_response @fedex_url

	begin
		response = Hash.new
		response[:url]       = REXML::XPath.first(@response, "//FDXEmailLabelReply/URL").text
		response[:userid]    = REXML::XPath.first(@response, "//FDXEmailLabelReply/UserID").text
		response[:password]  = REXML::XPath.first(@response, "//FDXEmailLabelReply/Password").text
		response[:tracking_number] = REXML::XPath.first(@response, "//FDXEmailLabelReply/Package/TrackingNumber").text
	rescue
		raise ShippingError, get_error
	end

	# allows for things like fedex.label.url
	def response.method_missing(name, *args)
		has_key?(name) ? self[name] : super
	end

	# don't allow people to edit the response
	return response.freeze
end

#void(tracking_number) ⇒ Object

Raises:



382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
# File 'lib/shipping/fedex.rb', line 382

def void(tracking_number)     
	@required = [:fedex_account, :fedex_url, :fedex_meter]

	@transaction_type ||= 'ship_ground'

	@data = String.new
	b = Builder::XmlMarkup.new :target => @data
	b.instruct!
	b.FDXShipDeleteRequest('xmlns:api' => 'http://www.fedex.com/fsmapi', 'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance', 'xsi:noNamespaceSchemaLocation' => 'FDXShipDeleteRequest.xsd') { |b|
		b.RequestHeader { |b|
			b.AccountNumber @fedex_account
			b.MeterNumber @fedex_meter
			b.CarrierCode TransactionTypes[@transaction_type][1]
		}
		b.TrackingNumber tracking_number
	}
	
	get_response @fedex_url
	raise ShippingError, get_error if get_error
	return true
end