shipping 1.5.1 → 1.6.0

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -44,6 +44,7 @@ Alternately, you can instantiate the base class and then see both UPS and FedEx
44
44
 
45
45
  == Authors
46
46
  * Lucas Carlson (mailto:lucas@rufy.com)
47
+ * Jimmy Baker (mailto:jimmyebaker@gmail.com)
47
48
  * Noah Zoschke (mailto:noah@bitscribe.net)
48
49
 
49
50
  This library is released under the terms of the GNU LGPL.
@@ -3,16 +3,16 @@
3
3
  # License:: LGPL
4
4
 
5
5
  module Shipping
6
- VERSION = "1.5.1"
6
+ VERSION = "1.6.0"
7
7
 
8
8
  class ShippingError < StandardError; end
9
9
  class ShippingRequiredFieldError < StandardError; end
10
10
 
11
11
  class Base
12
- attr_reader :data, :response, :plain_response, :required
12
+ attr_reader :data, :response, :plain_response, :required, :services
13
13
 
14
14
  attr_writer :ups_license_number, :ups_shipper_number, :ups_user, :ups_password, :ups_url, :ups_tool
15
- attr_writer :fedex_account, :fedex_meter, :fedex_url
15
+ attr_writer :fedex_account, :fedex_meter, :fedex_url, :fedex_package_weight_limit_in_lbs
16
16
 
17
17
  attr_accessor :name, :phone, :company, :email, :address, :address2, :city, :state, :zip, :country
18
18
  attr_accessor :sender_name, :sender_phone, :sender_company, :sender_email, :sender_address, :sender_city, :sender_state, :sender_zip, :sender_country
@@ -20,6 +20,8 @@ module Shipping
20
20
  attr_accessor :weight, :weight_units, :insured_value, :declared_value, :transaction_type, :description
21
21
  attr_accessor :measure_units, :measure_length, :measure_width, :measure_height
22
22
  attr_accessor :package_total, :packaging_type, :service_type
23
+
24
+ attr_accessor :price, :discount_price, :eta, :time_in_transit
23
25
 
24
26
  attr_accessor :ship_date, :dropoff_type, :pay_type, :currency_code, :image_type, :label_type
25
27
 
@@ -28,12 +30,22 @@ module Shipping
28
30
  YAML.load(File.open(prefs)).each {|pref, value| eval("@#{pref} = #{value.inspect}")} if File.exists?(prefs)
29
31
 
30
32
  @required = Array.new
33
+ @services = Array.new
31
34
 
32
35
  # include all provided data
33
36
  options.each do |method, value|
34
37
  instance_variable_set("@#{method}", value)
35
38
  end
36
-
39
+
40
+ case options[:carrier]
41
+ when "fedex"
42
+ fedex
43
+ when "ups"
44
+ ups
45
+ when nil
46
+ else
47
+ raise ShippingError, "unknown service"
48
+ end
37
49
  end
38
50
 
39
51
  # Initializes an instance of Shipping::FedEx with the same instance variables as the base object
@@ -45,6 +57,8 @@ module Shipping
45
57
  def ups
46
58
  Shipping::UPS.new prepare_vars
47
59
  end
60
+
61
+
48
62
 
49
63
  def self.state_from_zip(zip)
50
64
  zip = zip.to_i
@@ -160,5 +174,15 @@ module Shipping
160
174
  end
161
175
 
162
176
  STATES = {"al" => "alabama", "ne" => "nebraska", "ak" => "alaska", "nv" => "nevada", "az" => "arizona", "nh" => "new hampshire", "ar" => "arkansas", "nj" => "new jersey", "ca" => "california", "nm" => "new mexico", "co" => "colorado", "ny" => "new york", "ct" => "connecticut", "nc" => "north carolina", "de" => "delaware", "nd" => "north dakota", "fl" => "florida", "oh" => "ohio", "ga" => "georgia", "ok" => "oklahoma", "hi" => "hawaii", "or" => "oregon", "id" => "idaho", "pa" => "pennsylvania", "il" => "illinois", "pr" => "puerto rico", "in" => "indiana", "ri" => "rhode island", "ia" => "iowa", "sc" => "south carolina", "ks" => "kansas", "sd" => "south dakota", "ky" => "kentucky", "tn" => "tennessee", "la" => "louisiana", "tx" => "texas", "me" => "maine", "ut" => "utah", "md" => "maryland", "vt" => "vermont", "ma" => "massachusetts", "va" => "virginia", "mi" => "michigan", "wa" => "washington", "mn" => "minnesota", "dc" => "district of columbia", "ms" => "mississippi", "wv" => "west virginia", "mo" => "missouri", "wi" => "wisconsin", "mt" => "montana", "wy" => "wyoming"}
177
+
178
+ def self.initialize_for_fedex_service(xml)
179
+ s = Shipping::Base.new
180
+ s.fedex
181
+ s.eta = REXML::XPath.first(xml, "DeliveryDate").text unless REXML::XPath.match(xml, "DeliveryDate").empty?
182
+ s.service_type = REXML::XPath.first(xml, "Service").text
183
+ s.discount_price = REXML::XPath.first(xml, "EstimatedCharges/DiscountedCharges/BaseCharge").text
184
+ s.price = REXML::XPath.first(xml, "EstimatedCharges/DiscountedCharges/NetCharge").text
185
+ return s
186
+ end
163
187
  end
164
188
  end
@@ -68,7 +68,7 @@ module Shipping
68
68
  b.PersonName @name
69
69
  b.CompanyName @company
70
70
  b.Department @department if @department
71
- b.PhoneNumber @phone.gsub(/[^\d]/,"")
71
+ b.PhoneNumber @phone.to_s.gsub(/[^\d]/,"")
72
72
  b.tag! :"E-MailAddress", @email
73
73
  }
74
74
  b.Address { |b|
@@ -401,6 +401,14 @@ module Shipping
401
401
  return true
402
402
  end
403
403
 
404
+ def available_services(force=nil)
405
+ if @services.empty? || force
406
+ get_available_services
407
+ else
408
+ @services
409
+ end
410
+ end
411
+
404
412
  private
405
413
 
406
414
  def get_price #:nodoc:
@@ -456,6 +464,60 @@ module Shipping
456
464
  return "Error #{code}: #{message}"
457
465
  end
458
466
 
467
+ def get_available_services
468
+ @required = [:zip, :sender_zip, :weight]
469
+ @required += [:fedex_account, :fedex_meter, :fedex_url]
470
+
471
+ @transaction_type = 'rate_services'
472
+ @weight = (@weight.to_f*10).round/10.0
473
+
474
+ # Ground first
475
+ @services = []
476
+ rate_available_services_request('FDXG')
477
+ rate_available_services_request('FDXE')
478
+
479
+ return @services
480
+ end
481
+
482
+ def rate_available_services_request(carrier_code)
483
+ @data = String.new
484
+ b = Builder::XmlMarkup.new(:target => @data)
485
+ b.instruct!
486
+ b.FDXRateAvailableServicesRequest('xmlns:api' => 'http://www.fedex.com/fsmapi', 'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance', 'xsi:noNamespaceSchemaLocation' => 'FDXRateAvailableServicesRequest.xsd') { |b|
487
+ b.RequestHeader { |b|
488
+ b.AccountNumber @fedex_account
489
+ b.MeterNumber @fedex_meter
490
+ b.CarrierCode carrier_code
491
+ }
492
+ b.ShipDate @ship_date unless @ship_date.blank?
493
+ b.DropoffType DropoffTypes[@dropoff_type] || DropoffTypes['regular_pickup']
494
+ b.Packaging PackageTypes[@packaging_type] || PackageTypes['your_packaging']
495
+ b.WeightUnits @weight_units || 'LBS'
496
+ b.Weight @weight || '1.0'
497
+ b.ListRate 'false'
498
+ b.OriginAddress { |b|
499
+ b.StateOrProvince @sender_state
500
+ b.PostalCode @sender_zip
501
+ b.CountryCode @sender_country_code || 'US'
502
+ }
503
+ b.DestinationAddress { |b|
504
+ b.StateOrProvince @state
505
+ b.PostalCode @zip
506
+ b.CountryCode @country_code || 'US'
507
+ }
508
+ b.Payment { |b|
509
+ b.PayorType PaymentTypes[@pay_type] || PaymentTypes['sender']
510
+ }
511
+ b.PackageCount @package_total || 1
512
+ }
513
+ get_response @fedex_url
514
+
515
+ REXML::XPath.each(@response, "//Entry") { |el|
516
+ @services << Shipping::Base.initialize_for_fedex_service(el)
517
+ }
518
+
519
+ end
520
+
459
521
  # The following type hashes are to allow cross-api data retrieval
460
522
 
461
523
  ServiceTypes = {
@@ -64,6 +64,23 @@ class FedExTest < Test::Unit::TestCase
64
64
  assert @ship.void(@label.tracking_number)
65
65
  end
66
66
 
67
+ def test_available_services
68
+ @ship.name = "Package Receiver"
69
+ @ship.phone = "555-555-5555"
70
+ @ship.email = "lucas@rufy.com"
71
+ @ship.address = "Some address"
72
+ @ship.city = "Portland"
73
+
74
+ @ship.sender_name = "Package Sender"
75
+ @ship.sender_phone = "333-333-3333"
76
+ @ship.sender_email = "john@doe.com"
77
+ @ship.sender_address = "Ok old address"
78
+ @ship.sender_city = "New York"
79
+
80
+ assert_nothing_raised { @available_services = @ship.available_services }
81
+ assert_not_nil @available_services
82
+ end
83
+
67
84
  def test_void_fail
68
85
  # made up tracking number
69
86
  assert_raise(Shipping::ShippingError) { @ship.void('470012923511666') }
metadata CHANGED
@@ -1,68 +1,75 @@
1
1
  --- !ruby/object:Gem::Specification
2
- rubygems_version: 0.8.11
3
- specification_version: 1
4
2
  name: shipping
5
3
  version: !ruby/object:Gem::Version
6
- version: 1.5.1
7
- date: 2007-07-10 00:00:00 -07:00
8
- summary: A general shipping module to find out the shipping prices via UPS or FedEx.
9
- require_paths:
10
- - lib
11
- email: lucas@rufy.com
12
- homepage: http://shipping.rufy.com/
13
- rubyforge_project:
14
- description: A general shipping module to find out the shipping prices via UPS or FedEx.
15
- autorequire: shipping
16
- default_executable:
17
- bindir: bin
18
- has_rdoc: true
19
- required_ruby_version: !ruby/object:Gem::Version::Requirement
20
- requirements:
21
- - - ">"
22
- - !ruby/object:Gem::Version
23
- version: 0.0.0
24
- version:
4
+ version: 1.6.0
25
5
  platform: ruby
26
- signing_key:
27
- cert_chain:
28
6
  authors:
29
7
  - Lucas Carlson
8
+ autorequire: shipping
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-10 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: builder
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.2.0
24
+ version:
25
+ description: " A general shipping module to find out the shipping prices via UPS or FedEx.\n"
26
+ email: lucas@rufy.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files: []
32
+
30
33
  files:
31
34
  - lib/extensions.rb
32
- - lib/shipping
33
- - lib/shipping.rb
34
35
  - lib/shipping/base.rb
35
36
  - lib/shipping/fedex.rb
36
37
  - lib/shipping/ups.rb
37
- - test/base
38
- - test/fedex
39
- - test/test_helper.rb
40
- - test/ups
38
+ - lib/shipping.rb
41
39
  - test/base/base_test.rb
42
40
  - test/fedex/fedex_test.rb
41
+ - test/test_helper.rb
43
42
  - test/ups/ups_test.rb
44
43
  - LICENSE
45
44
  - Rakefile
46
45
  - README
47
- test_files: []
46
+ has_rdoc: true
47
+ homepage: http://shipping.rufy.com/
48
+ licenses: []
48
49
 
50
+ post_install_message:
49
51
  rdoc_options: []
50
52
 
51
- extra_rdoc_files: []
52
-
53
- executables: []
54
-
55
- extensions: []
56
-
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: "0"
60
+ version:
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: "0"
66
+ version:
57
67
  requirements:
58
68
  - An xml-builder library.
59
- dependencies:
60
- - !ruby/object:Gem::Dependency
61
- name: builder
62
- version_requirement:
63
- version_requirements: !ruby/object:Gem::Version::Requirement
64
- requirements:
65
- - - ">="
66
- - !ruby/object:Gem::Version
67
- version: 1.2.0
68
- version:
69
+ rubyforge_project:
70
+ rubygems_version: 1.3.5
71
+ signing_key:
72
+ specification_version: 3
73
+ summary: A general shipping module to find out the shipping prices via UPS or FedEx.
74
+ test_files: []
75
+