canada_post 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.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Tsokurov Alex <me@ximik.net>
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,59 @@
1
+ = Canada Post
2
+
3
+ Canada Post PHP Class will communicates with Canada Post Server and gets the shipping estimate. Its basic
4
+ and can be extended.
5
+
6
+
7
+ == How to Use
8
+
9
+ As per Canada Post's specification, make sure Port 30000 is opened.
10
+
11
+ At first require the gem.
12
+
13
+ require 'canada_post'
14
+
15
+ cPost = CanadaPost.new
16
+
17
+ Set your manufacturer CPCID, postal code by calling the function setManufacturer
18
+
19
+ merchant = {
20
+ :merchantCPCID => 'CPC_DEMO_XML',
21
+ :fromPostalCode => 'L1JK9',
22
+ :turnAroundTime => 24,
23
+ :itemsPrice => 14
24
+ }
25
+ cPost.setMerchant merchant
26
+
27
+ Then set the Customer address in the format shown below, again in associative array format.
28
+
29
+ *Note*: city and provOrState are optional. Only Postal Code and country is required.
30
+
31
+ customer = {
32
+ :city => 'Brampton',
33
+ :provOrState => 'Ontario,
34
+ :country => 'CA',
35
+ :postalCode => 'L1JK9'
36
+ }
37
+ cPost.setCustomer customer
38
+
39
+ Then, add the products needed to be shipped (add as many as you want), in the format shown below:
40
+
41
+ item = {
42
+ :quantity => 1,
43
+ :weight => 2,
44
+ :length => 3,
45
+ :width => 1,
46
+ :height => 8,
47
+ :description => 'some Description about Product'
48
+ }
49
+ cPost.addItem item
50
+
51
+ Then, invoke the method below (returns XML format of details from Canada Post Server):
52
+
53
+ response = cPost.getRates
54
+
55
+ == License
56
+
57
+ The gem is based on {CanadaPost PHP class}[http://github.com/itsalif/CanadaPost].
58
+
59
+ The script has been released under MIT License.
@@ -0,0 +1,18 @@
1
+ # encoding: UTF-8
2
+
3
+ Gem::Specification.new do |s|
4
+ s.platform = Gem::Platform::RUBY
5
+ s.name = 'canada_post'
6
+ s.version = '0.1.0'
7
+ s.author = 'Alex Tsokurov'
8
+ s.email = 'me@ximik.net'
9
+ s.summary = 'A Simple Ruby Class that communicates with Canada Post Server and provides a shipping estimate.'
10
+ s.homepage = 'http://github.com/Ximik/CanadaPost'
11
+
12
+ s.files = `git ls-files`.split("\n")
13
+
14
+ s.add_dependency 'builder'
15
+
16
+ s.has_rdoc = true
17
+ s.extra_rdoc_files << 'README.rdoc'
18
+ end
@@ -0,0 +1,102 @@
1
+ # :main: README.rdoc
2
+
3
+ require 'net/http'
4
+ require 'rexml/document'
5
+ require 'builder'
6
+
7
+ class CanadaPost
8
+
9
+ def initialize :nodoc:
10
+ @items = []
11
+ end
12
+
13
+ #
14
+ # This method sets the merchant information
15
+ #
16
+ def setMerchant(merchant)
17
+ @merchantInfo = merchant;
18
+ end
19
+
20
+ #
21
+ # This method sets the customer information
22
+ #
23
+ def setCustomer(customer)
24
+ @customerInfo = customer;
25
+ end
26
+
27
+ #
28
+ # This method allows you to add items to be shipped
29
+ #
30
+ def addItem(item)
31
+ @items << item;
32
+ end
33
+
34
+ #
35
+ # The main method. Makes request and parse the response
36
+ #
37
+ def getRates
38
+ url = URI.parse 'http://sellonline.canadapost.ca:30000/'
39
+ request = Net::HTTP::Post.new(url.path)
40
+ request.content_type = 'application/x-www-form-urlencoded'
41
+ request.body = prepareXML
42
+ response = Net::HTTP.start(url.host, url.port) {|http| http.request(request)}
43
+ xml = REXML::Document.new(response.body).root
44
+ if error = xml.elements['error']
45
+ error = error.elements['statusMessage'].text
46
+ raise StandardError.new(error)
47
+ end
48
+ xml = xml.elements['ratesAndServicesResponse']
49
+ result = {}
50
+ result[:products] = []
51
+ xml.elements.each('product') do |p|
52
+ product = {}
53
+ p.elements.each do |t|
54
+ product[t.name.to_sym] = t.text
55
+ end
56
+ result[:products] << product
57
+ end
58
+ result[:options] = {}
59
+ xml.elements['shippingOptions'].elements.each do |t|
60
+ result[:options][t.name.to_sym] = t.text
61
+ end
62
+ return result
63
+ end
64
+
65
+ private
66
+
67
+
68
+ #
69
+ # This method prepares the XML to be send to Canada Posts's Server.
70
+ #
71
+ def prepareXML
72
+ xml = Builder::XmlMarkup.new
73
+ xml.instruct!
74
+ xml.eparcel do
75
+ xml.language 'en'
76
+ xml.ratesAndServicesRequest do
77
+ buildTags(xml, [:merchantCPCID, :fromPostalCode, :turnAroundTime, :itemsPrice], @merchantInfo])
78
+ xml.lineItems do
79
+ @items.each do |item|
80
+ xml.item do
81
+ buildTags(xml, [:quantity, :weight, :length, :width, :height, :description], item)
82
+ end
83
+ xml.readyToShip if item[:readyToShip]
84
+ end
85
+ end
86
+ buildTags(xml, [:city, :provOrState, :country, :postalCode], @customerInfo])
87
+ end
88
+ end
89
+ return xml.target!
90
+ end
91
+
92
+ #
93
+ # This method just build tags in right order.
94
+ #
95
+ def buildTags(xml, tags, hash)
96
+ tags.each do |t|
97
+ text = hash[t]
98
+ xml.method_missing t, text if text
99
+ end
100
+ end
101
+
102
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: canada_post
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Alex Tsokurov
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-03-23 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: builder
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ description:
35
+ email: me@ximik.net
36
+ executables: []
37
+
38
+ extensions: []
39
+
40
+ extra_rdoc_files:
41
+ - README.rdoc
42
+ files:
43
+ - MIT-LICENSE
44
+ - README.rdoc
45
+ - canada_post.gemspec
46
+ - lib/canada_post.rb
47
+ homepage: http://github.com/Ximik/CanadaPost
48
+ licenses: []
49
+
50
+ post_install_message:
51
+ rdoc_options: []
52
+
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ hash: 3
61
+ segments:
62
+ - 0
63
+ version: "0"
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ hash: 3
70
+ segments:
71
+ - 0
72
+ version: "0"
73
+ requirements: []
74
+
75
+ rubyforge_project:
76
+ rubygems_version: 1.8.15
77
+ signing_key:
78
+ specification_version: 3
79
+ summary: A Simple Ruby Class that communicates with Canada Post Server and provides a shipping estimate.
80
+ test_files: []
81
+