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,131 @@
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: api_object.rb
16
+ # Description: base shipping api object class
17
+ #
18
+
19
+ module PBShipping
20
+ class ApiObject
21
+ include Enumerable
22
+
23
+ def initialize(values=nil)
24
+ @values = {}
25
+ if values != nil
26
+ self.update(values)
27
+ end
28
+ end
29
+
30
+ def self.convert_to_api_object(values)
31
+ case values
32
+ when Array
33
+ values.map { |v| self.convert_to_api_object(v) }
34
+ when Hash
35
+ self.new(values)
36
+ else
37
+ values
38
+ end
39
+ end
40
+
41
+ def update(values)
42
+ values.each do |k, v|
43
+ if v.is_a?(Array) || v.is_a?(Hash)
44
+ new_v = self.class.convert_to_api_object(v)
45
+ else
46
+ new_v = v
47
+ end
48
+ @values[k.to_sym] = new_v
49
+ end
50
+ instance_eval do
51
+ add_accessors(@values.keys)
52
+ end
53
+ end
54
+
55
+ def [](k)
56
+ @values[k.to_sym]
57
+ end
58
+
59
+ def []=(k, v)
60
+ update({k => v})
61
+ end
62
+
63
+ def keys
64
+ @values.keys
65
+ end
66
+
67
+ def values
68
+ @values.values
69
+ end
70
+
71
+ def to_s(*args)
72
+ JSON.pretty_generate @values
73
+ end
74
+
75
+ def to_json(*a)
76
+ JSON.dump(@values)
77
+ end
78
+
79
+ def as_json(*a)
80
+ @values.as_json(*a)
81
+ end
82
+
83
+ def to_hash
84
+ @values
85
+ end
86
+
87
+ def each(&blk)
88
+ @values.each(&blk)
89
+ end
90
+
91
+ def key?(k)
92
+ @values.key?(k)
93
+ end
94
+
95
+ if RUBY_VERSION < '1.9.2'
96
+ def respond_to?(symbol)
97
+ @values.has_key?(symbol) || super
98
+ end
99
+ end
100
+
101
+ def inspect()
102
+ "#<#{self.class}:0x#{self.object_id.to_s(16)}}> json: " + self.to_s
103
+ end
104
+
105
+ def create_accessor(k_name, k_index)
106
+ metaclass.instance_eval do
107
+ define_method(k_name) { @values[k_index] }
108
+ define_method(:"#{k_name}=") do |v|
109
+ @values[k_index] = v unless k_index == ''
110
+ end
111
+ end
112
+ end
113
+
114
+ def add_accessors(keys)
115
+ keys.each do |k|
116
+ orig_k = k
117
+ while respond_to?(k) do
118
+ k = "_#{k}".to_sym
119
+ end
120
+ create_accessor(k, orig_k)
121
+ end
122
+ end
123
+
124
+ def metaclass
125
+ class << self
126
+ self
127
+ end
128
+ end
129
+
130
+ end
131
+ end
@@ -0,0 +1,22 @@
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: api_resource.rb
16
+ # Description: base api resource class
17
+ #
18
+
19
+ module PBShipping
20
+ class ApiResource < ApiObject
21
+ end
22
+ end
@@ -0,0 +1,36 @@
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: authentication.rb
16
+ # Description: handling authentication tasks for subsequent shipping api calls.
17
+ #
18
+
19
+ module PBShipping
20
+ class AuthenticationToken
21
+ attr_accessor :api_key, :api_secret, :access_token, :auth_info
22
+
23
+ def initialize(api_key, api_secret)
24
+ @api_key = api_key
25
+ @api_secret = api_secret
26
+ @access_token = nil
27
+ @auth_info = nil
28
+ refresh_token()
29
+ end
30
+
31
+ def refresh_token()
32
+ @auth_info = PBShipping::authenticate_request(@api_key, @api_secret)
33
+ @access_token = @auth_info[:access_token]
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,53 @@
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: carrier.rb
16
+ # Description: carrier query such as supported countries
17
+ #
18
+
19
+ module PBShipping
20
+ class Carrier < ShippingApiResource
21
+
22
+ #
23
+ # COUNTRIES LIST
24
+ # API: GET /countries
25
+ # API signature: get/countries
26
+ #
27
+ # Returns a list of supported destination countries to which the carrier
28
+ # offers international shipping services.
29
+ #
30
+ def getCountries(auth_obj, originCountryCode)
31
+ if self.key?(:name) == false
32
+ raise MissingResourceAttribute.new(:name)
33
+ end
34
+ params = {
35
+ "carrier" => self[:name],
36
+ "originCountryCode" => originCountryCode
37
+ }
38
+ api_sig = "get/countries"
39
+ api_version = PBShipping::get_api_version(api_sig)
40
+ api_path = "/countries"
41
+ json_resp = PBShipping.api_request(auth_obj, :get, api_version, api_path,
42
+ {}, params, {})
43
+ country_list = []
44
+ json_resp.each { |country| country_list << Country.new(country) }
45
+ return country_list
46
+ end
47
+
48
+ def self.getCountriesForCarrier(auth_obj, carrier_name, originCountryCode)
49
+ return Carrier.new({:name => carrier_name}).getCountries(
50
+ auth_obj, originCountryCode)
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,22 @@
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: country.rb
16
+ # Description: base class for country objects.
17
+ #
18
+
19
+ module PBShipping
20
+ class Country < ShippingApiResource
21
+ end
22
+ end
@@ -0,0 +1,26 @@
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: country.rb
16
+ # Description: base class for custom objects.
17
+ #
18
+
19
+ module PBShipping
20
+ class Customs < ShippingApiResource
21
+ end
22
+ class CustomsInfo < ShippingApiResource
23
+ end
24
+ class CustomsItem < ShippingApiResource
25
+ end
26
+ end
@@ -0,0 +1,120 @@
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: developer.rb
16
+ # Description: developer account query, merchant managements, and transaction
17
+ # history query
18
+ #
19
+
20
+ module PBShipping
21
+ class Developer < ShippingApiResource
22
+
23
+ #
24
+ # CLIENT LIBRARY SPECIFIC
25
+ # API: GET /developers/{developerId}
26
+ # API signature: get/developers/...
27
+ #
28
+ # Query for developer account attributes
29
+ #
30
+ def refresh(auth_obj)
31
+ if self.key?(:developerId) == false
32
+ raise MissingResourceAttribute.new(:developerId)
33
+ end
34
+ api_sig = "get/developers/.."
35
+ api_version = PBShipping::get_api_version(api_sig)
36
+ api_path = "/developers/" + self[:developerId]
37
+ json_resp = PBShipping::api_request(auth_obj, :get, api_version, api_path, {}, {}, {})
38
+ self.update(json_resp)
39
+ end
40
+
41
+ #
42
+ # MANAGING MERCHANTS
43
+ # API: GET /developers/{developerId}/merchants/emails/{emailId}/
44
+ # API signature: get/developers/.../merchants/emails/...
45
+ #
46
+ # Register your merchants or shippers, if you have signed up for the
47
+ # individual account payment model.
48
+ #
49
+ # This method allows you to retrieve the merchant ID and related
50
+ # information based on the Email ID they used while registering,
51
+ # so that you can request transactions on their behalf.
52
+ #
53
+ def registerMerchantIndividualAccount(auth_obj, emailid)
54
+ if self.key?(:developerId) == false
55
+ raise MissingResourceAttribute.new(:developerId)
56
+ end
57
+ api_sig = "get/developers/.../merchants/emails/..."
58
+ api_version = PBShipping::get_api_version(api_sig)
59
+ api_path = "/developers/" + self[:developerId]
60
+ api_path += "/merchants/emails/" + emailid + "/"
61
+ Merchant.new(PBShipping::api_request(
62
+ auth_obj, :get, api_version, api_path, {}, {}, {}))
63
+ end
64
+
65
+ #
66
+ # CLIENT LIBRARY SPECIFIC
67
+ # API: GET /developers/{developerId}/merchants/emails/{emailId}/
68
+ # API signature: get/developers/.../merchants/emails/...
69
+ #
70
+ # Query for merchant details using merchant's email address if developer
71
+ # is operating in bulk mode
72
+ #
73
+ def getMerchantBulkAccount(auth_obj, emailid)
74
+ # use the same underlying REST call
75
+ return self.registerMerchantIndividualAccount(auth_obj, emailid)
76
+ end
77
+
78
+ #
79
+ # MANAGING MERCHANTS
80
+ # API: POST /developers/{developerId}/merchants/registration
81
+ # API signature: post/developers/.../merchants/registration
82
+ #
83
+ # Register your merchants or shippers, if you have signed up for the
84
+ # bulk account payment model.
85
+ #
86
+ # This method allows you to retrieve the merchant ID and related
87
+ # information, so that you can request transactions on their behalf.
88
+ #
89
+ def registerMerchantBulkAccount(auth_obj, address)
90
+ if self.key?(:developerId) == false
91
+ raise MissingResourceAttribute.new(:developerId)
92
+ end
93
+ api_sig = "post/developers/.../merchants/registration"
94
+ api_version = PBShipping::get_api_version(api_sig)
95
+ api_path = "/developers/" + self[:developerId]
96
+ api_path += "/merchants/registration"
97
+ Merchant.new(PBShipping::api_request(
98
+ auth_obj, :post, api_version, api_path, {}, {}, address))
99
+ end
100
+
101
+ #
102
+ # MANAGING MERCHANTS
103
+ # API: GET /ledger/developers/{developerId}/transactions/reports
104
+ # API signature: get/ledger/developers/.../transactions/reports
105
+ #
106
+ # Retrieve all transactions based on the given input parameters
107
+ #
108
+ def getTransactionReport(auth_obj, params)
109
+ if self.key?(:developerId) == false
110
+ raise MissingResourceAttribute.new(:developerId)
111
+ end
112
+ api_sig = "get/ledger/developers/.../transactions/reports"
113
+ api_version = PBShipping::get_api_version(api_sig)
114
+ api_path = "/ledger/developers/" + self[:developerId]
115
+ api_path += "/transactions/reports"
116
+ ApiObject.new(PBShipping::api_request(
117
+ auth_obj, :get, api_version, api_path, {}, params, {}))
118
+ end
119
+ end
120
+ end
@@ -0,0 +1,73 @@
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: error.rb
16
+ # Description: class encapsulating shipping api exceptions/errors.
17
+ #
18
+
19
+ module PBShipping
20
+ class ApiError < StandardError
21
+ attr_reader :message, :http_code, :http_body, :error_info
22
+ def initialize(message, http_code=nil, http_body=nil)
23
+ @message = message
24
+ @http_code = http_code
25
+ @http_body = http_body
26
+ @error_info = []
27
+ if http_body != nil
28
+ begin
29
+ json_error = PBShipping::json_parse(http_body)
30
+ rescue => e
31
+ end
32
+ if json_error.is_a?(Array)
33
+ for next_err in json_error
34
+ if next_err.key?(:key) == true
35
+ @error_info << {
36
+ :errorCode => next_err[:key],
37
+ :message => next_err[:message]
38
+ }
39
+ elsif next_err.key?(:errorCode) == true
40
+ @error_info << {
41
+ :errorCode => next_err[:errorCode],
42
+ :message => next_err[:message]
43
+ }
44
+ end
45
+ end
46
+ elsif json_error.is_a?(Hash) && json_error.key?(:errors)
47
+ for next_err in json_error[:errors]
48
+ @error_info << {
49
+ :errorCode => next_err[:errorCode],
50
+ :message => next_err[:errorDescription]
51
+ }
52
+ end
53
+ end
54
+ end
55
+ end
56
+
57
+ def to_s
58
+ if http_body != nil
59
+ msg = @message + " " + http_body.to_s
60
+ else
61
+ msg = @message
62
+ end
63
+ return msg
64
+ end
65
+ end
66
+ class AuthenticationError < ApiError
67
+ end
68
+ class MissingResourceAttribute < ApiError
69
+ def initialize(missing_attr)
70
+ super("Attribute " + missing_attr.to_s + " is missing")
71
+ end
72
+ end
73
+ end