pbshipping 1.0.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: febf1d66b2a57a7ac60318cf1cdd61481592cd65
4
+ data.tar.gz: 1cfe22e324612b25cd6458affe073fea73db418d
5
+ SHA512:
6
+ metadata.gz: 7d28b3263a39ad532070a258fb546baa8a87ffca109d07d236fded03baf915a56ae681d701a29fcd6b4b61a8f24eddeee528db8d7d01294a7a1088861bd102c0
7
+ data.tar.gz: ea1f7d18cf0235a0b57d2dce0032898193975394a455a31e24b6e3f9d37f1388ca4fa2c43445f98d5c9c403482cb8594f0b727ae9167f8b6c2b67fd8f6531efa
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2016 Pitney Bowes Inc.
2
+
3
+ The Shipping API for Client Library (Ruby) is licensed under the following license:
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this
6
+ software and associated documentation files (the "Software"), to deal in the Software
7
+ without restriction, including without limitation the rights to use, copy, modify,
8
+ merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
9
+ permit persons to whom the Software is furnished to do so, subject to the following
10
+ conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all copies
13
+ or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
16
+ INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
17
+ PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
18
+ FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19
+ OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20
+ DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,185 @@
1
+ # Pitney Bowes Shipping Client Library for Ruby
2
+ [![gem version](https://badge.fury.io/rb/pbshipping.svg)](https://badge.fury.io/rb/pbshipping)
3
+
4
+ The Pitney Bowes Shipping Services APIs provide easy access to United States
5
+ Postage Service shipping and tracking services that can be easily integrated
6
+ into any web application such as online shopping carts, e-commerce sites or
7
+ multi-carrier applications.
8
+
9
+ This package provides a wrapper layer allowing easy access to the Pitney Bowes
10
+ shipping REST APIs in the Ruby programming language.
11
+
12
+ You would need a Pitney Bowers developer account for testing and using the API
13
+ and the client library.
14
+
15
+ For more informaton please visit the
16
+ [Pitney Bowes developer portal](http://developer.pitneybowes.com).
17
+
18
+ ## Installation
19
+
20
+ You can clone or download the source from Github repository (https://github.com/pitneybowes/pitneybowes-shipping-api-ruby) or
21
+ install as [Ruby gem)](https://rubygems.org/).
22
+
23
+ ## Dependencies
24
+
25
+ You would need to install Ruby rest-client (https://rubygems.org/gems/rest-client/), version 1.8 or later.
26
+
27
+ ## Registration
28
+
29
+ Make sure you have signed up a developer account on the
30
+ [Pitney Bowes developer portal](http://developer.pitneybowes.com).
31
+
32
+ Once you have successfully registered for a developer account, sign in and
33
+ you can access your API test key (`API key` and `API secret`) from the *Dashboard*
34
+ page.
35
+
36
+ You can also access your `Developer ID` (e.g.: 12345678) from the *My Account*
37
+ page.
38
+
39
+ By default, your developer account is configured to work with individual
40
+ merchant account payment model. In this caes, each merchant signs up for
41
+ a Pitney Bowes shipment account directly and their funds are directly
42
+ managed by Pitney Bowes.
43
+
44
+ For testing purpose, you can create a merchant account
45
+ at https://developer.pbshippingmerchant.pitneybowes.com/home?developerID={DeveloperID}.
46
+ Make sure you replace the placeholder in the URL with your own developer ID.
47
+
48
+ You will use the `email address` for registering the merchant account for
49
+ identifying the corresponding mercchant in the shipping API call.
50
+
51
+ ## Understanding Configuration
52
+
53
+ The client library provides a configuration object (*lib/pbshipping.rb*) for
54
+ managing configuraton for environment using the Shipping APIs. The values
55
+ for each parameter can be readily accessed and modified programmatically:
56
+
57
+ ```
58
+ module PBShipping
59
+
60
+ @configuration = {
61
+ :sandbox => "https://api-sandbox.pitneybowes.com",
62
+ :production => "https://api.pitneybowes.com",
63
+ :is_production => false,
64
+ :default_api_version => "/v1",
65
+ :overwrite_api_version => {
66
+ "post/developers/.../merchants/registration" => "/v2",
67
+ "get/ledger/developers/.../transactions/reports" => "/v2"
68
+ }
69
+ }
70
+ . . . . .
71
+ ```
72
+ * *sandbox* and *production* refer to the URL pointing at the testing (sandbox)
73
+ and production server supporting the endpoints;
74
+ * *is_production* controls if the application should run in sandbox or
75
+ production mode;
76
+ * *default_api_version* defines the default API version to be used in general
77
+ except for cases where explict overrides are specified;
78
+ * *override_api_version* is a set (Ruby Hash) of API version
79
+ overrides for specific API calls; each entry defines the particular version
80
+ to be used for the corresponding API.
81
+
82
+ #### Choosing API version
83
+
84
+ You may need to use different version number for diferent Shipping APIs.
85
+ This can be accomplished by customizing the *defaul_api_version* and
86
+ *override_api_version* entries in the *Configuration.params* structure.
87
+
88
+ To specify the version number of a particular call type:
89
+ * Identify the *signature* for the API call; the signature mimics the relative
90
+ path of the API and can be found in the comment headers in the resource files
91
+ (*account.rb, address.rb, carrier.rb, developer.rb, manifest.rb, shipmente.rb, tracking.rb*).
92
+ * Add an entry to *override_api_version* in the format
93
+ *<api signature>: <verson string>*
94
+
95
+
96
+ ## Running the Tutorials
97
+
98
+ A tutorial *tutorial.rb* is provided to demonstrate how to use the different class and
99
+ methods available through the client library to consume the shipping services.
100
+
101
+ *Note: if you clone or download the source from Github and run the tutorial example
102
+ directly, make sure you include the lib directory/folder in your Ruby library path (RUBYLIB).*
103
+
104
+ ```
105
+ ruby tutorial.rb
106
+ ```
107
+
108
+ You would need to set your authentication (key and secret),
109
+ developer (ID), and merchant (email) credentials either through environment
110
+ variables or command line arguments.
111
+
112
+ ### Setting through Environment Variables
113
+
114
+ The following four varaibles should be set:
115
+ * PBSHIPPING_KEY: your assigned API key
116
+ * PBSHIPPING_SECRET: your assigned API secret
117
+ * PBSHIPPING_DEVID: your Developer ID
118
+ * PBSHIPPING_MERCHANT: email address for the regsitered merchant account
119
+
120
+ ```
121
+ export PBSHIPPING_KEY=<api key>
122
+ export PBSHIPPING_SECRET=<api secret>
123
+ export PBSHIPPING_DEVID=<developer id>
124
+ export PBSHIPPING_MERCHANT=<merchant email address>
125
+ ```
126
+
127
+ ### Setting through Command Line Arguments
128
+
129
+ Use the command line long options *--key*, *--secret*, *--devid*, and *--merchant*:
130
+
131
+ ```
132
+ ruby tutorial_client_library.rb --key=<your key> --secret=<your secret>
133
+ --devid==<developer id>
134
+ --merchant=<merchant email>
135
+ ```
136
+
137
+ ## Running Test
138
+
139
+ The test suite can be found under the *test* directory and can be run as follows:
140
+
141
+ ```
142
+ ruby ts_all_tests.rb
143
+ ```
144
+
145
+ Similar to the tutorial, you have to set the authentication, developer, and
146
+ merchant settings through environment variables to run the test.
147
+
148
+ ## Documentation
149
+
150
+ The Pitney Bowes Shipping Services API (REST) documentation can be found at the
151
+ [Pitney Bowes developer portal](http://developer.pitneybowes.com).
152
+
153
+ Developers are also encouraged to use the tutorial example and source code
154
+ comment blocks for information on the wrapper layer interface and mapping to
155
+ the underlying REST shipping APIs.
156
+
157
+ ## Attribution Notice
158
+
159
+ This product contains Rest-client Ruby, version number 1.8, which is licensed
160
+ under the MIT license. The license can be downloaded from https://opensource.org/licenses/MIT.
161
+
162
+ The source code for this software is available from http://www.rubydoc.info/gems/rest-client/1.8.0.
163
+
164
+ ## License
165
+
166
+ Copyright 2016 Pitney Bowes Inc.
167
+
168
+ The Shipping API for Client Library (Ruby) is licensed under the following license:
169
+
170
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this
171
+ software and associated documentation files (the "Software"), to deal in the Software
172
+ without restriction, including without limitation the rights to use, copy, modify,
173
+ merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
174
+ permit persons to whom the Software is furnished to do so, subject to the following
175
+ conditions:
176
+
177
+ The above copyright notice and this permission notice shall be included in all copies
178
+ or substantial portions of the Software.
179
+
180
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
181
+ INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
182
+ PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
183
+ FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
184
+ OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
185
+ DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,173 @@
1
+ #
2
+ # Copyright 2016 Pitney Bowes Inc.
3
+ #
4
+ # Licensed under the MIT License (the "License"); you may not use this file
5
+ # except in compliance with the License. You may obtain a copy of the License
6
+ # in the LICENSE file or at
7
+ # https://opensource.org/licenses/MIT
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11
+ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ #
15
+ # File: pbshipping.rb
16
+ # Description: top level shipping api client library source including
17
+ # configuration facilitiies
18
+ #
19
+
20
+ require 'rest-client'
21
+ require 'json'
22
+ require 'base64'
23
+ require 'bigdecimal'
24
+
25
+ require_relative 'pbshipping/error.rb'
26
+ require_relative 'pbshipping/api_object.rb'
27
+ require_relative 'pbshipping/api_resource.rb'
28
+ require_relative 'pbshipping/shipping_api_resource.rb'
29
+ require_relative 'pbshipping/account.rb'
30
+ require_relative 'pbshipping/address.rb'
31
+ require_relative 'pbshipping/authentication.rb'
32
+ require_relative 'pbshipping/carrier.rb'
33
+ require_relative 'pbshipping/country.rb'
34
+ require_relative 'pbshipping/customs.rb'
35
+ require_relative 'pbshipping/developer.rb'
36
+ require_relative 'pbshipping/manifest.rb'
37
+ require_relative 'pbshipping/merchant.rb'
38
+ require_relative 'pbshipping/parcel.rb'
39
+ require_relative 'pbshipping/rate.rb'
40
+ require_relative 'pbshipping/scandetails.rb'
41
+ require_relative 'pbshipping/shipment.rb'
42
+ require_relative 'pbshipping/tracking.rb'
43
+ require_relative 'pbshipping/transactiondetails.rb'
44
+
45
+ module PBShipping
46
+
47
+ @configuration = {
48
+ :sandbox => "https://api-sandbox.pitneybowes.com",
49
+ :production => "https://api.pitneybowes.com",
50
+ :is_production => false,
51
+ :default_api_version => "/v1",
52
+ :override_api_version => {
53
+ "post/developers/.../merchants/registration" => "/v2",
54
+ "get/ledger/developers/.../transactions/reports" => "/v2"
55
+ }
56
+ }
57
+ @api_group_shipping = "/shippingservices"
58
+ @txid_attrname = "X-PB-TransactionId"
59
+
60
+ class << self
61
+ attr_accessor :configuration, :api_group_shipping, :txid_attrname
62
+ end
63
+
64
+ def self.get_api_version(api_sig)
65
+ if @configuration[:override_api_version].key?(api_sig) == true
66
+ api_version = @configuration[:override_api_version][api_sig]
67
+ else
68
+ api_version = @configuration[:default_api_version]
69
+ end
70
+ return api_version
71
+ end
72
+
73
+ def self.api_url(api_version, api_path)
74
+ if @configuration[:is_production] == true
75
+ api_server = @configuration[:production]
76
+ else
77
+ api_server = @configuration[:sandbox]
78
+ end
79
+ api_server + @api_group_shipping + api_version + api_path
80
+ end
81
+
82
+ def self.authenticate_request(api_key, api_secret)
83
+ if @configuration[:is_production] == true
84
+ api_server = @configuration[:production]
85
+ else
86
+ api_server = @configuration[:sandbox]
87
+ end
88
+ url = api_server + "/oauth/token"
89
+
90
+ headers = {
91
+ :content_type => 'application/x-www-form-urlencoded',
92
+ :Authorization => 'Basic ' + Base64.strict_encode64(api_key + ":" + api_secret)
93
+ }
94
+ payload = {
95
+ :grant_type => 'client_credentials'
96
+ }
97
+ opts = {
98
+ :method => 'post',
99
+ :payload => payload,
100
+ :headers => headers,
101
+ :url => url,
102
+ :open_timeout => 15,
103
+ :timeout => 30
104
+ }
105
+
106
+ begin
107
+ res = make_http_request(opts)
108
+ json_parse(res)
109
+ rescue => e
110
+ case e
111
+ when RestClient::Exception
112
+ raise AuthenticationError.new(e.to_s, e.http_code, e.http_body)
113
+ else
114
+ raise AuthenticationError.new(e.to_s)
115
+ end
116
+ end
117
+ end
118
+
119
+ def self.api_request(auth_obj, method, api_version, api_path, headers = {}, params = {}, payload = {})
120
+ if auth_obj == nil || auth_obj.access_token == nil
121
+ raise AuthenticationError.new("Invalid or missing authentication credentials")
122
+ end
123
+ begin
124
+ url = api_url(api_version, api_path)
125
+ headers.merge!(
126
+ :accept => :json,
127
+ :content_type => :json,
128
+ :Authorization => "Bearer " + auth_obj.access_token
129
+ )
130
+ case method
131
+ when :get
132
+ payload = {}
133
+ pairs = []
134
+ params.each { |k, v|
135
+ pairs.push "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}"
136
+ }
137
+ url += "?#{pairs.join('&')}" unless pairs.empty?
138
+ end
139
+ opts = {
140
+ :headers => headers,
141
+ :method => method,
142
+ :payload => payload.to_json,
143
+ :url => url,
144
+ :open_timeout => 15,
145
+ :timeout => 30,
146
+ :user_agent => "pbshipping/v1 RubyBindings"
147
+ }
148
+ res = make_http_request(opts)
149
+ json_parse(res)
150
+ rescue => e
151
+ case e
152
+ when RestClient::Exception
153
+ raise ApiError.new(e.to_s, e.http_code, e.http_body)
154
+ else
155
+ raise ApiError.new(e.to_s)
156
+ end
157
+ end
158
+ end
159
+
160
+ def self.json_parse(response)
161
+ JSON::parse(response.body, { :symbolize_names => true })
162
+ end
163
+ end
164
+
165
+ def make_http_request(opts)
166
+ RestClient::Request.execute(opts){ |response, request, result, &block|
167
+ if [301, 302, 307].include? response.code
168
+ response.follow_redirection(request, result, &block)
169
+ else
170
+ response.return!(request, result, &block)
171
+ end
172
+ }
173
+ end
@@ -0,0 +1,45 @@
1
+ #
2
+ # Copyright 2016 Pitney Bowes Inc.
3
+ #
4
+ # Licensed under the MIT License (the "License"); you may not use this file
5
+ # except in compliance with the License. You may obtain a copy of the License
6
+ # in the LICENSE file or at
7
+ # https://opensource.org/licenses/MIT
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11
+ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ #
15
+ # File: account.rb
16
+ # Description: account management functions
17
+ #
18
+
19
+ module PBShipping
20
+ class Account < ShippingApiResource
21
+
22
+ #
23
+ # MANAGING MERCHANTS
24
+ # API: GET /ledger/accounts/{accountNumber}/balance
25
+ # API signature: get/ledger/accounts/.../balance
26
+ #
27
+ # Retrieve the account balance of a merchant account.
28
+ #
29
+ def getBalance(auth_obj)
30
+ if self.key?(:accountNumber) == false
31
+ raise MissingResourceAttribute.new(:accountNumber)
32
+ end
33
+ api_sig = "get/ledger/accounts/.../balance"
34
+ api_version = PBShipping::get_api_version(api_sig)
35
+ api_path = "/ledger/accounts/" + self[:accountNumber] + "/balance"
36
+ json_resp = PBShipping.api_request(auth_obj, :get, api_version, api_path,
37
+ {}, {}, {})
38
+ return ApiObject.new(json_resp)
39
+ end
40
+
41
+ def self.getBalanceByAccountNumber(auth_obj, accountNumber)
42
+ return Account.new({:accountNumber => accountNumber}).getBalance(auth_obj)
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,55 @@
1
+ #
2
+ # Copyright 2016 Pitney Bowes Inc.
3
+ #
4
+ # Licensed under the MIT License (the "License"); you may not use this file
5
+ # except in compliance with the License. You may obtain a copy of the License
6
+ # in the LICENSE file or at
7
+ # https://opensource.org/licenses/MIT
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11
+ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ #
15
+ # File: address.rb
16
+ # Description: address verification functions
17
+ #
18
+
19
+ module PBShipping
20
+ class Address < ShippingApiResource
21
+
22
+ #
23
+ # ADDRESS VALIDATION
24
+ # API: POST /addresses/verify
25
+ # API signature: post/addresses/verify
26
+ #
27
+ # Verify and cleanse any postal address within the United States.
28
+ # This will ensure that packages are rated accurately and the
29
+ # shipments arrive at their final destination on time.
30
+ #
31
+ # By default, the returned result would overwrite the current state
32
+ # of the object. To avoid overwriting, set the input argument
33
+ # overwrite to False and a copy of the result would be generated and
34
+ # returned instead
35
+ #
36
+ def verify(auth_obj, minimalAddressValidation=nil, overwrite=true)
37
+ if minimalAddressValidation == nil
38
+ hdrs = {"minimalAddressValidation" => false}
39
+ else
40
+ hdrs = {"minimalAddressValidation" => minimalAddressValidation}
41
+ end
42
+ api_sig = "post/address/verify"
43
+ api_version = PBShipping::get_api_version(api_sig)
44
+ api_path = "/addresses/verify"
45
+ json_resp = PBShipping::api_request(
46
+ auth_obj, :post, api_version, api_path, hdrs, {}, self)
47
+ if overwrite == true
48
+ self.update(json_resp)
49
+ self
50
+ else
51
+ Address.new(json_resp)
52
+ end
53
+ end
54
+ end
55
+ end