shipping-scale 0.1.0 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3356c36762733c31023ea62e9652f0b12b0b92f6
4
- data.tar.gz: 1b2c906fcad03b078f56f88e82dc5ff48a22057f
3
+ metadata.gz: 6895d8b360e1db8c6ff8de0dd9f4a61577368125
4
+ data.tar.gz: 68d68bcb2e4beefb59232f33bf14d6bcdbd44ed4
5
5
  SHA512:
6
- metadata.gz: de91ad6c1681d17285898c92848d738c35bd16d55809551e56d230028c93411abe6065379e75bb7235533e102df575cbed8db133bad94ad3b06755ae68acffc9
7
- data.tar.gz: 412db0928def8f25de3e52272e552087a525f0062b1e8601b75521ddc2a6add145e9e7598498cb69970d83268d332b475c1908aa6aecfb77ffb9262fa216402f
6
+ metadata.gz: 009e37f216d3a1cad3ac8053954e7abce48de48430999f900ee784af235aa446d687ab82641a7d86ee212c3bcd13a06dc6cfa2393a7cfefd5d00453fdb0671f9
7
+ data.tar.gz: 79b2a3c2b6a10a0dbcbfde3ed57b1d2202a52e669e22793d20fcc5ddf20ac4d57833f9c942318fcfddda9e71f7d6b20b6f2f77cbbd078ee07eee68e1689de651
data/README.md CHANGED
@@ -37,14 +37,12 @@ end
37
37
 
38
38
  ## Usage
39
39
 
40
- If you have all of the necessary information for the package you want to ship, using the gem is as simples as creating an instance of `ShipingScale::Package` and using `#get_price!`. Other details are also returned with this method.
40
+ If you have all of the necessary information for the package you want to ship, using the gem is as simple as creating an instance of `ShipingScale::Package` and using `#get_price!`. Other details are also returned with this method.
41
41
 
42
42
  ```ruby
43
43
  package_options = {
44
- weight: 1,
45
- length: 3,
46
- width: 2,
47
- height: 3,
44
+ pounds: 1,
45
+ ounces: 8, # or weight: 1.5
48
46
  zip_origin: "66204",
49
47
  zip_destination: "63501"
50
48
  }
@@ -59,7 +57,7 @@ package.price # => 15
59
57
  # for other details sent back from the request
60
58
  package.details # => { zip_origination: "66204", zip_destination: "63501", pounds: "1", ... }
61
59
  ```
62
-
60
+ Currently, all rates retreived from the USPS API are for 2-day Priority Mail only.
63
61
 
64
62
  ## License
65
63
 
Binary file
@@ -1,16 +1,20 @@
1
1
  require "shipping_scale/version"
2
2
  require "builder"
3
3
  require "nokogiri"
4
- require "snakecase_string"
4
+ require "string_module"
5
+ require "typhoeus"
6
+
5
7
 
6
8
  module ShippingScale
7
9
 
8
10
  autoload :Client, "shipping_scale/client"
9
11
  autoload :Configuration, "shipping_scale/configuration"
12
+ autoload :Error, "shipping_scale/error"
13
+ autoload :Package, "shipping_scale/packaging/package"
14
+ autoload :Packaging, "shipping_scale/packaging/packaging"
10
15
  autoload :Request, "shipping_scale/request"
11
16
  autoload :Response, "shipping_scale/response"
12
- autoload :Error, "shipping_scale/error"
13
- autoload :Package, "shipping_scale/package"
17
+ autoload :Shipment, "shipping_scale/packaging/shipment"
14
18
 
15
19
  class << self
16
20
  attr_writer :config
@@ -1,15 +1,13 @@
1
- require "typhoeus"
2
-
3
1
  module ShippingScale
4
2
  class Client
5
3
  def request(request)
6
- server = server(request)
4
+ server = "http://production.shippingapis.com/ShippingAPI.dll"
7
5
 
8
6
  response = Typhoeus::Request.get(server, {
9
7
  timeout: ShippingScale.config.timeout,
10
8
  params: {
11
9
  "API" => request.api,
12
- "XML" => request.build
10
+ "XML" => request.build.body.clean_xml
13
11
  }
14
12
  })
15
13
 
@@ -1,5 +1,5 @@
1
1
  module ShippingScale
2
- class Configuration < Struct.new(:user_id, :timeout, :testing, :zip_origin, :zip_destination)
2
+ class Configuration < Struct.new(:user_id, :timeout, :testing, :zip_origination, :zip_destination)
3
3
  def initialize
4
4
  self.timeout = 5
5
5
  self.testing = false
@@ -0,0 +1,63 @@
1
+ module ShippingScale
2
+ class Package < Packaging
3
+
4
+ class << self
5
+ def shipment(details)
6
+ packages = details.map { |package| new(package) }
7
+ ShippingScale::Shipment.new(packages)
8
+ end
9
+ end
10
+
11
+ attr_reader(
12
+ :weight,
13
+ :pounds,
14
+ :ounces,
15
+ :length,
16
+ :width,
17
+ :height,
18
+ :zip_origination,
19
+ :zip_destination,
20
+ )
21
+
22
+ def initialize(**options)
23
+ @weight = options[:weight]
24
+ @pounds = (options[:pounds]) ? options[:pounds] : get_pounds
25
+ @ounces = (options[:ounces]) ? options[:ounces] : get_ounces
26
+ @zip_origination = (options[:zip_origination]) ? options[:zip_origination] : ShippingScale.config.zip_origination
27
+ @zip_destination = (options[:zip_destination]) ? options[:zip_destination] : ShippingScale.config.zip_destination
28
+
29
+ @packages = [self]
30
+ end
31
+
32
+ def build_xml(package)
33
+ attrs.each do |k, v|
34
+ package.tag!(k.to_s.upper_camelcase, v)
35
+ end
36
+ end
37
+ #TODO set methods to determin service, container, and size
38
+
39
+ private
40
+
41
+ def get_pounds
42
+ @weight.floor.to_i
43
+ end
44
+
45
+ def get_ounces
46
+ frac = @weight - @weight.floor
47
+ ((frac * 16) / 10).ceil.to_i
48
+ end
49
+
50
+ def attrs
51
+ {
52
+ service: "All",
53
+ zip_origination: @zip_origination,
54
+ zip_destination: @zip_destination,
55
+ pounds: @pounds,
56
+ ounces: @ounces,
57
+ container: "VARIABLE",
58
+ size: "Regular",
59
+ machinable: "true"
60
+ }
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,18 @@
1
+ module ShippingScale
2
+ class Packaging
3
+ attr_accessor :response, :packages
4
+
5
+ def get_price!(options = {})
6
+ ShippingScale::Request.config(options)
7
+ @response = ShippingScale::Request.new(packages: packages).send!
8
+ end
9
+
10
+ def price
11
+ response.price
12
+ end
13
+
14
+ def details
15
+ response.details
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,11 @@
1
+ module ShippingScale
2
+ class Shipment < Packaging
3
+ def initialize(packages)
4
+ @packages = packages
5
+ end
6
+
7
+ def prices
8
+ response.prices
9
+ end
10
+ end
11
+ end
@@ -33,7 +33,8 @@ module ShippingScale
33
33
  xml.tag!(self.class.tag, USERID: ShippingScale.config.user_id) do |req|
34
34
  req.tag!("Revision", "2")
35
35
  packages_to_xml(req)
36
- end
36
+ end
37
+ xml
37
38
  end
38
39
 
39
40
  def send!
@@ -17,17 +17,36 @@ module ShippingScale
17
17
  attr_reader :xml
18
18
 
19
19
  def details
20
- details = {}
20
+ details = {postage: []}
21
21
 
22
22
  xml.search("Package").children.each do |node|
23
- details[node.name.snakecase.to_sym] = node.text
23
+ if node.name == "Postage"
24
+ postage = {}
25
+ node.children.each do |child|
26
+ postage[child.name.to_s.snakecase.to_sym] = child.text
27
+ end
28
+ details[:postage].push(postage)
29
+ else
30
+ details[node.name.to_s.snakecase.to_sym] = node.text
31
+ end
24
32
  end
25
33
 
26
34
  return details
27
35
  end
28
36
 
29
37
  def price
30
- xml.search("Package").children.search("Postage").text.to_f
38
+ xml.search("Postage[CLASSID='1']").search("Rate").inject(0) { |sum, t| sum + t.text.to_f }
39
+ end
40
+
41
+ def prices
42
+ prices = {}
43
+ xml.search("Package").each do |package|
44
+ key = package.attribute("ID").value.to_s
45
+ value = package.search("Postage[CLASSID='1']").search("Rate").text.to_f
46
+ prices[key] = value
47
+ end
48
+
49
+ prices
31
50
  end
32
51
  end
33
52
  end
@@ -1,3 +1,3 @@
1
1
  module ShippingScale
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.2"
3
3
  end
@@ -0,0 +1,46 @@
1
+ class String
2
+ def snakecase
3
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
4
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
5
+ tr('-', '_').
6
+ gsub(/\s/, '_').
7
+ gsub(/__+/, '_').
8
+ downcase
9
+ end
10
+
11
+ def camelcase(*separators)
12
+ case separators.first
13
+ when Symbol, TrueClass, FalseClass, NilClass
14
+ first_letter = separators.shift
15
+ end
16
+
17
+ separators = ['_', '\s'] if separators.empty?
18
+
19
+ str = self.dup
20
+
21
+ separators.each do |s|
22
+ str = str.gsub(/(?:#{s}+)([a-z])/){ $1.upcase }
23
+ end
24
+
25
+ case first_letter
26
+ when :upper, true
27
+ str = str.gsub(/(\A|\s)([a-z])/){ $1 + $2.upcase }
28
+ when :lower, false
29
+ str = str.gsub(/(\A|\s)([A-Z])/){ $1 + $2.downcase }
30
+ end
31
+
32
+ str
33
+ end
34
+
35
+ def upper_camelcase(*separators)
36
+ camelcase(:upper, *separators)
37
+ end
38
+
39
+ def lower_camelcase(*separators)
40
+ camelcase(:lower, *separators)
41
+ end
42
+
43
+ def clean_xml
44
+ self.gsub(/<body\/>/, "")
45
+ end
46
+ end
Binary file
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shipping-scale
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Josh Reinhardt
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-01-11 00:00:00.000000000 Z
11
+ date: 2017-01-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: builder
@@ -138,14 +138,18 @@ files:
138
138
  - bin/setup
139
139
  - lib/.DS_Store
140
140
  - lib/shipping_scale.rb
141
+ - lib/shipping_scale/.DS_Store
141
142
  - lib/shipping_scale/client.rb
142
143
  - lib/shipping_scale/configuration.rb
143
144
  - lib/shipping_scale/error.rb
144
- - lib/shipping_scale/package.rb
145
+ - lib/shipping_scale/packaging/package.rb
146
+ - lib/shipping_scale/packaging/packaging.rb
147
+ - lib/shipping_scale/packaging/shipment.rb
145
148
  - lib/shipping_scale/request.rb
146
149
  - lib/shipping_scale/response.rb
147
150
  - lib/shipping_scale/version.rb
148
- - lib/snakecase_string.rb
151
+ - lib/string_module.rb
152
+ - shipping-scale-0.1.0.gem
149
153
  - shipping-scale.gemspec
150
154
  - tasks/rspec.rake
151
155
  homepage: https://github.com/jereinhardt/shipping-scale
@@ -1,40 +0,0 @@
1
- module ShippingScale
2
- class Package
3
- def initialize(**options)
4
- @weight = options[:weight]
5
- @pounds = options[:pounds]
6
- @ounces = options[:ounces]
7
- @length = options[:length]
8
- @width = options[:width]
9
- @height = options[:height]
10
- @zip_origin = (options[:zip_origin]) ? options[:zip_origin] : ShippingScale.config.zip_origin
11
- @zip_destination = (options[:zip_destination]) ? options[:zip_destination] : ShippingScale.config.zip_destination
12
-
13
- @attrs = options
14
- end
15
-
16
- attr_accessor :response
17
-
18
- def build_xml(package)
19
- package.tag!("Service", "All")
20
- package.tag!("Container", "VARAIBLE")
21
- package.tag!("Size", "REGULAR")
22
- @attrs.each { |k, v| package.tag!(k.to_s, v) }
23
- end
24
-
25
- def get_price!(options = {})
26
- ShippingScale::Request.config(options)
27
- @response = ShippingScale::Request.new(packages: [self]).send!
28
- end
29
-
30
- def price
31
- response.price
32
- end
33
-
34
- def details
35
- response.details
36
- end
37
-
38
- #TODO set methods to determin service, container, and size
39
- end
40
- end
@@ -1,10 +0,0 @@
1
- class String
2
- def snakecase
3
- gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
4
- gsub(/([a-z\d])([A-Z])/,'\1_\2').
5
- tr('-', '_').
6
- gsub(/\s/, '_').
7
- gsub(/__+/, '_').
8
- downcase
9
- end
10
- end