easypost 4.8.1 → 5.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitattributes +5 -0
- data/.github/workflows/ci.yml +34 -5
- data/.gitignore +27 -20
- data/CHANGELOG.md +56 -0
- data/Makefile +30 -11
- data/README.md +111 -45
- data/UPGRADE_GUIDE.md +119 -0
- data/VERSION +1 -1
- data/easypost.gemspec +14 -10
- data/lib/easypost/client.rb +178 -0
- data/lib/easypost/connection.rb +2 -4
- data/lib/easypost/constants.rb +15 -0
- data/lib/easypost/errors/api/api_error.rb +108 -0
- data/lib/easypost/errors/api/bad_request_error.rb +6 -0
- data/lib/easypost/errors/api/connection_error.rb +6 -0
- data/lib/easypost/errors/api/external_api_error.rb +18 -0
- data/lib/easypost/errors/api/forbidden_error.rb +6 -0
- data/lib/easypost/errors/api/gateway_timeout_error.rb +6 -0
- data/lib/easypost/errors/api/internal_server_error.rb +6 -0
- data/lib/easypost/errors/api/invalid_request_error.rb +6 -0
- data/lib/easypost/errors/api/method_not_allowed_error.rb +6 -0
- data/lib/easypost/errors/api/not_found_error.rb +6 -0
- data/lib/easypost/errors/api/payment_error.rb +6 -0
- data/lib/easypost/errors/api/proxy_error.rb +6 -0
- data/lib/easypost/errors/api/rate_limit_error.rb +6 -0
- data/lib/easypost/errors/api/redirect_error.rb +6 -0
- data/lib/easypost/errors/api/retry_error.rb +6 -0
- data/lib/easypost/errors/api/service_unavailable_error.rb +6 -0
- data/lib/easypost/errors/api/ssl_error.rb +6 -0
- data/lib/easypost/errors/api/timeout_error.rb +6 -0
- data/lib/easypost/errors/api/unauthorized_error.rb +6 -0
- data/lib/easypost/errors/api/unknown_api_error.rb +6 -0
- data/lib/easypost/errors/easy_post_error.rb +7 -0
- data/lib/easypost/errors/end_of_pagination_error.rb +7 -0
- data/lib/easypost/errors/filtering_error.rb +4 -0
- data/lib/easypost/errors/invalid_object_error.rb +4 -0
- data/lib/easypost/errors/invalid_parameter_error.rb +11 -0
- data/lib/easypost/errors/missing_parameter_error.rb +9 -0
- data/lib/easypost/errors/signature_verification_error.rb +4 -0
- data/lib/easypost/errors.rb +32 -0
- data/lib/easypost/hooks/request_context.rb +16 -0
- data/lib/easypost/hooks/response_context.rb +23 -0
- data/lib/easypost/hooks.rb +34 -0
- data/lib/easypost/http_client.rb +117 -0
- data/lib/easypost/internal_utilities.rb +66 -0
- data/lib/easypost/models/address.rb +5 -0
- data/lib/easypost/models/api_key.rb +5 -0
- data/lib/easypost/models/base.rb +58 -0
- data/lib/easypost/models/batch.rb +5 -0
- data/lib/easypost/models/brand.rb +5 -0
- data/lib/easypost/{carbon_offset.rb → models/carbon_offset.rb} +1 -1
- data/lib/easypost/models/carrier_account.rb +5 -0
- data/lib/easypost/models/customs_info.rb +5 -0
- data/lib/easypost/models/customs_item.rb +5 -0
- data/lib/easypost/models/end_shipper.rb +5 -0
- data/lib/easypost/models/error.rb +21 -0
- data/lib/easypost/models/event.rb +5 -0
- data/lib/easypost/{insurance.rb → models/insurance.rb} +1 -1
- data/lib/easypost/models/order.rb +9 -0
- data/lib/easypost/models/parcel.rb +5 -0
- data/lib/easypost/models/payload.rb +5 -0
- data/lib/easypost/models/payment_method.rb +5 -0
- data/lib/easypost/models/pickup.rb +9 -0
- data/lib/easypost/{pickup_rate.rb → models/pickup_rate.rb} +1 -1
- data/lib/easypost/{postage_label.rb → models/postage_label.rb} +1 -1
- data/lib/easypost/models/rate.rb +5 -0
- data/lib/easypost/models/referral.rb +5 -0
- data/lib/easypost/{refund.rb → models/refund.rb} +1 -1
- data/lib/easypost/models/report.rb +5 -0
- data/lib/easypost/models/scan_form.rb +6 -0
- data/lib/easypost/models/shipment.rb +10 -0
- data/lib/easypost/{tax_identifier.rb → models/tax_identifier.rb} +1 -1
- data/lib/easypost/models/tracker.rb +5 -0
- data/lib/easypost/models/user.rb +5 -0
- data/lib/easypost/models/webhook.rb +6 -0
- data/lib/easypost/models.rb +35 -0
- data/lib/easypost/services/address.rb +50 -0
- data/lib/easypost/services/api_key.rb +8 -0
- data/lib/easypost/services/base.rb +27 -0
- data/lib/easypost/services/batch.rb +53 -0
- data/lib/easypost/services/beta_rate.rb +12 -0
- data/lib/easypost/services/beta_referral_customer.rb +40 -0
- data/lib/easypost/services/billing.rb +75 -0
- data/lib/easypost/services/carrier_account.rb +44 -0
- data/lib/easypost/services/carrier_metadata.rb +22 -0
- data/lib/easypost/services/customs_info.rb +17 -0
- data/lib/easypost/services/customs_item.rb +15 -0
- data/lib/easypost/services/end_shipper.rb +31 -0
- data/lib/easypost/services/event.rb +32 -0
- data/lib/easypost/services/insurance.rb +26 -0
- data/lib/easypost/services/order.rb +30 -0
- data/lib/easypost/services/parcel.rb +16 -0
- data/lib/easypost/services/pickup.rb +40 -0
- data/lib/easypost/services/rate.rb +8 -0
- data/lib/easypost/services/referral_customer.rb +103 -0
- data/lib/easypost/services/refund.rb +26 -0
- data/lib/easypost/services/report.rb +42 -0
- data/lib/easypost/services/scan_form.rb +25 -0
- data/lib/easypost/services/shipment.rb +106 -0
- data/lib/easypost/services/tracker.rb +38 -0
- data/lib/easypost/services/user.rb +66 -0
- data/lib/easypost/services/webhook.rb +34 -0
- data/lib/easypost/services.rb +32 -0
- data/lib/easypost/util.rb +116 -161
- data/lib/easypost/utilities/constants.rb +5 -0
- data/lib/easypost/utilities/json.rb +23 -0
- data/lib/easypost/utilities/static_mapper.rb +73 -0
- data/lib/easypost/utilities/system.rb +36 -0
- data/lib/easypost.rb +14 -136
- metadata +177 -65
- data/.rubocop.yml +0 -11
- data/CODE_OF_CONDUCT.md +0 -16
- data/CONTRIBUTING.md +0 -47
- data/SECURITY.md +0 -7
- data/SUPPORT.md +0 -3
- data/easycop.yml +0 -180
- data/lib/easypost/address.rb +0 -40
- data/lib/easypost/api_key.rb +0 -5
- data/lib/easypost/batch.rb +0 -50
- data/lib/easypost/beta/end_shipper.rb +0 -44
- data/lib/easypost/beta/referral.rb +0 -110
- data/lib/easypost/beta.rb +0 -7
- data/lib/easypost/billing.rb +0 -72
- data/lib/easypost/brand.rb +0 -13
- data/lib/easypost/carrier_account.rb +0 -9
- data/lib/easypost/carrier_type.rb +0 -5
- data/lib/easypost/customs_info.rb +0 -9
- data/lib/easypost/customs_item.rb +0 -9
- data/lib/easypost/end_shipper.rb +0 -24
- data/lib/easypost/error.rb +0 -32
- data/lib/easypost/event.rb +0 -11
- data/lib/easypost/object.rb +0 -171
- data/lib/easypost/order.rb +0 -37
- data/lib/easypost/parcel.rb +0 -9
- data/lib/easypost/payment_method.rb +0 -11
- data/lib/easypost/pickup.rb +0 -37
- data/lib/easypost/rate.rb +0 -9
- data/lib/easypost/referral.rb +0 -102
- data/lib/easypost/report.rb +0 -23
- data/lib/easypost/resource.rb +0 -106
- data/lib/easypost/scan_form.rb +0 -11
- data/lib/easypost/shipment.rb +0 -155
- data/lib/easypost/tracker.rb +0 -12
- data/lib/easypost/user.rb +0 -71
- data/lib/easypost/webhook.rb +0 -57
@@ -0,0 +1,53 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class EasyPost::Services::Batch < EasyPost::Services::Service
|
4
|
+
MODEL_CLASS = EasyPost::Models::Batch
|
5
|
+
|
6
|
+
# Create a Batch.
|
7
|
+
def create(params = {})
|
8
|
+
wrapped_params = { batch: params }
|
9
|
+
|
10
|
+
@client.make_request(:post, 'batches', MODEL_CLASS, wrapped_params)
|
11
|
+
end
|
12
|
+
|
13
|
+
# Create and buy a batch in one call.
|
14
|
+
def create_and_buy(params = {})
|
15
|
+
wrapped_params = { batch: params }
|
16
|
+
|
17
|
+
@client.make_request(:post, 'batches/create_and_buy', MODEL_CLASS, wrapped_params)
|
18
|
+
end
|
19
|
+
|
20
|
+
def all(params = {})
|
21
|
+
@client.make_request(:get, 'batches', EasyPost::Models::ApiKey, params)
|
22
|
+
end
|
23
|
+
|
24
|
+
# Retrieve a Batch
|
25
|
+
def retrieve(id)
|
26
|
+
@client.make_request(:get, "batches/#{id}", MODEL_CLASS)
|
27
|
+
end
|
28
|
+
|
29
|
+
# Buy a Batch.
|
30
|
+
def buy(id, params = {})
|
31
|
+
@client.make_request(:post, "batches/#{id}/buy", MODEL_CLASS, params)
|
32
|
+
end
|
33
|
+
|
34
|
+
# Convert the label format of a Batch.
|
35
|
+
def label(id, params = {})
|
36
|
+
@client.make_request(:post, "batches/#{id}/label", MODEL_CLASS, params)
|
37
|
+
end
|
38
|
+
|
39
|
+
# Remove Shipments from a Batch.
|
40
|
+
def remove_shipments(id, params = {})
|
41
|
+
@client.make_request(:post, "batches/#{id}/remove_shipments", MODEL_CLASS, params)
|
42
|
+
end
|
43
|
+
|
44
|
+
# Add Shipments to a Batch.
|
45
|
+
def add_shipments(id, params = {})
|
46
|
+
@client.make_request(:post, "batches/#{id}/add_shipments", MODEL_CLASS, params)
|
47
|
+
end
|
48
|
+
|
49
|
+
# Create a ScanForm for a Batch.
|
50
|
+
def create_scan_form(id, params = {})
|
51
|
+
@client.make_request(:post, "batches/#{id}/scan_form", MODEL_CLASS, params)
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class EasyPost::Services::BetaRate < EasyPost::Services::Service
|
4
|
+
# Retrieve a list of stateless rates.
|
5
|
+
def retrieve_stateless_rates(params = {})
|
6
|
+
wrapped_params = {
|
7
|
+
shipment: params,
|
8
|
+
}
|
9
|
+
|
10
|
+
@client.make_request(:post, 'rates', EasyPost::Models::Rate, wrapped_params, 'beta').rates
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class EasyPost::Services::BetaReferralCustomer < EasyPost::Services::Service
|
4
|
+
# Add a Stripe payment method to a ReferralCustomer Customer. This function requires the ReferralCustomer Customer's API key.
|
5
|
+
def add_payment_method(stripe_customer_id, payment_method_reference, priority = 'primary')
|
6
|
+
wrapped_params = {
|
7
|
+
payment_method: {
|
8
|
+
stripe_customer_id: stripe_customer_id,
|
9
|
+
payment_method_reference: payment_method_reference,
|
10
|
+
priority: priority.downcase,
|
11
|
+
},
|
12
|
+
}
|
13
|
+
@client.make_request(
|
14
|
+
:post,
|
15
|
+
'referral_customers/payment_method',
|
16
|
+
EasyPost::Models::EasyPostObject,
|
17
|
+
wrapped_params,
|
18
|
+
'beta',
|
19
|
+
)
|
20
|
+
end
|
21
|
+
|
22
|
+
# Refund a ReferralCustomer Customer's wallet by a specified amount. Refund will be issued to the user's original payment method.
|
23
|
+
# This function requires the ReferralCustomer Customer's API key.
|
24
|
+
def refund_by_amount(amount)
|
25
|
+
params = {
|
26
|
+
refund_amount: amount,
|
27
|
+
}
|
28
|
+
@client.make_request(:post, 'referral_customers/refunds', EasyPost::Models::EasyPostObject, params, 'beta')
|
29
|
+
# noinspection RubyMismatchedReturnType
|
30
|
+
end
|
31
|
+
|
32
|
+
# Refund a ReferralCustomer Customer's wallet for a specified payment log entry. Refund will be issued to the user's original payment method.
|
33
|
+
# This function requires the ReferralCustomer Customer's API key.
|
34
|
+
def refund_by_payment_log(payment_log_id)
|
35
|
+
params = {
|
36
|
+
payment_log_id: payment_log_id,
|
37
|
+
}
|
38
|
+
@client.make_request(:post, 'referral_customers/refunds', EasyPost::Models::EasyPostObject, params, 'beta')
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'easypost/constants'
|
4
|
+
|
5
|
+
class EasyPost::Services::Billing < EasyPost::Services::Service
|
6
|
+
# Get payment method info (type of the payment method and ID of the payment method)
|
7
|
+
def self.get_payment_method_info(priority)
|
8
|
+
payment_methods = EasyPost::Services::Billing.retrieve_payment_methods
|
9
|
+
payment_method_map = {
|
10
|
+
'primary' => 'primary_payment_method',
|
11
|
+
'secondary' => 'secondary_payment_method',
|
12
|
+
}
|
13
|
+
|
14
|
+
payment_method_to_use = payment_method_map[priority]
|
15
|
+
|
16
|
+
error_string = EasyPost::Constants::INVALID_PAYMENT_METHOD
|
17
|
+
suggestion = "Please use a valid payment method: #{payment_method_map.keys.join(', ')}"
|
18
|
+
if payment_methods[payment_method_to_use].nil?
|
19
|
+
raise EasyPost::Errors::InvalidParameterError.new(
|
20
|
+
error_string,
|
21
|
+
suggestion,
|
22
|
+
)
|
23
|
+
end
|
24
|
+
|
25
|
+
payment_method_id = payment_methods[payment_method_to_use]['id']
|
26
|
+
|
27
|
+
unless payment_method_id.nil?
|
28
|
+
if payment_method_id.start_with?('card_')
|
29
|
+
endpoint = '/v2/credit_cards'
|
30
|
+
elsif payment_method_id.start_with?('bank_')
|
31
|
+
endpoint = '/v2/bank_accounts'
|
32
|
+
else
|
33
|
+
raise EasyPost::Errors::InvalidObjectError.new(error_string)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
[endpoint, payment_method_id]
|
38
|
+
end
|
39
|
+
|
40
|
+
# Fund your EasyPost wallet by charging your primary or secondary card on file.
|
41
|
+
def fund_wallet(amount, priority = 'primary')
|
42
|
+
payment_info = EasyPost::Services::Billing.get_payment_method_info(priority.downcase)
|
43
|
+
endpoint = payment_info[0]
|
44
|
+
payment_id = payment_info[1]
|
45
|
+
|
46
|
+
wrapped_params = { amount: amount }
|
47
|
+
@client.make_request(:post, "#{endpoint}/#{payment_id}/charges", EasyPost::Models::EasyPostObject, wrapped_params)
|
48
|
+
|
49
|
+
# Return true if succeeds, an error will be thrown if it fails
|
50
|
+
true
|
51
|
+
end
|
52
|
+
|
53
|
+
# Delete a payment method.
|
54
|
+
def delete_payment_method(priority)
|
55
|
+
payment_info = EasyPost::Services::Billing.get_payment_method_info(priority.downcase)
|
56
|
+
endpoint = payment_info[0]
|
57
|
+
payment_id = payment_info[1]
|
58
|
+
|
59
|
+
@client.make_request(:delete, "#{endpoint}/#{payment_id}")
|
60
|
+
|
61
|
+
# Return true if succeeds, an error will be thrown if it fails
|
62
|
+
true
|
63
|
+
end
|
64
|
+
|
65
|
+
# Retrieve all payment methods.
|
66
|
+
def retrieve_payment_methods
|
67
|
+
response = @client.make_request(:get, '/v2/payment_methods')
|
68
|
+
|
69
|
+
if response['id'].nil?
|
70
|
+
raise EasyPost::Errors::InvalidObjectError.new(EasyPost::Constants::NO_PAYMENT_METHODS)
|
71
|
+
end
|
72
|
+
|
73
|
+
response
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class EasyPost::Services::CarrierAccount < EasyPost::Services::Service
|
4
|
+
CUSTOM_WORKFLOW_CARRIER_TYPES = %w[UpsAccount FedexAccount].freeze
|
5
|
+
MODEL_CLASS = EasyPost::Models::CarrierAccount
|
6
|
+
|
7
|
+
# Create a carrier account
|
8
|
+
def create(params = {})
|
9
|
+
wrapped_params = { carrier_account: params }
|
10
|
+
|
11
|
+
# For UPS and FedEx the endpoint is different
|
12
|
+
create_url = if CUSTOM_WORKFLOW_CARRIER_TYPES.include?(params[:type])
|
13
|
+
'carrier_accounts/register'
|
14
|
+
else
|
15
|
+
'carrier_accounts'
|
16
|
+
end
|
17
|
+
|
18
|
+
@client.make_request(:post, create_url, MODEL_CLASS, wrapped_params)
|
19
|
+
end
|
20
|
+
|
21
|
+
# Retrieve a carrier account
|
22
|
+
def retrieve(id)
|
23
|
+
@client.make_request(:get, "carrier_accounts/#{id}", MODEL_CLASS)
|
24
|
+
end
|
25
|
+
|
26
|
+
# Retrieve all carrier accounts
|
27
|
+
def all(params = {})
|
28
|
+
@client.make_request(:get, 'carrier_accounts', MODEL_CLASS, params)
|
29
|
+
end
|
30
|
+
|
31
|
+
# Update a carrier account
|
32
|
+
def update(id, params = {})
|
33
|
+
wrapped_params = { carrier_account: params }
|
34
|
+
@client.make_request(:put, "carrier_accounts/#{id}", MODEL_CLASS, wrapped_params)
|
35
|
+
end
|
36
|
+
|
37
|
+
# Delete a carrier account
|
38
|
+
def delete(id)
|
39
|
+
@client.make_request(:delete, "carrier_accounts/#{id}")
|
40
|
+
|
41
|
+
# Return true if succeeds, an error will be thrown if it fails
|
42
|
+
true
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class EasyPost::Services::CarrierMetadata < EasyPost::Services::Service
|
4
|
+
# Retrieve metadata for carrier(s).
|
5
|
+
def retrieve(carriers = [], types = [])
|
6
|
+
path = '/metadata/carriers?'
|
7
|
+
|
8
|
+
params = {}
|
9
|
+
|
10
|
+
if carriers.length.positive?
|
11
|
+
params[:carriers] = carriers.join(',')
|
12
|
+
end
|
13
|
+
|
14
|
+
if types.length.positive?
|
15
|
+
params[:types] = types.join(',')
|
16
|
+
end
|
17
|
+
|
18
|
+
path += URI.encode_www_form(params)
|
19
|
+
|
20
|
+
@client.make_request(:get, path, EasyPost::Models::EasyPostObject, params).carriers
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class EasyPost::Services::CustomsInfo < EasyPost::Services::Service
|
4
|
+
MODEL_CLASS = EasyPost::Models::CustomsInfo
|
5
|
+
|
6
|
+
# Create a CustomsInfo object
|
7
|
+
def create(params)
|
8
|
+
wrapped_params = { customs_info: params }
|
9
|
+
|
10
|
+
@client.make_request(:post, 'customs_infos', MODEL_CLASS, wrapped_params)
|
11
|
+
end
|
12
|
+
|
13
|
+
# Retrieve a CustomsInfo object
|
14
|
+
def retrieve(id)
|
15
|
+
@client.make_request(:get, "customs_infos/#{id}", MODEL_CLASS)
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class EasyPost::Services::CustomsItem < EasyPost::Services::Service
|
4
|
+
MODEL_CLASS = EasyPost::Models::CustomsItem
|
5
|
+
|
6
|
+
# Create a CustomsItem object
|
7
|
+
def create(params)
|
8
|
+
@client.make_request(:post, 'customs_items', MODEL_CLASS, params)
|
9
|
+
end
|
10
|
+
|
11
|
+
# Retrieve a CustomsItem object
|
12
|
+
def retrieve(id)
|
13
|
+
@client.make_request(:get, "customs_items/#{id}", MODEL_CLASS)
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class EasyPost::Services::EndShipper < EasyPost::Services::Service
|
4
|
+
MODEL_CLASS = EasyPost::Models::EndShipper
|
5
|
+
|
6
|
+
# Create an EndShipper object.
|
7
|
+
def create(params = {})
|
8
|
+
wrapped_params = { address: params }
|
9
|
+
|
10
|
+
@client.make_request(:post, 'end_shippers', MODEL_CLASS, wrapped_params)
|
11
|
+
end
|
12
|
+
|
13
|
+
# Retrieve an EndShipper object.
|
14
|
+
def retrieve(id)
|
15
|
+
@client.make_request(:get, "end_shippers/#{id}", MODEL_CLASS)
|
16
|
+
end
|
17
|
+
|
18
|
+
# Retrieve all EndShipper objects.
|
19
|
+
def all(params = {})
|
20
|
+
@client.make_request(:get, 'end_shippers', MODEL_CLASS, params)
|
21
|
+
end
|
22
|
+
|
23
|
+
# Updates an EndShipper object. This requires all parameters to be set.
|
24
|
+
def update(id, params)
|
25
|
+
wrapped_params = { address: params }
|
26
|
+
|
27
|
+
@client.make_request(:put, "end_shippers/#{id}", MODEL_CLASS, wrapped_params)
|
28
|
+
end
|
29
|
+
|
30
|
+
# TODO: Add support for getting the next page of end shippers when the API supports it.
|
31
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
class EasyPost::Services::Event < EasyPost::Services::Service
|
6
|
+
MODEL_CLASS = EasyPost::Models::Event
|
7
|
+
|
8
|
+
# Retrieve an Event object
|
9
|
+
def retrieve(id)
|
10
|
+
@client.make_request(:get, "events/#{id}", MODEL_CLASS)
|
11
|
+
end
|
12
|
+
|
13
|
+
# Retrieve all Event objects
|
14
|
+
def all(params = {})
|
15
|
+
@client.make_request(:get, 'events', MODEL_CLASS, params)
|
16
|
+
end
|
17
|
+
|
18
|
+
# Retrieve all payloads for an event.
|
19
|
+
def retrieve_all_payloads(event_id)
|
20
|
+
@client.make_request(:get, "events/#{event_id}/payloads", EasyPost::Models::Payload)
|
21
|
+
end
|
22
|
+
|
23
|
+
# Retrieve a specific payload for an event.
|
24
|
+
def retrieve_payload(event_id, payload_id)
|
25
|
+
@client.make_request(:get, "events/#{event_id}/payloads/#{payload_id}", EasyPost::Models::Payload)
|
26
|
+
end
|
27
|
+
|
28
|
+
# Get the next page of events.
|
29
|
+
def get_next_page(collection, page_size = nil)
|
30
|
+
get_next_page_helper(collection, collection.events, 'events', MODEL_CLASS, page_size)
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class EasyPost::Services::Insurance < EasyPost::Services::Service
|
4
|
+
MODEL_CLASS = EasyPost::Models::Insurance
|
5
|
+
|
6
|
+
# Create an Insurance object
|
7
|
+
def create(params = {})
|
8
|
+
wrapped_params = { insurance: params }
|
9
|
+
@client.make_request(:post, 'insurances', MODEL_CLASS, wrapped_params)
|
10
|
+
end
|
11
|
+
|
12
|
+
# Retrieve an Insurance object
|
13
|
+
def retrieve(id)
|
14
|
+
@client.make_request(:get, "insurances/#{id}", MODEL_CLASS)
|
15
|
+
end
|
16
|
+
|
17
|
+
# Retrieve all Insurance objects
|
18
|
+
def all(params = {})
|
19
|
+
@client.make_request(:get, 'insurances', MODEL_CLASS, params)
|
20
|
+
end
|
21
|
+
|
22
|
+
# Get the next page of insurances.
|
23
|
+
def get_next_page(collection, page_size = nil)
|
24
|
+
get_next_page_helper(collection, collection.insurances, 'insurances', MODEL_CLASS, page_size)
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class EasyPost::Services::Order < EasyPost::Services::Service
|
4
|
+
MODEL_CLASS = EasyPost::Models::Order
|
5
|
+
|
6
|
+
# Create an Order object
|
7
|
+
def create(params = {})
|
8
|
+
wrapped_params = { order: params }
|
9
|
+
@client.make_request(:post, 'orders', MODEL_CLASS, wrapped_params)
|
10
|
+
end
|
11
|
+
|
12
|
+
# Retrieve an Order object
|
13
|
+
def retrieve(id)
|
14
|
+
@client.make_request(:get, "orders/#{id}", MODEL_CLASS)
|
15
|
+
end
|
16
|
+
|
17
|
+
# Retrieve new rates for an Order object
|
18
|
+
def get_rates(id, params = {})
|
19
|
+
@client.make_request(:get, "orders/#{id}/rates", MODEL_CLASS, params)
|
20
|
+
end
|
21
|
+
|
22
|
+
# Buy an Order object
|
23
|
+
def buy(id, params = {})
|
24
|
+
if params.instance_of?(EasyPost::Models::Rate)
|
25
|
+
params = { carrier: params[:carrier], service: params[:service] }
|
26
|
+
end
|
27
|
+
|
28
|
+
@client.make_request(:post, "orders/#{id}/buy", MODEL_CLASS, params)
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class EasyPost::Services::Parcel < EasyPost::Services::Service
|
4
|
+
MODEL_CLASS = EasyPost::Models::Parcel
|
5
|
+
|
6
|
+
# Create a Parcel object
|
7
|
+
def create(params = {})
|
8
|
+
wrapped_params = { parcel: params }
|
9
|
+
@client.make_request(:post, 'parcels', MODEL_CLASS, wrapped_params)
|
10
|
+
end
|
11
|
+
|
12
|
+
# Retrieve a Parcel object
|
13
|
+
def retrieve(id)
|
14
|
+
@client.make_request(:get, "parcels/#{id}", MODEL_CLASS)
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class EasyPost::Services::Pickup < EasyPost::Services::Service
|
4
|
+
MODEL_CLASS = EasyPost::Models::Pickup
|
5
|
+
|
6
|
+
# Create a Pickup object
|
7
|
+
def create(params = {})
|
8
|
+
wrapped_params = { pickup: params }
|
9
|
+
@client.make_request(:post, 'pickups', MODEL_CLASS, wrapped_params)
|
10
|
+
end
|
11
|
+
|
12
|
+
# Retrieve a Pickup object
|
13
|
+
def retrieve(id)
|
14
|
+
@client.make_request(:get, "pickups/#{id}", MODEL_CLASS)
|
15
|
+
end
|
16
|
+
|
17
|
+
# Retrieve all Pickup objects
|
18
|
+
def all(params = {})
|
19
|
+
@client.make_request(:get, 'pickups', MODEL_CLASS, params)
|
20
|
+
end
|
21
|
+
|
22
|
+
# Buy a Pickup
|
23
|
+
def buy(id, params = {})
|
24
|
+
if params.instance_of?(EasyPost::Models::PickupRate)
|
25
|
+
params = { carrier: params[:carrier], service: params[:service] }
|
26
|
+
end
|
27
|
+
|
28
|
+
@client.make_request(:post, "pickups/#{id}/buy", MODEL_CLASS, params)
|
29
|
+
end
|
30
|
+
|
31
|
+
# Cancel a Pickup
|
32
|
+
def cancel(id, params = {})
|
33
|
+
@client.make_request(:post, "pickups/#{id}/cancel", MODEL_CLASS, params)
|
34
|
+
end
|
35
|
+
|
36
|
+
# Get next page of Pickups
|
37
|
+
def get_next_page(collection, page_size = nil)
|
38
|
+
get_next_page_helper(collection, collection.pickups, 'pickups', MODEL_CLASS, page_size)
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,103 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class EasyPost::Services::ReferralCustomer < EasyPost::Services::Service
|
4
|
+
MODEL_CLASS = EasyPost::Models::User
|
5
|
+
|
6
|
+
# Create a referral customer. This function requires the Partner User's API key.
|
7
|
+
def create(params = {})
|
8
|
+
@client.make_request(:post, 'referral_customers', MODEL_CLASS, { user: params })
|
9
|
+
end
|
10
|
+
|
11
|
+
# Update a referral customer. This function requires the Partner User's API key.
|
12
|
+
def update_email(user_id, email)
|
13
|
+
wrapped_params = {
|
14
|
+
user: {
|
15
|
+
email: email,
|
16
|
+
},
|
17
|
+
}
|
18
|
+
@client.make_request(:put, "referral_customers/#{user_id}", MODEL_CLASS, wrapped_params)
|
19
|
+
|
20
|
+
# return true if API succeeds, else an error is throw if it fails.
|
21
|
+
true
|
22
|
+
end
|
23
|
+
|
24
|
+
# Retrieve a list of referral customers. This function requires the Partner User's API key.
|
25
|
+
def all(params = {})
|
26
|
+
@client.make_request(:get, 'referral_customers', MODEL_CLASS, params)
|
27
|
+
end
|
28
|
+
|
29
|
+
# Get the next page of referral customers.
|
30
|
+
def get_next_page(collection, page_size = nil)
|
31
|
+
get_next_page_helper(collection, collection.referral_customers, 'referral_customers', MODEL_CLASS, page_size)
|
32
|
+
end
|
33
|
+
|
34
|
+
# Add credit card to a referral customer. This function requires the ReferralCustomer Customer's API key.
|
35
|
+
def add_credit_card(referral_api_key, number, expiration_month, expiration_year, cvc, priority = 'primary')
|
36
|
+
easypost_stripe_api_key = retrieve_easypost_stripe_api_key
|
37
|
+
|
38
|
+
begin
|
39
|
+
stripe_credit_card_token = create_stripe_token(
|
40
|
+
number,
|
41
|
+
expiration_month,
|
42
|
+
expiration_year,
|
43
|
+
cvc,
|
44
|
+
easypost_stripe_api_key,
|
45
|
+
)
|
46
|
+
rescue StandardError
|
47
|
+
raise EasyPost::Errors::ExternalApiError.new(EasyPost::Constants::STRIPE_CARD_CREATE_FAILED)
|
48
|
+
end
|
49
|
+
|
50
|
+
create_easypost_credit_card(referral_api_key, stripe_credit_card_token, priority)
|
51
|
+
end
|
52
|
+
|
53
|
+
private
|
54
|
+
|
55
|
+
# Retrieve EasyPost's Stripe public API key.
|
56
|
+
def retrieve_easypost_stripe_api_key
|
57
|
+
response = @client.make_request(:get, 'partners/stripe_public_key', EasyPost::Models::EasyPostObject, nil, 'beta')
|
58
|
+
response['public_key']
|
59
|
+
end
|
60
|
+
|
61
|
+
# Get credit card token from Stripe.
|
62
|
+
def create_stripe_token(number, expiration_month, expiration_year,
|
63
|
+
cvc, easypost_stripe_token)
|
64
|
+
headers = {
|
65
|
+
# This Stripe endpoint only accepts URL form encoded bodies.
|
66
|
+
Authorization: "Bearer #{easypost_stripe_token}",
|
67
|
+
'Content-type': 'application/x-www-form-urlencoded',
|
68
|
+
}
|
69
|
+
|
70
|
+
credit_card_hash = {
|
71
|
+
card: {
|
72
|
+
number: number,
|
73
|
+
exp_month: expiration_month,
|
74
|
+
exp_year: expiration_year,
|
75
|
+
cvc: cvc,
|
76
|
+
},
|
77
|
+
}
|
78
|
+
|
79
|
+
form_encoded_params = EasyPost::InternalUtilities.form_encode_params(credit_card_hash)
|
80
|
+
|
81
|
+
uri = URI.parse('https://api.stripe.com/v1/tokens')
|
82
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
83
|
+
http.use_ssl = true
|
84
|
+
request = Net::HTTP::Post.new(uri.request_uri, headers)
|
85
|
+
query = URI.encode_www_form(form_encoded_params)
|
86
|
+
|
87
|
+
response = http.request(request, query)
|
88
|
+
response_json = JSON.parse(response.body)
|
89
|
+
response_json['id']
|
90
|
+
end
|
91
|
+
|
92
|
+
# Submit Stripe credit card token to EasyPost.
|
93
|
+
def create_easypost_credit_card(referral_api_key, stripe_object_id, priority = 'primary')
|
94
|
+
wrapped_params = {
|
95
|
+
credit_card: {
|
96
|
+
stripe_object_id: stripe_object_id,
|
97
|
+
priority: priority,
|
98
|
+
},
|
99
|
+
}
|
100
|
+
referral_client = EasyPost::Client.new(api_key: referral_api_key)
|
101
|
+
referral_client.make_request(:post, 'credit_cards', EasyPost::Models::EasyPostObject, wrapped_params, 'beta')
|
102
|
+
end
|
103
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class EasyPost::Services::Refund < EasyPost::Services::Service
|
4
|
+
MODEL_CLASS = EasyPost::Models::Refund
|
5
|
+
|
6
|
+
# Create a Refund object
|
7
|
+
def create(params = {})
|
8
|
+
wrapped_params = { refund: params }
|
9
|
+
@client.make_request(:post, 'refunds', MODEL_CLASS, wrapped_params)
|
10
|
+
end
|
11
|
+
|
12
|
+
# Retrieve a Refund object
|
13
|
+
def retrieve(id)
|
14
|
+
@client.make_request(:get, "refunds/#{id}", MODEL_CLASS)
|
15
|
+
end
|
16
|
+
|
17
|
+
# Retrieve all Refund objects
|
18
|
+
def all(params = {})
|
19
|
+
@client.make_request(:get, 'refunds', MODEL_CLASS, params)
|
20
|
+
end
|
21
|
+
|
22
|
+
# Get the next page of refunds
|
23
|
+
def get_next_page(collection, page_size = nil)
|
24
|
+
get_next_page_helper(collection, collection.refunds, 'refunds', MODEL_CLASS, page_size)
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class EasyPost::Services::Report < EasyPost::Services::Service
|
4
|
+
MODEL_CLASS = EasyPost::Models::Report
|
5
|
+
|
6
|
+
# Create a Report
|
7
|
+
def create(params = {})
|
8
|
+
unless params[:type]
|
9
|
+
raise ArgumentError, "Missing 'type' parameter in params" # TODO: replace the error in the error-handling overhaul PR
|
10
|
+
end
|
11
|
+
|
12
|
+
type = params.delete(:type)
|
13
|
+
url = "reports/#{type}"
|
14
|
+
|
15
|
+
@client.make_request(:post, url, MODEL_CLASS, params)
|
16
|
+
end
|
17
|
+
|
18
|
+
# Retrieve a Report
|
19
|
+
def retrieve(id)
|
20
|
+
@client.make_request(:get, "reports/#{id}", MODEL_CLASS)
|
21
|
+
end
|
22
|
+
|
23
|
+
# Retrieve all Report objects
|
24
|
+
def all(params = {})
|
25
|
+
unless params[:type]
|
26
|
+
raise ArgumentError, "Missing 'type' parameter in params" # TODO: replace the error in the error-handling overhaul PR
|
27
|
+
end
|
28
|
+
|
29
|
+
type = params.delete(:type)
|
30
|
+
url = "reports/#{type}"
|
31
|
+
|
32
|
+
response = @client.make_request(:get, url, MODEL_CLASS, params)
|
33
|
+
response.define_singleton_method(:type) { type }
|
34
|
+
response
|
35
|
+
end
|
36
|
+
|
37
|
+
# Get next page of Report objects
|
38
|
+
def get_next_page(collection, page_size = nil)
|
39
|
+
url = "reports/#{collection.type}"
|
40
|
+
get_next_page_helper(collection, collection.reports, url, MODEL_CLASS, page_size)
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class EasyPost::Services::ScanForm < EasyPost::Services::Service
|
4
|
+
MODEL_CLASS = EasyPost::Models::ScanForm
|
5
|
+
|
6
|
+
# Create a ScanForm.
|
7
|
+
def create(params = {})
|
8
|
+
@client.make_request(:post, 'scan_forms', MODEL_CLASS, params)
|
9
|
+
end
|
10
|
+
|
11
|
+
# Retrieve a ScanForm.
|
12
|
+
def retrieve(id)
|
13
|
+
@client.make_request(:get, "scan_forms/#{id}", MODEL_CLASS)
|
14
|
+
end
|
15
|
+
|
16
|
+
# Retrieve a list of ScanForms
|
17
|
+
def all(params = {})
|
18
|
+
@client.make_request(:get, 'scan_forms', MODEL_CLASS, params)
|
19
|
+
end
|
20
|
+
|
21
|
+
# Get the next page of ScanForms.
|
22
|
+
def get_next_page(collection, page_size = nil)
|
23
|
+
get_next_page_helper(collection, collection.scan_forms, 'scan_forms', MODEL_CLASS, page_size)
|
24
|
+
end
|
25
|
+
end
|