Skip to content

Commit deffee3

Browse files
authored
Update carrier create and update function to account UPS endpoints (#305)
1 parent 94d0a69 commit deffee3

7 files changed

+538
-32
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# CHANGELOG
22

3+
## Next Release
4+
5+
- Routes `UpsAccount`, `UpsMailInnovationsAccount`, and `UpsSurepostAccount` create/update requests to the new `/ups_oauth_registrations` endpoint
6+
- Starting `2024-08-05`, UPS accounts will require a new payload to register or update. See [UPS OAuth 2.0 Update](https://support.easypost.com/hc/en-us/articles/26635027512717-UPS-OAuth-2-0-Update?utm_medium=email&_hsenc=p2ANqtz-96MmFtWICOzy9sKRbbcZSiMovZSrY3MSX1_bgY9N3f9yLVfWQdLhjAGq-SmNcOnDIS6GYhZ0OApjDBrGkKyLLMx1z6_TFOVp6-wllhEFQINrkuRuc&_hsmi=313130292&utm_content=313130292&utm_source=hs_email) for more details
7+
38
## v6.2.0 (2024-04-10)
49

510
- Fix payment method funding and deletion failures due to undetermined payment method type

Makefile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,13 @@ docs:
2424
install-styleguide: | update-examples-submodule
2525
sh examples/symlink_directory_files.sh examples/style_guides/ruby .
2626

27+
## init-examples-submodule - Initialize the examples submodule
28+
init-examples-submodule:
29+
git submodule init
30+
git submodule update
31+
2732
## install - Install globally from source
28-
install: | update-examples-submodule
33+
install: | init-examples-submodule
2934
bundle install
3035

3136
## lint - Lint the project

lib/easypost/services/carrier_account.rb

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
# frozen_string_literal: true
22

33
class EasyPost::Services::CarrierAccount < EasyPost::Services::Service
4-
CUSTOM_WORKFLOW_CARRIER_TYPES = %w[UpsAccount FedexAccount FedexSmartpostAccount].freeze
4+
CUSTOM_WORKFLOW_CARRIER_TYPES = %w[FedexAccount FedexSmartpostAccount].freeze
5+
UPS_OAUTH_CARRIER_ACCOUNT_TYPES = %w[UpsAccount UpsMailInnovationsAccount UpsSurepostAccount].freeze
56
MODEL_CLASS = EasyPost::Models::CarrierAccount
67

78
# Create a carrier account
89
def create(params = {})
9-
wrapped_params = { carrier_account: params }
10+
carrier_account_type = params[:type]
11+
wrapped_params = { select_top_layer_key(carrier_account_type).to_sym => params }
1012

1113
# For UPS and FedEx the endpoint is different
12-
create_url = if CUSTOM_WORKFLOW_CARRIER_TYPES.include?(params[:type])
14+
create_url = if CUSTOM_WORKFLOW_CARRIER_TYPES.include?(carrier_account_type)
1315
'carrier_accounts/register'
16+
elsif UPS_OAUTH_CARRIER_ACCOUNT_TYPES.include?(carrier_account_type)
17+
'ups_oauth_registrations'
1418
else
1519
'carrier_accounts'
1620
end
@@ -33,8 +37,14 @@ def all(params = {})
3337

3438
# Update a carrier account
3539
def update(id, params = {})
36-
wrapped_params = { carrier_account: params }
37-
response = @client.make_request(:put, "carrier_accounts/#{id}", wrapped_params)
40+
carrier_account = retrieve(id)
41+
wrapped_params = { select_top_layer_key(carrier_account[:type]).to_sym => params }
42+
update_url = if UPS_OAUTH_CARRIER_ACCOUNT_TYPES.include?(params[:type])
43+
'ups_oauth_registrations/'
44+
else
45+
'carrier_accounts/'
46+
end
47+
response = @client.make_request(:put, "#{update_url}#{id}", wrapped_params)
3848

3949
EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS)
4050
end
@@ -46,4 +56,15 @@ def delete(id)
4656
# Return true if succeeds, an error will be thrown if it fails
4757
true
4858
end
59+
60+
private
61+
62+
# Select the top-layer key for the carrier account creation/update request based on the carrier type.
63+
def select_top_layer_key(carrier_account_type)
64+
if UPS_OAUTH_CARRIER_ACCOUNT_TYPES.include?(carrier_account_type)
65+
'ups_oauth_registrations'
66+
else
67+
'carrier_account'
68+
end
69+
end
4970
end

spec/carrier_account_spec.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,17 @@
1717
client.carrier_account.delete(carrier_account.id)
1818
end
1919

20+
it 'creates an UPS account' do
21+
carrier_account = client.carrier_account.create({ type: 'UpsAccount', account_number: '123456789' })
22+
23+
expect(carrier_account).to be_an_instance_of(EasyPost::Models::CarrierAccount)
24+
expect(carrier_account.id).to match('ca_')
25+
expect(carrier_account.type).to eq('UpsAccount')
26+
27+
# Remove the carrier account once we have tested it so we don't pollute the account with test accounts
28+
client.carrier_account.delete(carrier_account.id)
29+
end
30+
2031
it 'sends FedexAccount to the correct endpoint' do
2132
allow(client).to receive(:make_request).with(
2233
:post, 'carrier_accounts/register',
@@ -65,6 +76,19 @@
6576
# Remove the carrier account once we have tested it so we don't pollute the account with test accounts
6677
client.carrier_account.delete(carrier_account.id)
6778
end
79+
80+
it 'updates an ups account' do
81+
ups_account = client.carrier_account.create({ type: 'UpsAccount', account_number: '123456789' })
82+
83+
updated_ups_account = client.carrier_account.update(ups_account.id, account_number: '987654321')
84+
85+
expect(updated_ups_account).to be_an_instance_of(EasyPost::Models::CarrierAccount)
86+
expect(updated_ups_account.id).to match('ca_')
87+
expect(updated_ups_account.type).to eq('UpsAccount')
88+
89+
# Remove the carrier account once we have tested it so we don't pollute the account with test accounts
90+
client.carrier_account.delete(ups_account.id)
91+
end
6892
end
6993

7094
describe '.delete' do

spec/cassettes/carrier_account/EasyPost_Services_CarrierAccount_create_creates_an_UPS_account.yml

Lines changed: 129 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)