-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathcarrier_account.rb
70 lines (57 loc) · 2.42 KB
/
carrier_account.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# frozen_string_literal: true
class EasyPost::Services::CarrierAccount < EasyPost::Services::Service
CUSTOM_WORKFLOW_CARRIER_TYPES = %w[FedexAccount FedexSmartpostAccount].freeze
UPS_OAUTH_CARRIER_ACCOUNT_TYPES = %w[UpsAccount UpsMailInnovationsAccount UpsSurepostAccount].freeze
MODEL_CLASS = EasyPost::Models::CarrierAccount # :nodoc:
# Create a carrier account
def create(params = {})
carrier_account_type = params[:type]
wrapped_params = { select_top_layer_key(carrier_account_type).to_sym => params }
# For UPS and FedEx the endpoint is different
create_url = if CUSTOM_WORKFLOW_CARRIER_TYPES.include?(carrier_account_type)
'carrier_accounts/register'
elsif UPS_OAUTH_CARRIER_ACCOUNT_TYPES.include?(carrier_account_type)
'ups_oauth_registrations'
else
'carrier_accounts'
end
response = @client.make_request(:post, create_url, wrapped_params)
EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS)
end
# Retrieve a carrier account
def retrieve(id)
response = @client.make_request(:get, "carrier_accounts/#{id}")
EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS)
end
# Retrieve all carrier accounts
def all(params = {})
get_all_helper('carrier_accounts', MODEL_CLASS, params)
end
# Update a carrier account
def update(id, params = {})
carrier_account = retrieve(id)
wrapped_params = { select_top_layer_key(carrier_account[:type]).to_sym => params }
update_url = if UPS_OAUTH_CARRIER_ACCOUNT_TYPES.include?(params[:type])
'ups_oauth_registrations/'
else
'carrier_accounts/'
end
response = @client.make_request(:put, "#{update_url}#{id}", wrapped_params)
EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS)
end
# Delete a carrier account
def delete(id)
@client.make_request(:delete, "carrier_accounts/#{id}")
# Return true if succeeds, an error will be thrown if it fails
true
end
private
# Select the top-layer key for the carrier account creation/update request based on the carrier type.
def select_top_layer_key(carrier_account_type)
if UPS_OAUTH_CARRIER_ACCOUNT_TYPES.include?(carrier_account_type)
'ups_oauth_registrations'
else
'carrier_account'
end
end
end