easypost 4.8.0 → 4.8.1

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: d6f62b9504efc5502cea14eb8c0ec792fe8c2987d857aedc34522663f2587706
4
- data.tar.gz: e8c927c3ee7d01bc7a4d1d42c9cc6c1360ea082c66017bdf31c6399d503753fe
3
+ metadata.gz: 9ad992fe480aa1a1c8b1fc7e85dd06c7af4478bca9bcc3a2f083daaeeedcc39e
4
+ data.tar.gz: f37a2a32dac1dcc1efa0121dce6b6edbf14e3aa690308d393815ecaabddedda4
5
5
  SHA512:
6
- metadata.gz: 7a731d243163def53e558f86191f992156ad50d2da7ed35f8993ae3b4f01298cacf99f5c507df44f420f856bd6c7c87b1f0965e98bd5f2719f7ebc048a383cd1
7
- data.tar.gz: c79fb8e2236b550a0ca7925674f685054262bf0a88dda6dbd5582ad74828e85bb5268d188656513feb6e8cdf164792eb0f646ca436cb2d67d95b83c6f5be9906
6
+ metadata.gz: 6f0e9c8cbfb299608c58c865a2193e163739ca3543fb5bbea44026930bbecf34ae8d772fc2377909492a89d8c7d3d1440c7a9f7ebf25935927db35bc6ba1b7c3
7
+ data.tar.gz: 8b02eee4c36c0c918e4a3f4884694c48d453bbf3aba7db653669618462d67bad30c0cf0ceec96ee44de0b335626e0992b493ad06e4457feeb5f661ebe55ceb70
@@ -4,6 +4,7 @@ on:
4
4
  push:
5
5
  branches: [master]
6
6
  pull_request: ~
7
+ workflow_dispatch: ~
7
8
 
8
9
  jobs:
9
10
  run-tests:
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## v4.8.1 (2022-10-24)
4
+
5
+ - Concatenates `error.message` if it incorrectly comes back from the API as an array
6
+ - Treats any HTTP status outside the `2xx` range as an error. Impact expected is minimal as this change only affects `1xx` and `3xx` HTTP status codes
7
+
3
8
  ## v4.8.0 (2022-09-21)
4
9
 
5
10
  - Adds support to buy a shipment by passing in `end_shipper_id`
data/Makefile CHANGED
@@ -39,9 +39,9 @@ release:
39
39
  scan:
40
40
  bundle exec brakeman lib --force
41
41
 
42
- ## test - Test the project
42
+ ## test - Test the project (and ignore warnings for test output)
43
43
  test:
44
- bundle exec rspec
44
+ RUBYOPT="-W0" bundle exec rspec
45
45
 
46
46
  ## update - Updates dependencies
47
47
  update:
data/VERSION CHANGED
@@ -1 +1 @@
1
- 4.8.0
1
+ 4.8.1
@@ -28,20 +28,12 @@ class EasyPost::Address < EasyPost::Resource
28
28
  wrapped_params[class_name.to_sym] = params
29
29
  response = EasyPost.make_request(:post, "#{url}/create_and_verify", api_key, wrapped_params)
30
30
 
31
- raise EasyPost::Error.new('Unable to verify address.') unless response.key?('address')
32
-
33
31
  EasyPost::Util.convert_to_easypost_object(response['address'], api_key)
34
32
  end
35
33
 
36
34
  # Verify an Address.
37
35
  def verify
38
- begin
39
- response = EasyPost.make_request(:get, "#{url}/verify", @api_key)
40
- rescue StandardError
41
- raise EasyPost::Error.new('Unable to verify address.')
42
- end
43
-
44
- raise EasyPost::Error.new('Unable to verify address.') unless response.key?('address')
36
+ response = EasyPost.make_request(:get, "#{url}/verify", @api_key)
45
37
 
46
38
  EasyPost::Util.convert_to_easypost_object(response['address'], api_key)
47
39
  end
@@ -2,10 +2,63 @@
2
2
 
3
3
  # Referral objects are User objects created from a Partner user.
4
4
  class EasyPost::Beta::Referral < EasyPost::Resource
5
+ class << self
6
+ protected
7
+
8
+ # Retrieve EasyPost's Stripe public API key.
9
+ def retrieve_easypost_stripe_api_key
10
+ response = EasyPost.make_request(:get, '/beta/partners/stripe_public_key', @api_key)
11
+ response['public_key']
12
+ end
13
+
14
+ # Get credit card token from Stripe.
15
+ def create_stripe_token(number, expiration_month, expiration_year,
16
+ cvc, easypost_stripe_token)
17
+ headers = {
18
+ # This Stripe endpoint only accepts URL form encoded bodies.
19
+ Authorization: "Bearer #{easypost_stripe_token}",
20
+ 'Content-type': 'application/x-www-form-urlencoded',
21
+ }
22
+
23
+ credit_card_hash = {
24
+ card: {
25
+ number: number,
26
+ exp_month: expiration_month,
27
+ exp_year: expiration_year,
28
+ cvc: cvc,
29
+ },
30
+ }
31
+
32
+ form_encoded_params = EasyPost::Util.form_encode_params(credit_card_hash)
33
+
34
+ uri = URI.parse('https://api.stripe.com/v1/tokens')
35
+ http = Net::HTTP.new(uri.host, uri.port)
36
+ http.use_ssl = true
37
+ request = Net::HTTP::Post.new(uri.request_uri, headers)
38
+ query = URI.encode_www_form(form_encoded_params)
39
+
40
+ response = http.request(request, query)
41
+ response_json = JSON.parse(response.body)
42
+ response_json['id']
43
+ end
44
+
45
+ # Submit Stripe credit card token to EasyPost.
46
+ def create_easypost_credit_card(referral_api_key, stripe_object_id, priority = 'primary')
47
+ wrapped_params = {
48
+ credit_card: {
49
+ stripe_object_id: stripe_object_id,
50
+ priority: priority,
51
+ },
52
+ }
53
+ response = EasyPost.make_request(:post, '/beta/credit_cards', referral_api_key, wrapped_params)
54
+ EasyPost::Util.convert_to_easypost_object(response, referral_api_key)
55
+ end
56
+ end
57
+
5
58
  # Create a referral user. This function requires the Partner User's API key.
6
59
  # <b>DEPRECATED:</b> Please use <tt>Referral</tt> in the main namespace instead.
7
60
  def self.create(params = {}, api_key = nil)
8
- warn '[DEPRECATION] Please use `EndShipper.create` in the main namespace instead.'
61
+ warn '[DEPRECATION] Please use `Referral.create` in the main namespace instead.'
9
62
  response = EasyPost.make_request(:post, '/beta/referral_customers', api_key, { user: params })
10
63
  EasyPost::Util.convert_to_easypost_object(response, api_key)
11
64
  end
@@ -13,7 +66,7 @@ class EasyPost::Beta::Referral < EasyPost::Resource
13
66
  # Update a referral user. This function requires the Partner User's API key.
14
67
  # <b>DEPRECATED:</b> Please use <tt>Referral</tt> in the main namespace instead.
15
68
  def self.update_email(email, user_id, api_key = nil)
16
- warn '[DEPRECATION] Please use `EndShipper.update_email` in the main namespace instead.'
69
+ warn '[DEPRECATION] Please use `Referral.update_email` in the main namespace instead.'
17
70
  wrapped_params = {
18
71
  user: {
19
72
  email: email,
@@ -28,7 +81,7 @@ class EasyPost::Beta::Referral < EasyPost::Resource
28
81
  # Retrieve a list of referral users. This function requires the Partner User's API key.
29
82
  # <b>DEPRECATED:</b> Please use <tt>Referral</tt> in the main namespace instead.
30
83
  def self.all(params = {}, api_key = nil)
31
- warn '[DEPRECATION] Please use `EndShipper.all` in the main namespace instead.'
84
+ warn '[DEPRECATION] Please use `Referral.all` in the main namespace instead.'
32
85
  response = EasyPost.make_request(:get, '/beta/referral_customers', api_key, params)
33
86
  EasyPost::Util.convert_to_easypost_object(response, api_key)
34
87
  end
@@ -36,7 +89,7 @@ class EasyPost::Beta::Referral < EasyPost::Resource
36
89
  # Add credit card to a referral user. This function requires the Referral User's API key.
37
90
  # <b>DEPRECATED:</b> Please use <tt>Referral</tt> in the main namespace instead.
38
91
  def self.add_credit_card(referral_api_key, number, expiration_month, expiration_year, cvc, priority = 'primary')
39
- warn '[DEPRECATION] Please use `EndShipper.add_credit_card` in the main namespace instead.'
92
+ warn '[DEPRECATION] Please use `Referral.add_credit_card` in the main namespace instead.'
40
93
  easypost_stripe_api_key = retrieve_easypost_stripe_api_key
41
94
 
42
95
  begin
@@ -54,53 +107,4 @@ class EasyPost::Beta::Referral < EasyPost::Resource
54
107
  response = create_easypost_credit_card(referral_api_key, stripe_credit_card_token, priority)
55
108
  EasyPost::Util.convert_to_easypost_object(response, referral_api_key)
56
109
  end
57
-
58
- # Retrieve EasyPost's Stripe public API key.
59
- private_class_method def self.retrieve_easypost_stripe_api_key
60
- response = EasyPost.make_request(:get, '/beta/partners/stripe_public_key', @api_key)
61
- response['public_key']
62
- end
63
-
64
- # Get credit card token from Stripe.
65
- private_class_method def self.create_stripe_token(number, expiration_month, expiration_year,
66
- cvc, easypost_stripe_token)
67
- headers = {
68
- # This Stripe endpoint only accepts URL form encoded bodies.
69
- Authorization: "Bearer #{easypost_stripe_token}",
70
- 'Content-type': 'application/x-www-form-urlencoded',
71
- }
72
-
73
- credit_card_hash = {
74
- card: {
75
- number: number,
76
- exp_month: expiration_month,
77
- exp_year: expiration_year,
78
- cvc: cvc,
79
- },
80
- }
81
-
82
- form_encoded_params = EasyPost::Util.form_encode_params(credit_card_hash)
83
-
84
- uri = URI.parse('https://api.stripe.com/v1/tokens')
85
- http = Net::HTTP.new(uri.host, uri.port)
86
- http.use_ssl = true
87
- request = Net::HTTP::Post.new(uri.request_uri, headers)
88
- query = URI.encode_www_form(form_encoded_params)
89
-
90
- response = http.request(request, query)
91
- response_json = JSON.parse(response.body)
92
- response_json['id']
93
- end
94
-
95
- # Submit Stripe credit card token to EasyPost.
96
- private_class_method def self.create_easypost_credit_card(referral_api_key, stripe_object_id, priority = 'primary')
97
- wrapped_params = {
98
- credit_card: {
99
- stripe_object_id: stripe_object_id,
100
- priority: priority,
101
- },
102
- }
103
- response = EasyPost.make_request(:post, '/beta/credit_cards', referral_api_key, wrapped_params)
104
- EasyPost::Util.convert_to_easypost_object(response, referral_api_key)
105
- end
106
110
  end
@@ -2,9 +2,41 @@
2
2
 
3
3
  # Billing class that users can manage their payment and fund
4
4
  class EasyPost::Billing < EasyPost::Resource
5
+ class << self
6
+ protected
7
+
8
+ # Get payment method info (type of the payment method and ID of the payment method)
9
+ def get_payment_method_info(primary_or_secondary)
10
+ payment_methods = EasyPost::Billing.retrieve_payment_methods
11
+ payment_method_map = {
12
+ 'primary' => 'primary_payment_method',
13
+ 'secondary' => 'secondary_payment_method',
14
+ }
15
+
16
+ payment_method_to_use = payment_method_map[primary_or_secondary]
17
+
18
+ error_string = 'The chosen payment method is not valid. Please try again.'
19
+ raise EasyPost::Error.new(error_string) if payment_methods[payment_method_to_use].nil?
20
+
21
+ payment_method_id = payment_methods[payment_method_to_use]['id']
22
+
23
+ unless payment_method_id.nil?
24
+ if payment_method_id.start_with?('card_')
25
+ endpoint = '/v2/credit_cards'
26
+ elsif payment_method_id.start_with?('bank_')
27
+ endpoint = '/v2/bank_accounts'
28
+ else
29
+ raise EasyPost::Error.new(error_string)
30
+ end
31
+ end
32
+
33
+ [endpoint, payment_method_id]
34
+ end
35
+ end
36
+
5
37
  # Fund your EasyPost wallet by charging your primary or secondary card on file.
6
38
  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)
39
+ payment_info = get_payment_method_info(primary_or_secondary.downcase)
8
40
  endpoint = payment_info[0]
9
41
  payment_id = payment_info[1]
10
42
 
@@ -17,7 +49,7 @@ class EasyPost::Billing < EasyPost::Resource
17
49
 
18
50
  # Delete a payment method.
19
51
  def self.delete_payment_method(primary_or_secondary, api_key = nil)
20
- payment_info = EasyPost::Billing.get_payment_method_info(primary_or_secondary.downcase)
52
+ payment_info = get_payment_method_info(primary_or_secondary.downcase)
21
53
  endpoint = payment_info[0]
22
54
  payment_id = payment_info[1]
23
55
 
@@ -37,32 +69,4 @@ class EasyPost::Billing < EasyPost::Resource
37
69
 
38
70
  EasyPost::Util.convert_to_easypost_object(response, api_key)
39
71
  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
72
  end
@@ -52,7 +52,7 @@ EasyPost::Connection = Struct.new(:uri, :config, keyword_init: true) do
52
52
  request.body = JSON.dump(EasyPost::Util.objects_to_ids(body)) if body
53
53
 
54
54
  EasyPost.default_headers.each_pair { |h, v| request[h] = v }
55
- request['Authorization'] = EasyPost.authorization(api_key) if api_key
55
+ request['Authorization'] = EasyPost.authorization(api_key)
56
56
 
57
57
  response = connection.request(request)
58
58
  response_is_json = response['Content-Type'] ? response['Content-Type'].start_with?('application/json') : false
@@ -6,7 +6,8 @@ class EasyPost::Error < StandardError
6
6
 
7
7
  # Initialize a new EasyPost Error
8
8
  def initialize(message = nil, status = nil, code = nil, errors = nil, http_body = nil)
9
- @message = message
9
+ # message should be a string but can sometimes incorrectly come back as an array
10
+ @message = message.is_a?(Array) ? message.join(', ') : message
10
11
  @status = status
11
12
  @code = code
12
13
  @errors = errors
@@ -2,6 +2,59 @@
2
2
 
3
3
  # Referral objects are User objects created from a Partner user.
4
4
  class EasyPost::Referral < EasyPost::Resource
5
+ class << self
6
+ protected
7
+
8
+ # Retrieve EasyPost's Stripe public API key.
9
+ def retrieve_easypost_stripe_api_key
10
+ response = EasyPost.make_request(:get, '/beta/partners/stripe_public_key', @api_key)
11
+ response['public_key']
12
+ end
13
+
14
+ # Get credit card token from Stripe.
15
+ def create_stripe_token(number, expiration_month, expiration_year,
16
+ cvc, easypost_stripe_token)
17
+ headers = {
18
+ # This Stripe endpoint only accepts URL form encoded bodies.
19
+ Authorization: "Bearer #{easypost_stripe_token}",
20
+ 'Content-type': 'application/x-www-form-urlencoded',
21
+ }
22
+
23
+ credit_card_hash = {
24
+ card: {
25
+ number: number,
26
+ exp_month: expiration_month,
27
+ exp_year: expiration_year,
28
+ cvc: cvc,
29
+ },
30
+ }
31
+
32
+ form_encoded_params = EasyPost::Util.form_encode_params(credit_card_hash)
33
+
34
+ uri = URI.parse('https://api.stripe.com/v1/tokens')
35
+ http = Net::HTTP.new(uri.host, uri.port)
36
+ http.use_ssl = true
37
+ request = Net::HTTP::Post.new(uri.request_uri, headers)
38
+ query = URI.encode_www_form(form_encoded_params)
39
+
40
+ response = http.request(request, query)
41
+ response_json = JSON.parse(response.body)
42
+ response_json['id']
43
+ end
44
+
45
+ # Submit Stripe credit card token to EasyPost.
46
+ def create_easypost_credit_card(referral_api_key, stripe_object_id, priority = 'primary')
47
+ wrapped_params = {
48
+ credit_card: {
49
+ stripe_object_id: stripe_object_id,
50
+ priority: priority,
51
+ },
52
+ }
53
+ response = EasyPost.make_request(:post, '/beta/credit_cards', referral_api_key, wrapped_params)
54
+ EasyPost::Util.convert_to_easypost_object(response, referral_api_key)
55
+ end
56
+ end
57
+
5
58
  # Create a referral user. This function requires the Partner User's API key.
6
59
  def self.create(params = {}, api_key = nil)
7
60
  response = EasyPost.make_request(:post, '/v2/referral_customers', api_key, { user: params })
@@ -46,53 +99,4 @@ class EasyPost::Referral < EasyPost::Resource
46
99
  response = create_easypost_credit_card(referral_api_key, stripe_credit_card_token, priority)
47
100
  EasyPost::Util.convert_to_easypost_object(response, referral_api_key)
48
101
  end
49
-
50
- # Retrieve EasyPost's Stripe public API key.
51
- private_class_method def self.retrieve_easypost_stripe_api_key
52
- response = EasyPost.make_request(:get, '/beta/partners/stripe_public_key', @api_key)
53
- response['public_key']
54
- end
55
-
56
- # Get credit card token from Stripe.
57
- private_class_method def self.create_stripe_token(number, expiration_month, expiration_year,
58
- cvc, easypost_stripe_token)
59
- headers = {
60
- # This Stripe endpoint only accepts URL form encoded bodies.
61
- Authorization: "Bearer #{easypost_stripe_token}",
62
- 'Content-type': 'application/x-www-form-urlencoded',
63
- }
64
-
65
- credit_card_hash = {
66
- card: {
67
- number: number,
68
- exp_month: expiration_month,
69
- exp_year: expiration_year,
70
- cvc: cvc,
71
- },
72
- }
73
-
74
- form_encoded_params = EasyPost::Util.form_encode_params(credit_card_hash)
75
-
76
- uri = URI.parse('https://api.stripe.com/v1/tokens')
77
- http = Net::HTTP.new(uri.host, uri.port)
78
- http.use_ssl = true
79
- request = Net::HTTP::Post.new(uri.request_uri, headers)
80
- query = URI.encode_www_form(form_encoded_params)
81
-
82
- response = http.request(request, query)
83
- response_json = JSON.parse(response.body)
84
- response_json['id']
85
- end
86
-
87
- # Submit Stripe credit card token to EasyPost.
88
- private_class_method def self.create_easypost_credit_card(referral_api_key, stripe_object_id, priority = 'primary')
89
- wrapped_params = {
90
- credit_card: {
91
- stripe_object_id: stripe_object_id,
92
- priority: priority,
93
- },
94
- }
95
- response = EasyPost.make_request(:post, '/beta/credit_cards', referral_api_key, wrapped_params)
96
- EasyPost::Util.convert_to_easypost_object(response, referral_api_key)
97
- end
98
102
  end
@@ -48,6 +48,7 @@ class EasyPost::Resource < EasyPost::EasyPostObject
48
48
  EasyPost::Util.convert_to_easypost_object(response, api_key)
49
49
  end
50
50
 
51
+ # Filter all results based on criteria.
51
52
  def self.each(filters = {}, api_key = EasyPost.api_key, &block)
52
53
  return to_enum(:each, filters, api_key) unless block_given?
53
54
 
data/lib/easypost.rb CHANGED
@@ -26,6 +26,7 @@ require 'easypost/event'
26
26
  require 'easypost/insurance'
27
27
  require 'easypost/order'
28
28
  require 'easypost/parcel'
29
+ require 'easypost/payment_method' # deprecated
29
30
  require 'easypost/pickup_rate'
30
31
  require 'easypost/pickup'
31
32
  require 'easypost/postage_label'
@@ -133,7 +134,7 @@ module EasyPost
133
134
  end
134
135
 
135
136
  def self.parse_response(status:, body:, json:)
136
- if status >= 400
137
+ if status < 200 || status >= 300
137
138
  error = JSON.parse(body)['error']
138
139
 
139
140
  raise EasyPost::Error.new(
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: easypost
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.8.0
4
+ version: 4.8.1
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-09-21 00:00:00.000000000 Z
11
+ date: 2022-10-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: brakeman