shipping-scale 0.1.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3356c36762733c31023ea62e9652f0b12b0b92f6
4
+ data.tar.gz: 1b2c906fcad03b078f56f88e82dc5ff48a22057f
5
+ SHA512:
6
+ metadata.gz: de91ad6c1681d17285898c92848d738c35bd16d55809551e56d230028c93411abe6065379e75bb7235533e102df575cbed8db133bad94ad3b06755ae68acffc9
7
+ data.tar.gz: 412db0928def8f25de3e52272e552087a525f0062b1e8601b75521ddc2a6add145e9e7598498cb69970d83268d332b475c1908aa6aecfb77ffb9262fa216402f
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /.env
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.13.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in shipping-scale.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Josh Reinhardt
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,70 @@
1
+ # ShippingScale
2
+
3
+ ShippingScale is a simple interface with the USPS Price Calculator API. With it, you can quickly and easily calculate postage prices for packages.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'shipping-scale'
11
+
12
+ # or from the github repository
13
+ gem 'shipping-scale', :git => 'https://github.com/jereinhardt/shipping-scale.git'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install shipping-scale
23
+
24
+ ## Setup
25
+
26
+ In order to properly configure your interface, you must first have a registered developer account with the US Postal Service.
27
+
28
+ Add an initializer to your application and configure it to use your API USER ID
29
+
30
+ ```ruby
31
+ ShippingScale.configure do |config|
32
+ config.user_id = ENV["USPS_USER_ID"]
33
+ config.timeout = 5
34
+ config.testing = false
35
+ end
36
+ ```
37
+
38
+ ## Usage
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.
41
+
42
+ ```ruby
43
+ package_options = {
44
+ weight: 1,
45
+ length: 3,
46
+ width: 2,
47
+ height: 3,
48
+ zip_origin: "66204",
49
+ zip_destination: "63501"
50
+ }
51
+
52
+ package = ShippingScale::Package.new(package_options)
53
+
54
+ package.get_price!
55
+
56
+ # for the floating point value of postage
57
+ package.price # => 15
58
+
59
+ # for other details sent back from the request
60
+ package.details # => { zip_origination: "66204", zip_destination: "63501", pounds: "1", ... }
61
+ ```
62
+
63
+
64
+ ## License
65
+
66
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
67
+
68
+ Bug reports and pull requests are welcome on GitHub at https://github.com/jereinhardt/shipping-scale.
69
+
70
+
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ Dir.glob("tasks/**/*.rake").each(&method(:import))
4
+
5
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,31 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "shipping_scale"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
15
+
16
+ require "shipping_scale"
17
+
18
+ # ShippingScale.config
19
+ # ShippingScale::Request.config
20
+
21
+ # package1_options = { weight: 1.5, length: 3, width: 2, height: 3, zip_origin: "66204", zip_destination: "63501" }
22
+ # package2_options = { weight: 2.5, length: 1, width: 2, height: 5, zip_origin: "63501", zip_destination: "66204" }
23
+ # package_1 = ShippingScale::Package.new(package1_options)
24
+ # package_2 = ShippingScale::Package.new(package2_options)
25
+ # packages = [package_1, package_2]
26
+
27
+ # request = ShippingScale.new(packages: packages)
28
+
29
+ # xml = request.build
30
+
31
+ # puts xml
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/lib/.DS_Store ADDED
Binary file
@@ -0,0 +1,34 @@
1
+ require "shipping_scale/version"
2
+ require "builder"
3
+ require "nokogiri"
4
+ require "snakecase_string"
5
+
6
+ module ShippingScale
7
+
8
+ autoload :Client, "shipping_scale/client"
9
+ autoload :Configuration, "shipping_scale/configuration"
10
+ autoload :Request, "shipping_scale/request"
11
+ autoload :Response, "shipping_scale/response"
12
+ autoload :Error, "shipping_scale/error"
13
+ autoload :Package, "shipping_scale/package"
14
+
15
+ class << self
16
+ attr_writer :config
17
+
18
+ def client
19
+ @client ||= Client.new
20
+ end
21
+
22
+ def testing=(val)
23
+ config.testing = val
24
+ end
25
+
26
+ def config
27
+ @config ||= Configuration.new
28
+ end
29
+
30
+ def configure(&block)
31
+ block.call(self.config)
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,43 @@
1
+ require "typhoeus"
2
+
3
+ module ShippingScale
4
+ class Client
5
+ def request(request)
6
+ server = server(request)
7
+
8
+ response = Typhoeus::Request.get(server, {
9
+ timeout: ShippingScale.config.timeout,
10
+ params: {
11
+ "API" => request.api,
12
+ "XML" => request.build
13
+ }
14
+ })
15
+
16
+ xml = Nokogiri::XML.parse(response.body)
17
+
18
+ if ((error = xml.search('Error')).any?)
19
+ why = error.search("Description").children.first
20
+ code = error.search("Number").children.first.to_s
21
+ source = error.search("Source").children.first
22
+ raise ShippingScale::Error.for_code(code).new(why, code, source)
23
+ end
24
+
25
+ ShippingScale::Response.parse(xml)
26
+ end
27
+
28
+ def testing?
29
+ ShippingScale.config.testing?
30
+ end
31
+
32
+ def server(request)
33
+ case
34
+ when request.secure?
35
+ "https://secure.shippingapis.com/ShippingAPI.dll"
36
+ when testing?
37
+ "http://testing.shippingapis.com/ShippingAPI.dll"
38
+ else
39
+ "http://production.shippingapis.com/ShippingAPI.dll"
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,11 @@
1
+ module ShippingScale
2
+ class Configuration < Struct.new(:user_id, :timeout, :testing, :zip_origin, :zip_destination)
3
+ def initialize
4
+ self.timeout = 5
5
+ self.testing = false
6
+ self.user_id = (ENV.fetch("USPS_USER_ID")) ? ENV.fetch("USPS_USER_ID") : nil
7
+ end
8
+
9
+ alias :testing? :testing
10
+ end
11
+ end
@@ -0,0 +1,36 @@
1
+ module ShippingScale
2
+ class Error < StandardError
3
+ attr_reader :code, :source
4
+
5
+ def initialize(message, code, source)
6
+ super(message)
7
+
8
+ @code = code
9
+ @source = source
10
+ end
11
+
12
+ class << self
13
+ def for_code(code)
14
+ case code
15
+ when "-2147219498" ; AuthorizationError
16
+ when "-2147219100" ; MissingZipOriginError
17
+ when "-2147219099" ; MissingZipDestinationError
18
+ when "-2147219497" ; InvalidZipDestinationError
19
+ when "-2147219498" ; InvalidZipOriginError
20
+ when "-2147219097" ; MissingWeightError
21
+ when "-2147219098" ; MissingWeightError
22
+ else ; Error
23
+ end
24
+ end
25
+ end
26
+ end
27
+
28
+ class AuthorizationError < Error; end
29
+
30
+ class ValidationError < Error; end
31
+ class InvalidZipOriginError < ValidationError; end
32
+ class InvalidZipDestinationError < ValidationError; end
33
+ class MissingZipOriginError < ValidationError; end
34
+ class MissingZipDestinationError < ValidationError; end
35
+ class MissingWeightError < ValidationError; end
36
+ end
@@ -0,0 +1,40 @@
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
@@ -0,0 +1,57 @@
1
+ module ShippingScale
2
+ class Request
3
+ class << self
4
+ attr_reader :api, :tag, :secure
5
+
6
+ def config(options = {})
7
+ defaults = {api: "RateV4", tag: "RateV4Request"}
8
+ options = defaults.merge(options)
9
+
10
+ @api = options[:api]
11
+ @tag = options[:tag]
12
+ @secure = !!options[:secure]
13
+ end
14
+ end
15
+
16
+ def initialize(options={})
17
+ @response = options[:response]
18
+ @packages = options[:packages]
19
+ end
20
+
21
+ attr_accessor :response
22
+ attr_reader :packages
23
+
24
+ def secure?
25
+ !!self.class.secure
26
+ end
27
+
28
+ def api
29
+ self.class.api
30
+ end
31
+
32
+ def build
33
+ xml.tag!(self.class.tag, USERID: ShippingScale.config.user_id) do |req|
34
+ req.tag!("Revision", "2")
35
+ packages_to_xml(req)
36
+ end
37
+ end
38
+
39
+ def send!
40
+ ShippingScale.client.request(self)
41
+ end
42
+
43
+ private
44
+
45
+ def packages_to_xml(xml)
46
+ i = 1
47
+ packages.each do |package|
48
+ xml.tag!("Package", ID: i) { |pac| package.build_xml(pac) }
49
+ i += 1
50
+ end
51
+ end
52
+
53
+ def xml
54
+ @_xml ||= Builder::XmlMarkup.new(indent: 0)
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,33 @@
1
+ module ShippingScale
2
+ class Response
3
+ attr_accessor :raw
4
+
5
+ class << self
6
+ def parse(xml)
7
+ response = self.new(xml)
8
+ response.raw = xml
9
+ response
10
+ end
11
+ end
12
+
13
+ def initialize(xml)
14
+ @xml = xml
15
+ end
16
+
17
+ attr_reader :xml
18
+
19
+ def details
20
+ details = {}
21
+
22
+ xml.search("Package").children.each do |node|
23
+ details[node.name.snakecase.to_sym] = node.text
24
+ end
25
+
26
+ return details
27
+ end
28
+
29
+ def price
30
+ xml.search("Package").children.search("Postage").text.to_f
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,3 @@
1
+ module ShippingScale
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,10 @@
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
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'shipping_scale/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "shipping-scale"
8
+ spec.version = ShippingScale::VERSION
9
+ spec.authors = ["Josh Reinhardt"]
10
+ spec.email = ["joshua.e.reinhardt@gmail.com"]
11
+
12
+ spec.summary = %q{A simple Ruby API wrapper for the USPS price calculator API}
13
+ spec.homepage = "https://github.com/jereinhardt/shipping-scale"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_dependency "builder", "~> 2.0"
24
+ spec.add_runtime_dependency 'nokogiri', '~> 1.4', '>= 1.4.1'
25
+ spec.add_runtime_dependency 'typhoeus', '~> 0.1', '>= 0.1.18'
26
+
27
+ spec.add_development_dependency "bundler", "~> 1.13"
28
+ spec.add_development_dependency "rake", "~> 10.0"
29
+ spec.add_development_dependency "rspec", "~> 3.0"
30
+ spec.add_development_dependency 'dotenv', '~> 0'
31
+ end
data/tasks/rspec.rake ADDED
@@ -0,0 +1,3 @@
1
+ require "rspec/core/rake_task"
2
+
3
+ RSpec::Core::RakeTask.new(:spec)
metadata ADDED
@@ -0,0 +1,175 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: shipping-scale
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Josh Reinhardt
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-01-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: builder
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: nokogiri
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.4'
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: 1.4.1
37
+ type: :runtime
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - "~>"
42
+ - !ruby/object:Gem::Version
43
+ version: '1.4'
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 1.4.1
47
+ - !ruby/object:Gem::Dependency
48
+ name: typhoeus
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '0.1'
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: 0.1.18
57
+ type: :runtime
58
+ prerelease: false
59
+ version_requirements: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - "~>"
62
+ - !ruby/object:Gem::Version
63
+ version: '0.1'
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: 0.1.18
67
+ - !ruby/object:Gem::Dependency
68
+ name: bundler
69
+ requirement: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - "~>"
72
+ - !ruby/object:Gem::Version
73
+ version: '1.13'
74
+ type: :development
75
+ prerelease: false
76
+ version_requirements: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - "~>"
79
+ - !ruby/object:Gem::Version
80
+ version: '1.13'
81
+ - !ruby/object:Gem::Dependency
82
+ name: rake
83
+ requirement: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - "~>"
86
+ - !ruby/object:Gem::Version
87
+ version: '10.0'
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - "~>"
93
+ - !ruby/object:Gem::Version
94
+ version: '10.0'
95
+ - !ruby/object:Gem::Dependency
96
+ name: rspec
97
+ requirement: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - "~>"
100
+ - !ruby/object:Gem::Version
101
+ version: '3.0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - "~>"
107
+ - !ruby/object:Gem::Version
108
+ version: '3.0'
109
+ - !ruby/object:Gem::Dependency
110
+ name: dotenv
111
+ requirement: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - "~>"
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ type: :development
117
+ prerelease: false
118
+ version_requirements: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - "~>"
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ description:
124
+ email:
125
+ - joshua.e.reinhardt@gmail.com
126
+ executables: []
127
+ extensions: []
128
+ extra_rdoc_files: []
129
+ files:
130
+ - ".gitignore"
131
+ - ".rspec"
132
+ - ".travis.yml"
133
+ - Gemfile
134
+ - LICENSE.txt
135
+ - README.md
136
+ - Rakefile
137
+ - bin/console
138
+ - bin/setup
139
+ - lib/.DS_Store
140
+ - lib/shipping_scale.rb
141
+ - lib/shipping_scale/client.rb
142
+ - lib/shipping_scale/configuration.rb
143
+ - lib/shipping_scale/error.rb
144
+ - lib/shipping_scale/package.rb
145
+ - lib/shipping_scale/request.rb
146
+ - lib/shipping_scale/response.rb
147
+ - lib/shipping_scale/version.rb
148
+ - lib/snakecase_string.rb
149
+ - shipping-scale.gemspec
150
+ - tasks/rspec.rake
151
+ homepage: https://github.com/jereinhardt/shipping-scale
152
+ licenses:
153
+ - MIT
154
+ metadata: {}
155
+ post_install_message:
156
+ rdoc_options: []
157
+ require_paths:
158
+ - lib
159
+ required_ruby_version: !ruby/object:Gem::Requirement
160
+ requirements:
161
+ - - ">="
162
+ - !ruby/object:Gem::Version
163
+ version: '0'
164
+ required_rubygems_version: !ruby/object:Gem::Requirement
165
+ requirements:
166
+ - - ">="
167
+ - !ruby/object:Gem::Version
168
+ version: '0'
169
+ requirements: []
170
+ rubyforge_project:
171
+ rubygems_version: 2.6.8
172
+ signing_key:
173
+ specification_version: 4
174
+ summary: A simple Ruby API wrapper for the USPS price calculator API
175
+ test_files: []