canadapost 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +56 -0
- data/Rakefile +1 -0
- data/canadapost.gemspec +25 -0
- data/lib/canadapost.rb +133 -0
- metadata +92 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 134aaf80be1b1760b5ff916e7610f3a68c54bc84
|
4
|
+
data.tar.gz: 6402272921f2a0246282ac313152a2f7fe3cda7e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 66f2bb34d2dd943be4334f38e69f11e65c46b4a15cd0d9cd63c16b62034ea3aee8fda4b838d1103edb57ff486607c3428eb6a47b555c56be84b9c99c6e80d1ad
|
7
|
+
data.tar.gz: 9f4bcfb2dc25989786e13442af59ed18d4940c0a1322f2d3c26f7780a904f9d4438009dd4e6994fbba822b2b920dde00802ce64809556a9c3a83bac77098281b
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Gordon B. Isnor
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
# Canadapost
|
2
|
+
|
3
|
+
A gem to facilitate Ruby on Rails interaction with Canada Post's REST API.
|
4
|
+
|
5
|
+
This is currently the most basic implementation that I needed for a particular project, and only has provisions for obtaining a list of shipping rates. I invite you to extend or improve as you see fit.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'canadapost'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install canadapost
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
canada_post = CanadaPost.new({
|
23
|
+
username: "xxx",
|
24
|
+
password: "xxx",
|
25
|
+
customer_number: 'xxx',
|
26
|
+
development: true (optional)
|
27
|
+
})
|
28
|
+
|
29
|
+
canada_post.origin_postal_code = "xxx"
|
30
|
+
canada_post.dimensions = dimension_hash
|
31
|
+
canada_post.customer = customer_hash
|
32
|
+
canada_post.get_rates
|
33
|
+
|
34
|
+
get_rates returns a hash of services, including:
|
35
|
+
- :service_name
|
36
|
+
- :service_code
|
37
|
+
- :price_details[:due]
|
38
|
+
- :service_link[:href]
|
39
|
+
- :service_standard[:expected_transit_time]
|
40
|
+
|
41
|
+
**customer hash:**
|
42
|
+
for us: { :'united-states' => { :'zip-code' => 'xxxxx' } }
|
43
|
+
for canada: { domestic: { :'postal-code' => 'xxxxxx' } }
|
44
|
+
for international: { international: { :'country-code' => 'xx' } }
|
45
|
+
|
46
|
+
**dimension hash**
|
47
|
+
{weight: x.x, height: x.x, length: x.x, width: x.x }
|
48
|
+
Weight is kilograms; height, length, width are centimetres
|
49
|
+
|
50
|
+
## Contributing
|
51
|
+
|
52
|
+
1. Fork it
|
53
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
54
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
55
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
56
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/canadapost.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "canadapost"
|
7
|
+
spec.version = "0.0.1"
|
8
|
+
spec.authors = ["Gordon B. Isnor"]
|
9
|
+
spec.email = ["gordonbisnor@gmail.com"]
|
10
|
+
spec.description = %q{A basic gem to interface with the Canada POST REST API}
|
11
|
+
spec.summary = %q{A basic gem to interface with the Canada POST REST API}
|
12
|
+
spec.homepage = "http://www.github.com/gordonbisnor/canadapost"
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files`.split($/)
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_dependency('httparty')
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
23
|
+
spec.add_development_dependency "rake"
|
24
|
+
end
|
25
|
+
|
data/lib/canadapost.rb
ADDED
@@ -0,0 +1,133 @@
|
|
1
|
+
# CanadaPost gem for use with Canada Post REST API
|
2
|
+
class Canadapost
|
3
|
+
attr_accessor :username,
|
4
|
+
:password,
|
5
|
+
:development,
|
6
|
+
:auth,
|
7
|
+
:customer_number,
|
8
|
+
:response,
|
9
|
+
:content_type_header,
|
10
|
+
:accept_header,
|
11
|
+
|
12
|
+
# Get Rates
|
13
|
+
:dimensions,
|
14
|
+
:customer,
|
15
|
+
:origin_postal_code
|
16
|
+
|
17
|
+
# @param [Hash] options
|
18
|
+
# @option options [String] :username your API key username
|
19
|
+
# @option options [String] :password your API key password
|
20
|
+
# @option options [Boolean] :development to specify development mode
|
21
|
+
# @option options [String] :customer_number your Canada Post customer account number
|
22
|
+
def initialize(params = {})
|
23
|
+
@username = params[:username]
|
24
|
+
@password = params[:password]
|
25
|
+
@development = params[:development]
|
26
|
+
@customer_number = params[:customer_number]
|
27
|
+
@auth = { username: username, password: password }
|
28
|
+
end
|
29
|
+
|
30
|
+
# @return [Hash] Hash with symbolized keys
|
31
|
+
def results
|
32
|
+
resp = if response.parsed_response.is_a? String
|
33
|
+
Hash.from_xml(response.parsed_response.gsub("\n", ""))
|
34
|
+
else
|
35
|
+
response.parsed_response
|
36
|
+
end
|
37
|
+
hash = resp.deep_symbolize_keys!
|
38
|
+
end
|
39
|
+
private :results
|
40
|
+
|
41
|
+
# @param xml [String] A string containing the XML data
|
42
|
+
# @param url [String] The Canadda POST API endpoint
|
43
|
+
def do_request(xml, url)
|
44
|
+
self.response = HTTParty.post(url,
|
45
|
+
body: xml,
|
46
|
+
basic_auth: auth,
|
47
|
+
headers: {
|
48
|
+
'Content-type' => content_type_header,
|
49
|
+
'Accept' => accept_header
|
50
|
+
}
|
51
|
+
)
|
52
|
+
end
|
53
|
+
private :do_request
|
54
|
+
|
55
|
+
# @return String
|
56
|
+
def base_url
|
57
|
+
if development
|
58
|
+
"https://ct.soa-gw.canadapost.ca"
|
59
|
+
else
|
60
|
+
"https://soa-gw.canadapost.ca"
|
61
|
+
end
|
62
|
+
end
|
63
|
+
private :base_url
|
64
|
+
|
65
|
+
|
66
|
+
#
|
67
|
+
# Get Rates
|
68
|
+
#
|
69
|
+
|
70
|
+
# Get Rates from Canada POST REST API
|
71
|
+
# @param [Hash] options
|
72
|
+
# @option options [String] :original_postal_code the postal code the parcel is being sent from
|
73
|
+
# @option options [Hash] :customer a hash of customer details either { :'united-states' => { :'zip-code' => '' } }; { domestic: { :'postal-code' => '' } }; or { international: { :'country-code' => '' } }
|
74
|
+
# @option options [Hash] :dimensions a hash of parcel dimensions, containing height, width and length in CM and weight in KG
|
75
|
+
# @return Hash a hash of services
|
76
|
+
#
|
77
|
+
def get_rates(params = {})
|
78
|
+
self.origin_postal_code = params[:origin_postal_code].gsub(/\s+/, "") if params[:origin_postal_code].present?
|
79
|
+
self.customer = params[:customer] if params[:customer].present?
|
80
|
+
self.dimensions = params[:dimensions] if params[:dimensions].present?
|
81
|
+
|
82
|
+
self.content_type_header = self.accept_header = 'application/vnd.cpc.ship.rate-v2+xml'
|
83
|
+
|
84
|
+
begin
|
85
|
+
do_request(get_rates_xml, get_rates_url)
|
86
|
+
services = results[:price_quotes][:price_quote]
|
87
|
+
services = services.map(&:deep_symbolize_keys!)
|
88
|
+
return services
|
89
|
+
rescue => e
|
90
|
+
Rails.logger.info "ERROR :#{e}"
|
91
|
+
end
|
92
|
+
return services
|
93
|
+
end
|
94
|
+
|
95
|
+
#
|
96
|
+
# @return String
|
97
|
+
#
|
98
|
+
def get_rates_url
|
99
|
+
"#{base_url}/rs/ship/price"
|
100
|
+
end
|
101
|
+
private :get_rates_url
|
102
|
+
|
103
|
+
#
|
104
|
+
# @return String
|
105
|
+
#
|
106
|
+
def get_rates_xml
|
107
|
+
Nokogiri::XML::Builder.new { |xml|
|
108
|
+
xml.send(:"mailing-scenario", xmlns: "http://www.canadapost.ca/ws/ship/rate-v2") {
|
109
|
+
xml.send(:"customer-number", customer_number)
|
110
|
+
xml.send(:"origin-postal-code", origin_postal_code)
|
111
|
+
xml.send(:"parcel-characteristics") {
|
112
|
+
xml.weight dimensions[:weight]
|
113
|
+
xml.dimensions {
|
114
|
+
xml.height dimensions[:height].round(1)
|
115
|
+
xml.width dimensions[:width].round(1)
|
116
|
+
xml.length dimensions[:length].round(1)
|
117
|
+
}
|
118
|
+
}
|
119
|
+
xml.destination {
|
120
|
+
customer.each_pair do |key, value|
|
121
|
+
xml.send(key) {
|
122
|
+
value.each_pair { |key,value|
|
123
|
+
xml.send(key, value)
|
124
|
+
}
|
125
|
+
}
|
126
|
+
end
|
127
|
+
}
|
128
|
+
}
|
129
|
+
}.to_xml
|
130
|
+
end
|
131
|
+
private :get_rates_xml
|
132
|
+
|
133
|
+
end
|
metadata
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: canadapost
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Gordon B. Isnor
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-12-21 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: httparty
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.3'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.3'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: A basic gem to interface with the Canada POST REST API
|
56
|
+
email:
|
57
|
+
- gordonbisnor@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- Gemfile
|
63
|
+
- LICENSE
|
64
|
+
- README.md
|
65
|
+
- Rakefile
|
66
|
+
- canadapost.gemspec
|
67
|
+
- lib/canadapost.rb
|
68
|
+
homepage: http://www.github.com/gordonbisnor/canadapost
|
69
|
+
licenses:
|
70
|
+
- MIT
|
71
|
+
metadata: {}
|
72
|
+
post_install_message:
|
73
|
+
rdoc_options: []
|
74
|
+
require_paths:
|
75
|
+
- lib
|
76
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - '>='
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0'
|
81
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
requirements: []
|
87
|
+
rubyforge_project:
|
88
|
+
rubygems_version: 2.0.3
|
89
|
+
signing_key:
|
90
|
+
specification_version: 4
|
91
|
+
summary: A basic gem to interface with the Canada POST REST API
|
92
|
+
test_files: []
|