shipping-calc 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Manifest.txt CHANGED
@@ -3,5 +3,4 @@ Manifest.txt
3
3
  README.txt
4
4
  Rakefile
5
5
  lib/shipping_calc.rb
6
- lib/shipping_calc/base.rb
7
6
  lib/shipping_calc/dhl.rb
data/README.txt CHANGED
@@ -1,6 +1,7 @@
1
1
  = ShippingCalc
2
2
 
3
3
  * http://github.com/febuiles/shipping_calc/
4
+ * mailto:federico.builes@gmail.com
4
5
 
5
6
  == DESCRIPTION:
6
7
 
@@ -12,6 +13,9 @@ Shipping Calculator written in Ruby to get quick quotes from the major carriers
12
13
 
13
14
  == SYNOPSIS:
14
15
 
16
+ require 'rubygems'
17
+ require 'shipping_calc'
18
+
15
19
  include ShippingCalc
16
20
 
17
21
  opts = {
@@ -40,6 +44,19 @@ Shipping Calculator written in Ruby to get quick quotes from the major carriers
40
44
 
41
45
  * sudo gem install shipping_calc
42
46
 
47
+ == TEST
48
+
49
+ To run the DHL tests you'll need to have a .dhl_info.yml file in your home directory with your auth info like this:
50
+ ~/.dhl_info.yml
51
+
52
+ api_user: your_user
53
+ api_password: your_password
54
+ shipping_key: your_key
55
+ account_num: your_accnt_num
56
+
57
+ This is necessary only for the tests and it'll be ignored for the actual
58
+ usage of the the library.
59
+
43
60
  == LICENSE:
44
61
 
45
62
  Copyright (c) 2008 Federico Builes
data/lib/shipping_calc.rb CHANGED
@@ -6,15 +6,19 @@ require 'net/http'
6
6
  require 'net/https'
7
7
  require 'uri'
8
8
 
9
- require 'shipping_calc/base'
10
9
  require 'shipping_calc/dhl'
11
10
 
12
11
  module ShippingCalc
13
12
  class ShippingCalcError < StandardError
14
13
  end
14
+ VERSION = "0.0.2"
15
15
 
16
- VERSION = "0.0.1"
17
-
16
+ US_STATES = ['AK', 'AL', 'AR', 'AZ', 'CA', 'CO', 'CT', 'DC',
17
+ 'DE', 'FL', 'GA', 'HI', 'IA', 'ID', 'IL', 'IN',
18
+ 'KS', 'KY', 'LA', 'MA', 'MD', 'ME', 'MI', 'MN',
19
+ 'MO', 'MS', 'MT', 'NC', 'ND', 'NE', 'NH', 'NJ',
20
+ 'NM', 'NV', 'NY', 'OH', 'OK', 'OR', 'PA', 'RI',
21
+ 'SC', 'SD', 'TN', 'TX', 'UT', 'VA', 'VT', 'WA',
22
+ 'WI', 'WV']
18
23
  end
19
-
20
24
 
@@ -12,8 +12,7 @@ module ShippingCalc
12
12
  # against their live platform tests. The test bed should be enough to get
13
13
  # simple calculations.
14
14
  # Currently, only shipments made inside the US are available.
15
- class DHL < Base
16
-
15
+ class DHL
17
16
 
18
17
  # Obtains an estimate quote from the DHL site.
19
18
  # <tt>params</tt> is a hash with all the settings for the shipment. They are:
@@ -33,7 +32,7 @@ module ShippingCalc
33
32
  def quote(params)
34
33
  @xml = xml = Document.new
35
34
  xml << XMLDecl.new("1.0' encoding='UTF-8")
36
-
35
+ raise ShippingCalcError.new("Missing shipping parameters") unless params.keys.length == 10
37
36
  auth(params[:api_user], params[:api_password])
38
37
  rate_estimate(params)
39
38
  request
@@ -64,8 +63,7 @@ module ShippingCalc
64
63
  # After having the auth message ready, we create the RateEstimate request.
65
64
  # shipping_key: API shipping key, provided by DHL.
66
65
  # account_num: Account number, provided by DHL.
67
- # date: Date for the shipping in format YYYY-MM-DD (defaults to
68
- # Time.now).
66
+ # date: Date for the shipping. Must be a Ruby "Time" object.
69
67
  # service_code: Service code defined in Rate Estimate Specification
70
68
  # (E, N, S, G). 1030 and SAT are not supported yet. Defaults to G
71
69
  # (ground service).
@@ -110,7 +108,7 @@ module ShippingCalc
110
108
  detail << type << t_code
111
109
 
112
110
  weight = Element.new "Weight"
113
- weight.text = weight(params[:weight])
111
+ weight.text = weight(params[:weight], params[:shipment_code])
114
112
  shipment << detail << weight
115
113
 
116
114
  billing = Element.new "Billing"
@@ -161,12 +159,16 @@ module ShippingCalc
161
159
  if result == "Shipment estimate successful."
162
160
  doc.elements["//Shipment/EstimateDetail/RateEstimate/TotalChargeEstimate"].text.to_f
163
161
  else
164
- raise ShippingCalcError.new("Error calculating shipping costs: + #{result}")
162
+ raise ShippingCalcError.new(doc.to_s)
165
163
  end
166
164
  end
167
165
 
168
166
  def date(date)
169
- date =~ /\d{4}-\d{2}-\d{2}/ ? date : Time.now.strftime("%Y-%m-%d")
167
+ if date.strftime("%A") == "Sunday"
168
+ (date + 86400).strftime("%Y-%m-%d") # DHL doesn't ship on Sundays, add 1 day.
169
+ else
170
+ date.strftime("%Y-%m-%d")
171
+ end
170
172
  end
171
173
 
172
174
  def shipment_code(code)
@@ -177,8 +179,12 @@ module ShippingCalc
177
179
  ["E", "N", "S", "G"].include?(code) ? code : "G"
178
180
  end
179
181
 
180
- def weight(w)
181
- (w > 0 && w <= 150) ? w.to_s : (raise ShippingCalcError.new("Invalid weight - Must be between 1 and 150 lbs."))
182
+ def weight(w, type)
183
+ if type == "L"
184
+ "0"
185
+ else
186
+ (w > 0 && w <= 150) ? w.to_s : (raise ShippingCalcError.new("Invalid weight - Must be between 1 and 150 lbs."))
187
+ end
182
188
  end
183
189
 
184
190
  def state(s)
@@ -186,10 +192,13 @@ module ShippingCalc
186
192
  end
187
193
 
188
194
  def valid_state?(s)
189
- US_STATES.include?(s)
195
+ ShippingCalc::US_STATES.include?(s)
190
196
  end
191
197
 
192
198
  def zip_code(code)
199
+ if code.class != Fixnum
200
+ raise ShippingCalcError.new("Zip Code must be a number. Perhaps you are using a string?")
201
+ end
193
202
  code.to_s =~ /\d{5}/ ? code.to_s : (raise ShippingCalcError.new("Invalid zip code for recipient"))
194
203
  end
195
204
 
@@ -0,0 +1,4 @@
1
+ require 'test/unit'
2
+ require 'shipping_calc'
3
+
4
+ require 'dhl/dhl_test'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shipping-calc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Federico Builes
@@ -38,7 +38,6 @@ files:
38
38
  - README.txt
39
39
  - Rakefile
40
40
  - lib/shipping_calc.rb
41
- - lib/shipping_calc/base.rb
42
41
  - lib/shipping_calc/dhl.rb
43
42
  has_rdoc: true
44
43
  homepage: http://github.com/febuiles/shipping_calc/
@@ -67,5 +66,5 @@ rubygems_version: 1.0.1
67
66
  signing_key:
68
67
  specification_version: 2
69
68
  summary: Shipping Calculator written in Ruby to get quick quotes from the major carriers (UPS, DHL, FedEX).
70
- test_files: []
71
-
69
+ test_files:
70
+ - test/test_helper.rb
@@ -1,13 +0,0 @@
1
- module ShippingCalc
2
-
3
- class Base
4
- US_STATES = ['AK', 'AL', 'AR', 'AZ', 'CA', 'CO', 'CT', 'DC',
5
- 'DE', 'FL', 'GA', 'HI', 'IA', 'ID', 'IL', 'IN',
6
- 'KS', 'KY', 'LA', 'MA', 'MD', 'ME', 'MI', 'MN',
7
- 'MO', 'MS', 'MT', 'NC', 'ND', 'NE', 'NH', 'NJ',
8
- 'NM', 'NV', 'NY', 'OH', 'OK', 'OR', 'PA', 'RI',
9
- 'SC', 'SD', 'TN', 'TX', 'UT', 'VA', 'VT', 'WA',
10
- 'WI', 'WV']
11
- end
12
-
13
- end