easypost 4.13.0 → 5.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (141) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ci.yml +22 -1
  3. data/.gitignore +9 -11
  4. data/.rubocop.yml +6 -1
  5. data/CHANGELOG.md +21 -0
  6. data/Makefile +1 -1
  7. data/README.md +45 -38
  8. data/UPGRADE_GUIDE.md +119 -0
  9. data/VERSION +1 -1
  10. data/easycop.yml +3 -3
  11. data/easypost.gemspec +12 -10
  12. data/lib/easypost/client.rb +129 -0
  13. data/lib/easypost/connection.rb +2 -4
  14. data/lib/easypost/constants.rb +15 -0
  15. data/lib/easypost/errors/api/api_error.rb +106 -0
  16. data/lib/easypost/errors/api/connection_error.rb +6 -0
  17. data/lib/easypost/errors/api/external_api_error.rb +18 -0
  18. data/lib/easypost/errors/api/forbidden_error.rb +6 -0
  19. data/lib/easypost/errors/api/gateway_timeout_error.rb +6 -0
  20. data/lib/easypost/errors/api/internal_server_error.rb +6 -0
  21. data/lib/easypost/errors/api/invalid_request_error.rb +6 -0
  22. data/lib/easypost/errors/api/method_not_allowed_error.rb +6 -0
  23. data/lib/easypost/errors/api/not_found_error.rb +6 -0
  24. data/lib/easypost/errors/api/payment_error.rb +6 -0
  25. data/lib/easypost/errors/api/proxy_error.rb +6 -0
  26. data/lib/easypost/errors/api/rate_limit_error.rb +6 -0
  27. data/lib/easypost/errors/api/redirect_error.rb +6 -0
  28. data/lib/easypost/errors/api/retry_error.rb +6 -0
  29. data/lib/easypost/errors/api/service_unavailable_error.rb +6 -0
  30. data/lib/easypost/errors/api/ssl_error.rb +6 -0
  31. data/lib/easypost/errors/api/timeout_error.rb +6 -0
  32. data/lib/easypost/errors/api/unauthorized_error.rb +6 -0
  33. data/lib/easypost/errors/api/unknown_api_error.rb +6 -0
  34. data/lib/easypost/errors/easy_post_error.rb +7 -0
  35. data/lib/easypost/errors/end_of_pagination_error.rb +7 -0
  36. data/lib/easypost/errors/filtering_error.rb +4 -0
  37. data/lib/easypost/errors/invalid_object_error.rb +4 -0
  38. data/lib/easypost/errors/invalid_parameter_error.rb +11 -0
  39. data/lib/easypost/errors/missing_parameter_error.rb +9 -0
  40. data/lib/easypost/errors/signature_verification_error.rb +4 -0
  41. data/lib/easypost/errors.rb +31 -0
  42. data/lib/easypost/http_client.rb +62 -0
  43. data/lib/easypost/internal_utilities.rb +66 -0
  44. data/lib/easypost/models/address.rb +5 -0
  45. data/lib/easypost/models/api_key.rb +5 -0
  46. data/lib/easypost/models/base.rb +58 -0
  47. data/lib/easypost/models/batch.rb +5 -0
  48. data/lib/easypost/models/brand.rb +5 -0
  49. data/lib/easypost/{carbon_offset.rb → models/carbon_offset.rb} +1 -1
  50. data/lib/easypost/models/carrier_account.rb +5 -0
  51. data/lib/easypost/models/customs_info.rb +5 -0
  52. data/lib/easypost/models/customs_item.rb +5 -0
  53. data/lib/easypost/models/end_shipper.rb +5 -0
  54. data/lib/easypost/models/error.rb +21 -0
  55. data/lib/easypost/models/event.rb +5 -0
  56. data/lib/easypost/models/insurance.rb +6 -0
  57. data/lib/easypost/models/order.rb +9 -0
  58. data/lib/easypost/models/parcel.rb +5 -0
  59. data/lib/easypost/{payload.rb → models/payload.rb} +1 -1
  60. data/lib/easypost/models/payment_method.rb +5 -0
  61. data/lib/easypost/models/pickup.rb +9 -0
  62. data/lib/easypost/{pickup_rate.rb → models/pickup_rate.rb} +1 -1
  63. data/lib/easypost/{postage_label.rb → models/postage_label.rb} +1 -1
  64. data/lib/easypost/models/rate.rb +5 -0
  65. data/lib/easypost/models/referral.rb +5 -0
  66. data/lib/easypost/models/refund.rb +5 -0
  67. data/lib/easypost/models/report.rb +5 -0
  68. data/lib/easypost/models/scan_form.rb +6 -0
  69. data/lib/easypost/models/shipment.rb +10 -0
  70. data/lib/easypost/{tax_identifier.rb → models/tax_identifier.rb} +1 -1
  71. data/lib/easypost/models/tracker.rb +5 -0
  72. data/lib/easypost/models/user.rb +5 -0
  73. data/lib/easypost/models/webhook.rb +6 -0
  74. data/lib/easypost/models.rb +35 -0
  75. data/lib/easypost/services/address.rb +50 -0
  76. data/lib/easypost/services/api_key.rb +8 -0
  77. data/lib/easypost/services/base.rb +27 -0
  78. data/lib/easypost/services/batch.rb +53 -0
  79. data/lib/easypost/services/beta_rate.rb +12 -0
  80. data/lib/easypost/services/beta_referral_customer.rb +40 -0
  81. data/lib/easypost/services/billing.rb +75 -0
  82. data/lib/easypost/services/carrier_account.rb +44 -0
  83. data/lib/easypost/services/carrier_metadata.rb +22 -0
  84. data/lib/easypost/services/customs_info.rb +15 -0
  85. data/lib/easypost/services/customs_item.rb +15 -0
  86. data/lib/easypost/services/end_shipper.rb +31 -0
  87. data/lib/easypost/services/event.rb +32 -0
  88. data/lib/easypost/services/insurance.rb +26 -0
  89. data/lib/easypost/services/order.rb +30 -0
  90. data/lib/easypost/services/parcel.rb +16 -0
  91. data/lib/easypost/services/pickup.rb +40 -0
  92. data/lib/easypost/services/rate.rb +8 -0
  93. data/lib/easypost/services/referral_customer.rb +103 -0
  94. data/lib/easypost/services/refund.rb +26 -0
  95. data/lib/easypost/services/report.rb +42 -0
  96. data/lib/easypost/services/scan_form.rb +25 -0
  97. data/lib/easypost/services/shipment.rb +106 -0
  98. data/lib/easypost/services/tracker.rb +38 -0
  99. data/lib/easypost/services/user.rb +66 -0
  100. data/lib/easypost/services/webhook.rb +34 -0
  101. data/lib/easypost/services.rb +32 -0
  102. data/lib/easypost/util.rb +80 -187
  103. data/lib/easypost/utilities/constants.rb +5 -0
  104. data/lib/easypost/utilities/json.rb +23 -0
  105. data/lib/easypost/utilities/static_mapper.rb +73 -0
  106. data/lib/easypost/utilities/system.rb +36 -0
  107. data/lib/easypost.rb +12 -138
  108. metadata +147 -64
  109. data/lib/easypost/address.rb +0 -55
  110. data/lib/easypost/api_key.rb +0 -5
  111. data/lib/easypost/batch.rb +0 -52
  112. data/lib/easypost/beta/end_shipper.rb +0 -44
  113. data/lib/easypost/beta/payment_refund.rb +0 -5
  114. data/lib/easypost/beta/rate.rb +0 -14
  115. data/lib/easypost/beta/referral.rb +0 -158
  116. data/lib/easypost/beta.rb +0 -8
  117. data/lib/easypost/billing.rb +0 -72
  118. data/lib/easypost/brand.rb +0 -13
  119. data/lib/easypost/carrier_account.rb +0 -26
  120. data/lib/easypost/carrier_type.rb +0 -5
  121. data/lib/easypost/customs_info.rb +0 -9
  122. data/lib/easypost/customs_item.rb +0 -9
  123. data/lib/easypost/end_shipper.rb +0 -26
  124. data/lib/easypost/error.rb +0 -32
  125. data/lib/easypost/event.rb +0 -38
  126. data/lib/easypost/insurance.rb +0 -20
  127. data/lib/easypost/object.rb +0 -171
  128. data/lib/easypost/order.rb +0 -37
  129. data/lib/easypost/parcel.rb +0 -9
  130. data/lib/easypost/payment_method.rb +0 -12
  131. data/lib/easypost/pickup.rb +0 -47
  132. data/lib/easypost/rate.rb +0 -9
  133. data/lib/easypost/referral.rb +0 -117
  134. data/lib/easypost/refund.rb +0 -19
  135. data/lib/easypost/report.rb +0 -44
  136. data/lib/easypost/resource.rb +0 -124
  137. data/lib/easypost/scan_form.rb +0 -26
  138. data/lib/easypost/shipment.rb +0 -186
  139. data/lib/easypost/tracker.rb +0 -43
  140. data/lib/easypost/user.rb +0 -74
  141. data/lib/easypost/webhook.rb +0 -57
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  # Webhook Event Payloads are triggered by changes in objects you've created via the API.
4
- class EasyPost::Payload < EasyPost::Resource
4
+ class EasyPost::Models::Payload < EasyPost::Models::EasyPostObject
5
5
  end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ # PaymentMethod objects represent a payment method of a user.
4
+ class EasyPost::Models::PaymentMethod < EasyPost::Models::EasyPostObject
5
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ # The Pickup object allows you to schedule a pickup from your carrier from your customer's residence or place of business.
4
+ class EasyPost::Models::Pickup < EasyPost::Models::EasyPostObject
5
+ # Get the lowest rate of a Pickup (can exclude by having `'!'` as the first element of your optional filter lists).
6
+ def lowest_rate(carriers = [], services = [])
7
+ EasyPost::Util.get_lowest_object_rate(self, carriers, services, 'pickup_rates')
8
+ end
9
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  # A PickupRate is the rate available for a Pickup.
4
- class EasyPost::PickupRate < EasyPost::Resource
4
+ class EasyPost::Models::PickupRate < EasyPost::Models::EasyPostObject
5
5
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  # PostageLabel is the object containing details about the shipping label.
4
- class EasyPost::PostageLabel < EasyPost::Resource
4
+ class EasyPost::Models::PostageLabel < EasyPost::Models::EasyPostObject
5
5
  end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ # A Rate object contains all the details about the rate of a Shipment.
4
+ class EasyPost::Models::Rate < EasyPost::Models::EasyPostObject
5
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ # ReferralCustomer objects are User objects created from a Partner user.
4
+ class EasyPost::Models::Referral < EasyPost::Models::EasyPostObject
5
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ # The Refund object contains details about the Refund of a Shipment.
4
+ class EasyPost::Models::Refund < EasyPost::Models::EasyPostObject
5
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ # A Report contains a csv that is a log of all the objects created within a certain time frame.
4
+ class EasyPost::Models::Report < EasyPost::Models::EasyPostObject
5
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ # A ScanForm can be created to speed up and simplify the carrier pickup process. The ScanForm is one document that can
4
+ # be scanned to mark all included tracking codes as "Accepted for Shipment" by the carrier.
5
+ class EasyPost::Models::ScanForm < EasyPost::Models::EasyPostObject
6
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ # The workhorse of the EasyPost API, a Shipment is made up of a "to" and "from" Address, the Parcel
4
+ # being shipped, and any customs forms required for international deliveries.
5
+ class EasyPost::Models::Shipment < EasyPost::Models::EasyPostObject
6
+ # Get the lowest rate of a Shipment (can exclude by having `'!'` as the first element of your optional filter lists).
7
+ def lowest_rate(carriers = [], services = [])
8
+ EasyPost::Util.get_lowest_object_rate(self, carriers, services)
9
+ end
10
+ end
@@ -2,5 +2,5 @@
2
2
 
3
3
  # TaxIdentifiers are identifying numbers or IDs that are used to charge a specific party when
4
4
  # dealing with the importing or exporting of good across international borders.
5
- class EasyPost::TaxIdentifier < EasyPost::Resource
5
+ class EasyPost::Models::TaxIdentifier < EasyPost::Models::EasyPostObject
6
6
  end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ # A Tracker object contains all of the tracking information for a package.
4
+ class EasyPost::Models::Tracker < EasyPost::Models::EasyPostObject
5
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ # The User object can be used to manage your own account and to create child accounts.
4
+ class EasyPost::Models::User < EasyPost::Models::EasyPostObject
5
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Each Webhook contains the url which EasyPost will notify whenever an object in our system updates. Several types of objects are processed
4
+ # asynchronously in the EasyPost system, so whenever an object updates, an Event is sent via HTTP POST to each configured webhook URL.
5
+ class EasyPost::Models::Webhook < EasyPost::Models::EasyPostObject
6
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EasyPost::Models
4
+ end
5
+
6
+ require_relative 'models/base' # Must be imported first before the rest of child models
7
+ require_relative 'models/address'
8
+ require_relative 'models/api_key'
9
+ require_relative 'models/batch'
10
+ require_relative 'models/brand'
11
+ require_relative 'models/carbon_offset'
12
+ require_relative 'models/carrier_account'
13
+ require_relative 'models/customs_info'
14
+ require_relative 'models/customs_item'
15
+ require_relative 'models/end_shipper'
16
+ require_relative 'models/error'
17
+ require_relative 'models/event'
18
+ require_relative 'models/insurance'
19
+ require_relative 'models/order'
20
+ require_relative 'models/parcel'
21
+ require_relative 'models/payload'
22
+ require_relative 'models/payment_method'
23
+ require_relative 'models/pickup_rate'
24
+ require_relative 'models/pickup'
25
+ require_relative 'models/postage_label'
26
+ require_relative 'models/rate'
27
+ require_relative 'models/referral'
28
+ require_relative 'models/refund'
29
+ require_relative 'models/report'
30
+ require_relative 'models/scan_form'
31
+ require_relative 'models/shipment'
32
+ require_relative 'models/tax_identifier'
33
+ require_relative 'models/tracker'
34
+ require_relative 'models/user'
35
+ require_relative 'models/webhook'
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ class EasyPost::Services::Address < EasyPost::Services::Service
4
+ MODEL_CLASS = EasyPost::Models::Address
5
+
6
+ # Create an address.
7
+ def create(params = {})
8
+ address = params.reject { |k, _| [:verify, :verify_strict].include?(k) }
9
+
10
+ wrapped_params = { address: address }
11
+
12
+ if params[:verify]
13
+ wrapped_params[:verify] = params[:verify]
14
+ end
15
+
16
+ if params[:verify_strict]
17
+ wrapped_params[:verify_strict] = params[:verify_strict]
18
+ end
19
+
20
+ @client.make_request(:post, 'addresses', MODEL_CLASS, params)
21
+ end
22
+
23
+ # Create and verify an Address in one call.
24
+ def create_and_verify(params = {})
25
+ wrapped_params = {}
26
+ wrapped_params[:address] = params
27
+
28
+ @client.make_request(:post, 'addresses/create_and_verify', MODEL_CLASS, wrapped_params).address
29
+ end
30
+
31
+ # Verify an Address.
32
+ def verify(id)
33
+ @client.make_request(:get, "addresses/#{id}/verify", MODEL_CLASS).address
34
+ end
35
+
36
+ # Retrieve an Address.
37
+ def retrieve(id)
38
+ @client.make_request(:get, "addresses/#{id}", MODEL_CLASS)
39
+ end
40
+
41
+ # Retrieve all Addresses.
42
+ def all(filters = {})
43
+ @client.make_request(:get, 'addresses', MODEL_CLASS, filters)
44
+ end
45
+
46
+ # Get the next page of addresses.
47
+ def get_next_page(collection, page_size = nil)
48
+ get_next_page_helper(collection, collection.addresses, 'addresses', MODEL_CLASS, page_size)
49
+ end
50
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ class EasyPost::Services::ApiKey < EasyPost::Services::Service
4
+ # Retrieve all api keys.
5
+ def all
6
+ @client.make_request(:get, 'api_keys', EasyPost::Models::ApiKey)
7
+ end
8
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ # The base class for all services in the library.
4
+ class EasyPost::Services::Service
5
+ def initialize(client)
6
+ @client = client
7
+ end
8
+
9
+ protected
10
+
11
+ # Get next page of an object collection
12
+ def get_next_page_helper(collection, current_page_items, endpoint, cls, page_size = nil)
13
+ has_more = collection.has_more || false
14
+ unless !has_more || current_page_items.nil? || current_page_items.empty?
15
+ params = {}
16
+ params[:before_id] = current_page_items.last.id
17
+ unless page_size.nil?
18
+ params[:page_size] = page_size
19
+ end
20
+
21
+ @client.make_request(:get, endpoint, cls, params)
22
+ end
23
+
24
+ # issue with getting the next page
25
+ raise EasyPost::Errors::EndOfPaginationError.new
26
+ end
27
+ end
@@ -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,15 @@
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
+ @client.make_request(:post, 'customs_infos', MODEL_CLASS, params)
9
+ end
10
+
11
+ # Retrieve a CustomsInfo object
12
+ def retrieve(id)
13
+ @client.make_request(:get, "customs_infos/#{id}", MODEL_CLASS)
14
+ end
15
+ 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