easypost 4.3.0 → 4.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '02404008f0fc1078e49e1e418c5a208fbca5d646f31886c3c7fab55d932def70'
4
- data.tar.gz: b8a2f7d514f9f22fa5b7ec3daf010adbfcc793c689954102b6ccbaafd7742e6a
3
+ metadata.gz: c07c842f7ff2eb209a6f447259afce469b6e1f0f481ea2bf68585869167ea8f9
4
+ data.tar.gz: 0ffb89bcb615cd1835f12e70d3c23ffa9ca9033daddddaad9db20a363f483212
5
5
  SHA512:
6
- metadata.gz: f6c9af835a6ff8ccb7574fd5a14ef96f850784e6600249a8441d23a521763ad3fb40d826cdce0f758cd4b87213a65c6ee733476d34824dabbb7fb09db2af0de0
7
- data.tar.gz: c492ad0635133598e8535a2a0c72949b0acd6f0095b511bb91011c50cec67e6d8999b5358da8aa7ae4543e38a30bc029367d8c0cfd791e4944f46fac3cdf75d9
6
+ metadata.gz: f03686d79b7d2023c26dc3c69362955465f3f99efc9b666d101a3499697ed155061afa2e8a47767bd83ae78bdd265d5363339ad4fb7686f47e5cff472483fa6a
7
+ data.tar.gz: 25f79bcc1b8c745ae9556c675d3aad56a06518853b4389081325170d2c1c1e425906bb8ded8925d40805ef961c5ccea3394da7bfe1f845117a8701f80526d526
data/.gitattributes ADDED
@@ -0,0 +1,4 @@
1
+ * text=auto
2
+
3
+ spec/cassettes/**/* -diff
4
+ spec/cassettes/**/* linguist-generated
@@ -20,7 +20,7 @@ jobs:
20
20
  ruby-version: ${{ matrix.rubyversion }}
21
21
  bundler-cache: true
22
22
  - name: run tests
23
- run: EASYPOST_TEST_API_KEY=123 EASYPOST_PROD_API_KEY=123 bundle exec rspec
23
+ run: EASYPOST_TEST_API_KEY=123 EASYPOST_PROD_API_KEY=123 make test
24
24
  lint:
25
25
  runs-on: ubuntu-latest
26
26
  steps:
@@ -31,6 +31,8 @@ jobs:
31
31
  with:
32
32
  ruby-version: "3.1"
33
33
  - name: Install Dependencies
34
- run: bundle install
34
+ run: make install
35
35
  - name: Lint Project
36
- run: rubocop
36
+ run: make lint
37
+ - name: Run security analysis
38
+ run: make brakeman
data/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## v4.4.0 (2022-07-11)
4
+
5
+ - Adds `Billing.retrieve_payment_methods()`, `Billing.fund_wallet()`, and `Billing.delete_payment_method()` functions
6
+ - Captures OS information in the user-agent header for easier debugging
7
+ - Update functions now use `patch` instead of `put` under the hood to better match API behavior and documentation. The behavior of these functions should remain the same
8
+
3
9
  ## v4.3.0 (2022-05-19)
4
10
 
5
11
  - Adds the `EndShipper` Beta class with `create`, `retrieve`, `all`, and `save` functions
data/Makefile ADDED
@@ -0,0 +1,37 @@
1
+ ## help - Display help about make targets for this Makefile
2
+ help:
3
+ @cat Makefile | grep '^## ' --color=never | cut -c4- | sed -e "`printf 's/ - /\t- /;'`" | column -s "`printf '\t'`" -t
4
+
5
+ ## brakeman - Runs security analysis on the project with Brakeman
6
+ brakeman:
7
+ brakeman lib --force
8
+
9
+ ## build - Builds the project
10
+ build:
11
+ gem build easypost.gemspec
12
+
13
+ ## clean - Cleans the project
14
+ clean:
15
+ rm -rf coverage doc *.gem
16
+
17
+ ## fix - Fix Rubocop errors
18
+ fix:
19
+ rubocop -A
20
+
21
+ ## install - Install globally from source
22
+ install:
23
+ bundle install
24
+
25
+ ## lint - Lint the project
26
+ lint:
27
+ rubocop
28
+
29
+ ## push - Pushes the built gem to Rubygems
30
+ push:
31
+ gem push *.gem
32
+
33
+ ## test - Test the project
34
+ test:
35
+ bundle exec rspec
36
+
37
+ .PHONY: help build clean fix install lint push test
data/README.md CHANGED
@@ -116,10 +116,13 @@ API Documentation can be found at: <https://easypost.com/docs/api>.
116
116
 
117
117
  ```bash
118
118
  # Install dependencies
119
- bundle install
119
+ make install
120
+
121
+ # Lint project
122
+ make lint
120
123
 
121
124
  # Run tests (coverage is generated on a successful test suite run)
122
- EASYPOST_TEST_API_KEY=123... EASYPOST_PROD_API_KEY=123... bundle exec rspec
125
+ EASYPOST_TEST_API_KEY=123... EASYPOST_PROD_API_KEY=123... make test
123
126
  ```
124
127
 
125
128
  ### Testing
data/VERSION CHANGED
@@ -1 +1 @@
1
- 4.3.0
1
+ 4.4.0
data/easypost.gemspec CHANGED
@@ -21,6 +21,7 @@ Gem::Specification.new do |spec|
21
21
  spec.require_paths = ['lib']
22
22
  spec.required_ruby_version = '>= 2.5'
23
23
 
24
+ spec.add_development_dependency 'brakeman', '~> 5.2'
24
25
  spec.add_development_dependency 'pry', '~> 0.14'
25
26
  spec.add_development_dependency 'rake', '~> 13.0'
26
27
  spec.add_development_dependency 'rspec', '~> 3.10'
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Billing class that users can manage their payment and fund
4
+ class EasyPost::Billing < EasyPost::Resource
5
+ # Fund your EasyPost wallet by charging your primary or secondary card on file.
6
+ def self.fund_wallet(amount, primary_or_secondary = 'primary', api_key = nil)
7
+ payment_info = EasyPost::Billing.get_payment_method_info(primary_or_secondary.downcase)
8
+ endpoint = payment_info[0]
9
+ payment_id = payment_info[1]
10
+
11
+ wrapped_params = { amount: amount }
12
+ EasyPost.make_request(:post, "#{endpoint}/#{payment_id}/charges", api_key, wrapped_params)
13
+
14
+ # Return true if succeeds, an error will be thrown if it fails
15
+ true
16
+ end
17
+
18
+ # Delete a payment method.
19
+ def self.delete_payment_method(primary_or_secondary, api_key = nil)
20
+ payment_info = EasyPost::Billing.get_payment_method_info(primary_or_secondary.downcase)
21
+ endpoint = payment_info[0]
22
+ payment_id = payment_info[1]
23
+
24
+ EasyPost.make_request(:delete, "#{endpoint}/#{payment_id}", api_key)
25
+
26
+ # Return true if succeeds, an error will be thrown if it fails
27
+ true
28
+ end
29
+
30
+ # Retrieve all payment methods.
31
+ def self.retrieve_payment_methods(api_key = nil)
32
+ response = EasyPost.make_request(:get, '/v2/payment_methods', api_key)
33
+
34
+ if response['id'].nil?
35
+ raise EasyPost::Error.new('Billing has not been setup for this user. Please add a payment method.')
36
+ end
37
+
38
+ EasyPost::Util.convert_to_easypost_object(response, api_key)
39
+ end
40
+
41
+ # Get payment method info (type of the payment method and ID of the payment method)
42
+ private_class_method def self.get_payment_method_info(primary_or_secondary)
43
+ payment_methods = EasyPost::Billing.retrieve_payment_methods
44
+ payment_method_map = {
45
+ 'primary' => 'primary_payment_method',
46
+ 'secondary' => 'secondary_payment_method',
47
+ }
48
+
49
+ payment_method_to_use = payment_method_map[primary_or_secondary]
50
+
51
+ error_string = 'The chosen payment method is not valid. Please try again.'
52
+ raise EasyPost::Error.new(error_string) if payment_methods[payment_method_to_use].nil?
53
+
54
+ payment_method_id = payment_methods[payment_method_to_use]['id']
55
+
56
+ unless payment_method_id.nil?
57
+ if payment_method_id.start_with?('card_')
58
+ endpoint = '/v2/credit_cards'
59
+ elsif payment_method_id.start_with?('bank_')
60
+ endpoint = '/v2/bank_accounts'
61
+ else
62
+ raise EasyPost::Error.new(error_string)
63
+ end
64
+ end
65
+
66
+ [endpoint, payment_method_id]
67
+ end
68
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ # PaymentMethod objects represent a payment method of a user.
4
+ class EasyPost::PaymentMethod < EasyPost::Resource
5
+ # Retrieve all payment methods.
6
+ def self.all(api_key = nil)
7
+ response = EasyPost.make_request(:get, url, api_key)
8
+
9
+ if response['id'].nil?
10
+ raise EasyPost::Error.new('Billing has not been setup for this user. Please add a payment method.')
11
+ end
12
+
13
+ EasyPost::Util.convert_to_easypost_object(response, api_key)
14
+ end
15
+ end
@@ -97,7 +97,7 @@ class EasyPost::Resource < EasyPost::EasyPostObject
97
97
 
98
98
  wrapped_params = { self.class.class_name => values }
99
99
 
100
- response = EasyPost.make_request(:put, url, @api_key, wrapped_params)
100
+ response = EasyPost.make_request(:patch, url, @api_key, wrapped_params)
101
101
  refresh_from(response, api_key)
102
102
  end
103
103
  self
data/lib/easypost/user.rb CHANGED
@@ -16,7 +16,7 @@ class EasyPost::User < EasyPost::Resource
16
16
 
17
17
  wrapped_params = { user: values }
18
18
 
19
- response = EasyPost.make_request(:put, url, @api_key, wrapped_params)
19
+ response = EasyPost.make_request(:patch, url, @api_key, wrapped_params)
20
20
  refresh_from(response, api_key)
21
21
  end
22
22
  self
data/lib/easypost/util.rb CHANGED
@@ -2,6 +2,90 @@
2
2
 
3
3
  # Internal utilities helpful for this libraries operation.
4
4
  module EasyPost::Util
5
+ attr_accessor :os_name, :os_version, :os_arch
6
+
7
+ BY_PREFIX = {
8
+ 'adr' => EasyPost::Address,
9
+ 'batch' => EasyPost::Batch,
10
+ 'brd' => EasyPost::Brand,
11
+ 'ca' => EasyPost::CarrierAccount,
12
+ 'cstinfo' => EasyPost::CustomsInfo,
13
+ 'cstitem' => EasyPost::CustomsItem,
14
+ 'es' => EasyPost::Beta::EndShipper,
15
+ 'evt' => EasyPost::Event,
16
+ 'hook' => EasyPost::Webhook,
17
+ 'ins' => EasyPost::Insurance,
18
+ 'order' => EasyPost::Order,
19
+ 'pickup' => EasyPost::Pickup,
20
+ 'pickuprate' => EasyPost::PickupRate,
21
+ 'pl' => EasyPost::PostageLabel,
22
+ 'plrep' => EasyPost::Report,
23
+ 'prcl' => EasyPost::Parcel,
24
+ 'rate' => EasyPost::Rate,
25
+ 'refrep' => EasyPost::Report,
26
+ 'rfnd' => EasyPost::Refund,
27
+ 'sf' => EasyPost::ScanForm,
28
+ 'shp' => EasyPost::Shipment,
29
+ 'shpinvrep' => EasyPost::Report,
30
+ 'shprep' => EasyPost::Report,
31
+ 'trk' => EasyPost::Tracker,
32
+ 'trkrep' => EasyPost::Report,
33
+ 'user' => EasyPost::User,
34
+ }.freeze
35
+
36
+ BY_TYPE = {
37
+ 'Address' => EasyPost::Address,
38
+ 'Batch' => EasyPost::Batch,
39
+ 'Brand' => EasyPost::Brand,
40
+ 'CarrierAccount' => EasyPost::CarrierAccount,
41
+ 'CustomsInfo' => EasyPost::CustomsInfo,
42
+ 'CustomsItem' => EasyPost::CustomsItem,
43
+ 'EndShipper' => EasyPost::Beta::EndShipper,
44
+ 'Event' => EasyPost::Event,
45
+ 'Insurance' => EasyPost::Insurance,
46
+ 'Order' => EasyPost::Order,
47
+ 'Parcel' => EasyPost::Parcel,
48
+ 'PaymentLogReport' => EasyPost::Report,
49
+ 'Pickup' => EasyPost::Pickup,
50
+ 'PickupRate' => EasyPost::PickupRate,
51
+ 'PostageLabel' => EasyPost::PostageLabel,
52
+ 'Rate' => EasyPost::Rate,
53
+ 'Referral' => EasyPost::Beta::Referral,
54
+ 'Refund' => EasyPost::Refund,
55
+ 'RefundReport' => EasyPost::Report,
56
+ 'Report' => EasyPost::Report,
57
+ 'ScanForm' => EasyPost::ScanForm,
58
+ 'Shipment' => EasyPost::Shipment,
59
+ 'ShipmentInvoiceReport' => EasyPost::Report,
60
+ 'ShipmentReport' => EasyPost::Report,
61
+ 'TaxIdentifier' => EasyPost::TaxIdentifier,
62
+ 'Tracker' => EasyPost::Tracker,
63
+ 'TrackerReport' => EasyPost::Report,
64
+ 'User' => EasyPost::User,
65
+ 'Webhook' => EasyPost::Webhook,
66
+ }.freeze
67
+
68
+ def self.os_name
69
+ case RUBY_PLATFORM
70
+ when /linux/i
71
+ 'Linux'
72
+ when /darwin/i
73
+ 'Darwin'
74
+ when /cygwin|mswin|mingw|bccwin|wince|emx/i
75
+ 'Windows'
76
+ else
77
+ 'Unknown'
78
+ end
79
+ end
80
+
81
+ def self.os_version
82
+ Gem::Platform.local.version
83
+ end
84
+
85
+ def self.os_arch
86
+ Gem::Platform.local.cpu
87
+ end
88
+
5
89
  # Form-encode a multi-layer dictionary to a one-layer dictionary.
6
90
  def self.form_encode_params(hash, parent_keys = [], parent_dict = {})
7
91
  result = parent_dict or {}
@@ -55,84 +139,23 @@ module EasyPost::Util
55
139
 
56
140
  # Convert data to an EasyPost Object.
57
141
  def self.convert_to_easypost_object(response, api_key, parent = nil, name = nil)
58
- types = {
59
- 'Address' => EasyPost::Address,
60
- 'Batch' => EasyPost::Batch,
61
- 'Brand' => EasyPost::Brand,
62
- 'CarrierAccount' => EasyPost::CarrierAccount,
63
- 'CustomsInfo' => EasyPost::CustomsInfo,
64
- 'CustomsItem' => EasyPost::CustomsItem,
65
- 'EndShipper' => EasyPost::Beta::EndShipper,
66
- 'Event' => EasyPost::Event,
67
- 'Insurance' => EasyPost::Insurance,
68
- 'Order' => EasyPost::Order,
69
- 'Parcel' => EasyPost::Parcel,
70
- 'PaymentLogReport' => EasyPost::Report,
71
- 'Pickup' => EasyPost::Pickup,
72
- 'PickupRate' => EasyPost::PickupRate,
73
- 'PostageLabel' => EasyPost::PostageLabel,
74
- 'Rate' => EasyPost::Rate,
75
- 'Referral' => EasyPost::Beta::Referral,
76
- 'Refund' => EasyPost::Refund,
77
- 'RefundReport' => EasyPost::Report,
78
- 'Report' => EasyPost::Report,
79
- 'ScanForm' => EasyPost::ScanForm,
80
- 'Shipment' => EasyPost::Shipment,
81
- 'ShipmentInvoiceReport' => EasyPost::Report,
82
- 'ShipmentReport' => EasyPost::Report,
83
- 'TaxIdentifier' => EasyPost::TaxIdentifier,
84
- 'Tracker' => EasyPost::Tracker,
85
- 'TrackerReport' => EasyPost::Report,
86
- 'User' => EasyPost::User,
87
- 'Webhook' => EasyPost::Webhook,
88
- }
89
-
90
- prefixes = {
91
- 'adr' => EasyPost::Address,
92
- 'batch' => EasyPost::Batch,
93
- 'brd' => EasyPost::Brand,
94
- 'ca' => EasyPost::CarrierAccount,
95
- 'cstinfo' => EasyPost::CustomsInfo,
96
- 'cstitem' => EasyPost::CustomsItem,
97
- 'es' => EasyPost::Beta::EndShipper,
98
- 'evt' => EasyPost::Event,
99
- 'hook' => EasyPost::Webhook,
100
- 'ins' => EasyPost::Insurance,
101
- 'order' => EasyPost::Order,
102
- 'pickup' => EasyPost::Pickup,
103
- 'pickuprate' => EasyPost::PickupRate,
104
- 'pl' => EasyPost::PostageLabel,
105
- 'plrep' => EasyPost::Report,
106
- 'prcl' => EasyPost::Parcel,
107
- 'rate' => EasyPost::Rate,
108
- 'refrep' => EasyPost::Report,
109
- 'rfnd' => EasyPost::Refund,
110
- 'sf' => EasyPost::ScanForm,
111
- 'shp' => EasyPost::Shipment,
112
- 'shpinvrep' => EasyPost::Report,
113
- 'shprep' => EasyPost::Report,
114
- 'trk' => EasyPost::Tracker,
115
- 'trkrep' => EasyPost::Report,
116
- 'user' => EasyPost::User,
117
- }
118
-
119
142
  case response
120
143
  when Array
121
144
  response.map { |i| convert_to_easypost_object(i, api_key, parent) }
122
145
  when Hash
123
146
  if (cls_name = response[:object])
124
- cls = types[cls_name]
147
+ cls = BY_TYPE[cls_name]
125
148
  elsif response[:id]
126
149
  if response[:id].index('_').nil?
127
150
  cls = EasyPost::EasyPostObject
128
151
  elsif (cls_prefix = response[:id][0..response[:id].index('_')])
129
- cls = prefixes[cls_prefix[0..-2]]
152
+ cls = BY_PREFIX[cls_prefix[0..-2]]
130
153
  end
131
154
  elsif response['id']
132
155
  if response['id'].index('_').nil?
133
156
  cls = EasyPost::EasyPostObject
134
157
  elsif (cls_prefix = response['id'][0..response['id'].index('_')])
135
- cls = prefixes[cls_prefix[0..-2]]
158
+ cls = BY_PREFIX[cls_prefix[0..-2]]
136
159
  end
137
160
  end
138
161
 
@@ -12,7 +12,7 @@ class EasyPost::Webhook < EasyPost::Resource
12
12
 
13
13
  instance_url = "#{self.class.url}/#{CGI.escape(id)}"
14
14
 
15
- response = EasyPost.make_request(:put, instance_url, @api_key, params)
15
+ response = EasyPost.make_request(:patch, instance_url, @api_key, params)
16
16
  refresh_from(response, api_key)
17
17
 
18
18
  self
data/lib/easypost.rb CHANGED
@@ -5,7 +5,6 @@ require 'cgi'
5
5
  require 'net/http'
6
6
 
7
7
  require 'easypost/version'
8
- require 'easypost/util'
9
8
  require 'easypost/object'
10
9
  require 'easypost/resource'
11
10
  require 'easypost/error'
@@ -15,6 +14,7 @@ require 'easypost/connection'
15
14
  require 'easypost/address'
16
15
  require 'easypost/api_key'
17
16
  require 'easypost/batch'
17
+ require 'easypost/billing'
18
18
  require 'easypost/brand'
19
19
  require 'easypost/carrier_account'
20
20
  require 'easypost/carrier_type'
@@ -38,9 +38,10 @@ require 'easypost/user'
38
38
  require 'easypost/webhook'
39
39
  require 'easypost/beta'
40
40
 
41
+ require 'easypost/util'
42
+
41
43
  module EasyPost
42
44
  DEFAULT_API_BASE = 'https://api.easypost.com'
43
- DEFAULT_USER_AGENT = "EasyPost/v2 RubyClient/#{EasyPost::VERSION} Ruby/#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}"
44
45
 
45
46
  class << self
46
47
  attr_accessor :api_key, :api_base
@@ -49,10 +50,17 @@ module EasyPost
49
50
 
50
51
  self.api_base = DEFAULT_API_BASE
51
52
 
53
+ def self.user_agent
54
+ @user_agent ||=
55
+ "EasyPost/v2 RubyClient/#{EasyPost::VERSION} Ruby/#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL} " \
56
+ "OS/#{EasyPost::Util.os_name} OSVersion/#{EasyPost::Util.os_version} " \
57
+ "OSArch/#{EasyPost::Util.os_arch}"
58
+ end
59
+
52
60
  def self.default_headers
53
61
  @default_headers ||= {
54
62
  'Content-Type' => 'application/json',
55
- 'User-Agent' => EasyPost::DEFAULT_USER_AGENT,
63
+ 'User-Agent' => user_agent,
56
64
  }
57
65
  end
58
66
 
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: easypost
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.3.0
4
+ version: 4.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - EasyPost Developers
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-05-19 00:00:00.000000000 Z
11
+ date: 2022-07-11 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: brakeman
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '5.2'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '5.2'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: pry
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -143,6 +157,7 @@ executables:
143
157
  extensions: []
144
158
  extra_rdoc_files: []
145
159
  files:
160
+ - ".gitattributes"
146
161
  - ".github/ISSUE_TEMPLATE/bug_report.yml"
147
162
  - ".github/ISSUE_TEMPLATE/feature_request.yml"
148
163
  - ".github/PULL_REQUEST_TEMPLATE.md"
@@ -154,6 +169,7 @@ files:
154
169
  - CONTRIBUTING.md
155
170
  - Gemfile
156
171
  - LICENSE
172
+ - Makefile
157
173
  - README.md
158
174
  - Rakefile
159
175
  - SECURITY.md
@@ -170,6 +186,7 @@ files:
170
186
  - lib/easypost/beta.rb
171
187
  - lib/easypost/beta/end_shipper.rb
172
188
  - lib/easypost/beta/referral.rb
189
+ - lib/easypost/billing.rb
173
190
  - lib/easypost/brand.rb
174
191
  - lib/easypost/carrier_account.rb
175
192
  - lib/easypost/carrier_type.rb
@@ -182,6 +199,7 @@ files:
182
199
  - lib/easypost/object.rb
183
200
  - lib/easypost/order.rb
184
201
  - lib/easypost/parcel.rb
202
+ - lib/easypost/payment_method.rb
185
203
  - lib/easypost/pickup.rb
186
204
  - lib/easypost/pickup_rate.rb
187
205
  - lib/easypost/postage_label.rb
@@ -216,7 +234,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
216
234
  - !ruby/object:Gem::Version
217
235
  version: '0'
218
236
  requirements: []
219
- rubygems_version: 3.3.7
237
+ rubygems_version: 3.3.11
220
238
  signing_key:
221
239
  specification_version: 4
222
240
  summary: EasyPost Ruby Client Library