easypost 4.13.1 → 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 +19 -2
  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 -46
  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,47 +0,0 @@
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::Pickup < EasyPost::Resource
5
- # Buy a Pickup.
6
- def buy(params = {})
7
- if params.instance_of?(EasyPost::PickupRate)
8
- temp = params.clone
9
- params = {}
10
- params[:carrier] = temp.carrier
11
- params[:service] = temp.service
12
- end
13
-
14
- response = EasyPost.make_request(:post, "#{url}/buy", @api_key, params)
15
- refresh_from(response, @api_key)
16
-
17
- self
18
- end
19
-
20
- # Cancel a Pickup.
21
- def cancel(params = {})
22
- response = EasyPost.make_request(:post, "#{url}/cancel", @api_key, params)
23
- refresh_from(response, @api_key)
24
-
25
- self
26
- end
27
-
28
- # Get the lowest rate of a Pickup (can exclude by having `'!'` as the first element of your optional filter lists).
29
- def lowest_rate(carriers = [], services = [])
30
- EasyPost::Util.get_lowest_object_rate(self, carriers, services, 'pickup_rates')
31
- end
32
-
33
- # Get the next page of pickups.
34
- def self.get_next_page(collection, page_size = nil)
35
- get_next_page_exec(method(:all), collection, collection.pickups, page_size)
36
- end
37
-
38
- # Build the next page parameters.
39
- def self.build_next_page_params(_collection, current_page_items, page_size = nil)
40
- params = {}
41
- params[:before_id] = current_page_items.last.id
42
- unless page_size.nil?
43
- params[:page_size] = page_size
44
- end
45
- params
46
- end
47
- end
data/lib/easypost/rate.rb DELETED
@@ -1,9 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # A Rate object contains all the details about the rate of a Shipment.
4
- class EasyPost::Rate < EasyPost::Resource
5
- # Retrieving all Rate objects is not supported.
6
- def self.all(_filters = {}, _api_key = nil)
7
- raise NotImplementedError.new('Rate.all not implemented.')
8
- end
9
- end
@@ -1,117 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # Referral objects are User objects created from a Partner user.
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
-
58
- # Create a referral customer. This function requires the Partner User's API key.
59
- def self.create(params = {}, api_key = nil)
60
- response = EasyPost.make_request(:post, '/v2/referral_customers', api_key, { user: params })
61
- EasyPost::Util.convert_to_easypost_object(response, api_key)
62
- end
63
-
64
- # Update a referral customer. This function requires the Partner User's API key.
65
- def self.update_email(email, user_id, api_key = nil)
66
- wrapped_params = {
67
- user: {
68
- email: email,
69
- },
70
- }
71
- EasyPost.make_request(:put, "/v2/referral_customers/#{user_id}", api_key, wrapped_params)
72
-
73
- # return true if API succeeds, else an error is throw if it fails.
74
- true
75
- end
76
-
77
- # Retrieve a list of referral customers. This function requires the Partner User's API key.
78
- def self.all(params = {}, api_key = nil)
79
- response = EasyPost.make_request(:get, '/v2/referral_customers', api_key, params)
80
- EasyPost::Util.convert_to_easypost_object(response, api_key)
81
- end
82
-
83
- # Get the next page of referral customers.
84
- def self.get_next_page(collection, page_size = nil)
85
- get_next_page_exec(method(:all), collection, collection.referral_customers, page_size)
86
- end
87
-
88
- # Build the next page parameters.
89
- def self.build_next_page_params(_collection, current_page_items, page_size = nil)
90
- params = {}
91
- params[:before_id] = current_page_items.last.id
92
- unless page_size.nil?
93
- params[:page_size] = page_size
94
- end
95
- params
96
- end
97
-
98
- # Add credit card to a referral customer. This function requires the Referral Customer's API key.
99
- def self.add_credit_card(referral_api_key, number, expiration_month, expiration_year, cvc, priority = 'primary')
100
- easypost_stripe_api_key = retrieve_easypost_stripe_api_key
101
-
102
- begin
103
- stripe_credit_card_token = create_stripe_token(
104
- number,
105
- expiration_month,
106
- expiration_year,
107
- cvc,
108
- easypost_stripe_api_key,
109
- )
110
- rescue StandardError
111
- raise EasyPost::Error.new('Could not send card details to Stripe, please try again later.')
112
- end
113
-
114
- response = create_easypost_credit_card(referral_api_key, stripe_credit_card_token, priority)
115
- EasyPost::Util.convert_to_easypost_object(response, referral_api_key)
116
- end
117
- end
@@ -1,19 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # The Refund object contains details about the Refund of a Shipment.
4
- class EasyPost::Refund < EasyPost::Resource
5
- # Get the next page of refunds.
6
- def self.get_next_page(collection, page_size = nil)
7
- get_next_page_exec(method(:all), collection, collection.refunds, page_size)
8
- end
9
-
10
- # Build the next page parameters.
11
- def self.build_next_page_params(_collection, current_page_items, page_size = nil)
12
- params = {}
13
- params[:before_id] = current_page_items.last.id
14
- unless page_size.nil?
15
- params[:page_size] = page_size
16
- end
17
- params
18
- end
19
- end
@@ -1,44 +0,0 @@
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::Report < EasyPost::Resource
5
- # Create a Report.
6
- def self.create(params = {}, api_key = nil)
7
- url = "#{self.url}/#{params[:type]}"
8
-
9
- wrapped_params = {}
10
- wrapped_params[class_name.to_sym] = params
11
-
12
- response = EasyPost.make_request(:post, url, api_key, params)
13
- EasyPost::Util.convert_to_easypost_object(response, api_key)
14
- end
15
-
16
- # Retrieve a list of Report objects.
17
- def self.all(filters = {}, api_key = nil)
18
- url = "#{self.url}/#{filters[:type]}"
19
-
20
- response = EasyPost.make_request(:get, url, api_key, filters)
21
- collection = EasyPost::Util.convert_to_easypost_object(response, api_key)
22
-
23
- # Store the filters used to retrieve the collection.
24
- collection.refresh_from({ type: filters[:type] }, api_key)
25
-
26
- collection
27
- end
28
-
29
- # Get the next page of addresses.
30
- def self.get_next_page(collection, page_size = nil)
31
- get_next_page_exec(method(:all), collection, collection.reports, page_size)
32
- end
33
-
34
- # Build the next page parameters.
35
- def self.build_next_page_params(collection, current_page_items, page_size = nil)
36
- params = {}
37
- params[:before_id] = current_page_items.last.id
38
- unless page_size.nil?
39
- params[:page_size] = page_size
40
- end
41
- params[:type] = collection.type
42
- params
43
- end
44
- end
@@ -1,124 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # The Resource object is extended by each EasyPost object.
4
- class EasyPost::Resource < EasyPost::EasyPostObject
5
- extend Enumerable
6
-
7
- # The class name of an EasyPost object.
8
- def self.class_name
9
- camel = name.split('::')[-1]
10
- snake = camel[0..0] + camel[1..-1].gsub(/([A-Z])/, '_\1')
11
- snake.downcase
12
- end
13
-
14
- # The instance url of the Resource.
15
- def self.url
16
- if class_name == 'resource'
17
- raise NotImplementedError.new(
18
- 'Resource is an abstract class. You should perform actions on its subclasses (Address, Shipment, etc.)',
19
- )
20
- end
21
-
22
- if class_name[-1..-1] == 's' || class_name[-1..-1] == 'h'
23
- "/v2/#{CGI.escape(class_name.downcase)}es"
24
- else
25
- "/v2/#{CGI.escape(class_name.downcase)}s"
26
- end
27
- end
28
-
29
- # The url of the Resource.
30
- def url
31
- unless id
32
- raise EasyPost::Error.new("Could not determine which URL to request: #{self.class} instance has invalid ID: #{id.inspect}")
33
- end
34
-
35
- "#{self.class.url}/#{CGI.escape(id)}"
36
- end
37
-
38
- # Refresh an object from the API response.
39
- def refresh
40
- response = EasyPost.make_request(:get, url, @api_key, @retrieve_options)
41
- refresh_from(response, api_key)
42
- self
43
- end
44
-
45
- # Retrieve a list of EasyPost objects.
46
- def self.all(filters = {}, api_key = nil)
47
- response = EasyPost.make_request(:get, url, api_key, filters)
48
- EasyPost::Util.convert_to_easypost_object(response, api_key)
49
- end
50
-
51
- # Filter all results based on criteria.
52
- def self.each(filters = {}, api_key = EasyPost.api_key, &block)
53
- return to_enum(:each, filters, api_key) unless block_given?
54
-
55
- loop do
56
- page, has_more = all(filters, api_key).values
57
- last = page.each(&block).last
58
- break if page.empty? || !has_more
59
-
60
- filters[:before_id] = last.id
61
- end
62
- end
63
-
64
- # Retrieve an EasyPost object.
65
- def self.retrieve(id, api_key = nil)
66
- instance = new(id, api_key)
67
- instance.refresh
68
- instance
69
- end
70
-
71
- # Create an EasyPost object.
72
- def self.create(params = {}, api_key = nil)
73
- wrapped_params = {}
74
- wrapped_params[class_name.to_sym] = params
75
- response = EasyPost.make_request(:post, url, api_key, wrapped_params)
76
- EasyPost::Util.convert_to_easypost_object(response, api_key)
77
- end
78
-
79
- # Delete an EasyPost object.
80
- def delete
81
- response = EasyPost.make_request(:delete, url, @api_key)
82
- refresh_from(response, api_key)
83
- self
84
- end
85
-
86
- # Save (update) and EasyPost object.
87
- def save
88
- if @unsaved_values.length.positive?
89
- values = {}
90
- @unsaved_values.each { |k| values[k] = @values[k] }
91
-
92
- @unsaved_values.each do |key| # rubocop:disable Style/CombinableLoops
93
- value = values[key]
94
- if value.is_a?(EasyPost::EasyPostObject)
95
- values[key] = value.flatten_unsaved
96
- end
97
- end
98
-
99
- wrapped_params = { self.class.class_name => values }
100
-
101
- response = EasyPost.make_request(:patch, url, @api_key, wrapped_params)
102
- refresh_from(response, api_key)
103
- end
104
- self
105
- end
106
-
107
- # Build the next page parameters and call the next page function.
108
- def self.get_next_page_exec(api_func, collection, current_page_items, page_size = nil)
109
- has_more = collection.has_more || false
110
- unless !has_more || current_page_items.nil? || current_page_items.empty?
111
- # if no issues, then we can get the next page
112
- params = build_next_page_params(collection, current_page_items, page_size)
113
- return api_func.call(params)
114
- end
115
-
116
- # issue with getting the next page
117
- raise EasyPost::Error.new('There are no more pages to retrieve.')
118
- end
119
-
120
- # Build the next page parameters. This is a stub method that should be overridden by the child class.
121
- def self.build_next_page_params(_collection, _current_page_items, _page_size = nil)
122
- {}
123
- end
124
- end
@@ -1,26 +0,0 @@
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::ScanForm < EasyPost::Resource
6
- # Create a ScanForm.
7
- def self.create(params = {}, api_key = nil)
8
- response = EasyPost.make_request(:post, url, api_key, params)
9
- EasyPost::Util.convert_to_easypost_object(response, api_key)
10
- end
11
-
12
- # Get the next page of scan forms.
13
- def self.get_next_page(collection, page_size = nil)
14
- get_next_page_exec(method(:all), collection, collection.scan_forms, page_size)
15
- end
16
-
17
- # Build the next page parameters.
18
- def self.build_next_page_params(_collection, current_page_items, page_size = nil)
19
- params = {}
20
- params[:before_id] = current_page_items.last.id
21
- unless page_size.nil?
22
- params[:page_size] = page_size
23
- end
24
- params
25
- end
26
- end
@@ -1,186 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'set'
4
-
5
- # The workhorse of the EasyPost API, a Shipment is made up of a "to" and "from" Address, the Parcel
6
- # being shipped, and any customs forms required for international deliveries.
7
- class EasyPost::Shipment < EasyPost::Resource
8
- # Create a Shipment.
9
- def self.create(params = {}, api_key = nil, with_carbon_offset = false)
10
- wrapped_params = {
11
- shipment: params,
12
- carbon_offset: with_carbon_offset,
13
- }
14
-
15
- response = EasyPost.make_request(:post, url, api_key, wrapped_params)
16
- EasyPost::Util.convert_to_easypost_object(response, api_key)
17
- end
18
-
19
- # Regenerate the rates of a Shipment.
20
- def regenerate_rates(with_carbon_offset = false)
21
- params = {}
22
- params[:carbon_offset] = with_carbon_offset
23
-
24
- response = EasyPost.make_request(:post, "#{url}/rerate", @api_key, params)
25
- refresh_from(response, @api_key)
26
-
27
- self
28
- end
29
-
30
- # Get the SmartRates of a Shipment.
31
- def get_smartrates # rubocop:disable Naming/AccessorMethodName
32
- response = EasyPost.make_request(:get, "#{url}/smartrate", @api_key)
33
-
34
- response.fetch('result', [])
35
- end
36
-
37
- # Retrieve a list of Shipment objects.
38
- def self.all(filters = {}, api_key = nil)
39
- collection = super(filters, api_key)
40
-
41
- # Store the filters used to retrieve the collection.
42
- collection.refresh_from({ include_children: filters[:include_children], purchased: filters[:purchased] }, api_key)
43
-
44
- collection
45
- end
46
-
47
- # Get the next page of shipments.
48
- def self.get_next_page(collection, page_size = nil)
49
- get_next_page_exec(method(:all), collection, collection.shipments, page_size)
50
- end
51
-
52
- # Build the next page parameters.
53
- def self.build_next_page_params(collection, current_page_items, page_size = nil)
54
- params = {}
55
- params[:before_id] = current_page_items.last.id
56
- unless page_size.nil?
57
- params[:page_size] = page_size
58
- end
59
- unless collection.include_children.nil?
60
- params[:include_children] = collection.include_children
61
- end
62
- unless collection.purchased.nil?
63
- params[:purchased] = collection.purchased
64
- end
65
- params
66
- end
67
-
68
- # Buy a Shipment.
69
- def buy(params = {}, with_carbon_offset = false, end_shipper_id = nil)
70
- if params.instance_of?(EasyPost::Rate)
71
- temp = params.clone
72
- params = {}
73
- params[:rate] = temp
74
- end
75
-
76
- if params[:with_carbon_offset]
77
- params[:carbon_offset] = params[:with_carbon_offset]
78
- params.delete(:with_carbon_offset)
79
- else
80
- params[:carbon_offset] = with_carbon_offset
81
- end
82
-
83
- if end_shipper_id
84
- params[:end_shipper_id] = end_shipper_id
85
- end
86
-
87
- response = EasyPost.make_request(:post, "#{url}/buy", @api_key, params)
88
- refresh_from(response, @api_key)
89
-
90
- self
91
- end
92
-
93
- # Insure a Shipment.
94
- def insure(params = {})
95
- if params.is_a?(Integer) || params.is_a?(Float)
96
- temp = params.clone
97
- params = {}
98
- params[:amount] = temp
99
- end
100
-
101
- response = EasyPost.make_request(:post, "#{url}/insure", @api_key, params)
102
- refresh_from(response, @api_key)
103
-
104
- self
105
- end
106
-
107
- # Refund a Shipment.
108
- def refund(params = {})
109
- response = EasyPost.make_request(:post, "#{url}/refund", @api_key, params)
110
- refresh_from(response, @api_key)
111
-
112
- self
113
- end
114
-
115
- # Convert the label format of a Shipment.
116
- def label(params = {})
117
- if params.is_a?(String)
118
- temp = params.clone
119
- params = {}
120
- params[:file_format] = temp
121
- end
122
-
123
- response = EasyPost.make_request(:get, "#{url}/label", @api_key, params)
124
- refresh_from(response, @api_key)
125
-
126
- self
127
- end
128
-
129
- # Get the lowest rate of a Shipment (can exclude by having `'!'` as the first element of your optional filter lists).
130
- def lowest_rate(carriers = [], services = [])
131
- EasyPost::Util.get_lowest_object_rate(self, carriers, services)
132
- end
133
-
134
- # Get the lowest smartrate of a Shipment.
135
- def lowest_smartrate(delivery_days, delivery_accuracy)
136
- smartrates = get_smartrates
137
- EasyPost::Shipment.get_lowest_smartrate(smartrates, delivery_days, delivery_accuracy)
138
- end
139
-
140
- # Get the lowest smartrate from a list of smartrates.
141
- def self.get_lowest_smartrate(smartrates, delivery_days, delivery_accuracy)
142
- valid_delivery_accuracy_values = Set[
143
- 'percentile_50',
144
- 'percentile_75',
145
- 'percentile_85',
146
- 'percentile_90',
147
- 'percentile_95',
148
- 'percentile_97',
149
- 'percentile_99',
150
- ]
151
- lowest_smartrate = nil
152
-
153
- unless valid_delivery_accuracy_values.include?(delivery_accuracy.downcase)
154
- raise EasyPost::Error.new("Invalid delivery accuracy value, must be one of: #{valid_delivery_accuracy_values}")
155
- end
156
-
157
- smartrates.each do |rate|
158
- next if rate['time_in_transit'][delivery_accuracy] > delivery_days.to_i
159
-
160
- if lowest_smartrate.nil? || rate['rate'].to_f < lowest_smartrate['rate'].to_f
161
- lowest_smartrate = rate
162
- end
163
- end
164
-
165
- if lowest_smartrate.nil?
166
- raise EasyPost::Error.new('No rates found.')
167
- end
168
-
169
- lowest_smartrate
170
- end
171
-
172
- # Generate a form for a Shipment.
173
- def generate_form(form_type, form_options = {})
174
- params = {}
175
- params[:type] = form_type
176
- merged_params = params.merge(form_options)
177
- wrapped_params = {
178
- form: merged_params,
179
- }
180
-
181
- response = EasyPost.make_request(:post, "#{url}/forms", @api_key, wrapped_params)
182
- refresh_from(response, @api_key)
183
-
184
- self
185
- end
186
- end
@@ -1,43 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # A Tracker object contains all of the tracking information for a package.
4
- class EasyPost::Tracker < EasyPost::Resource
5
- # Create multiple Tracker objects in bulk.
6
- def self.create_list(params = {}, api_key = nil)
7
- url = "#{self.url}/create_list"
8
- new_params = { 'trackers' => params }
9
- EasyPost.make_request(:post, url, api_key, new_params)
10
- true # This endpoint does not return a response so we return true here instead
11
- end
12
-
13
- # Retrieve a list of Tracker objects.
14
- def self.all(filters = {}, api_key = nil)
15
- collection = super(filters, api_key)
16
-
17
- # Store the filters used to retrieve the collection.
18
- collection.refresh_from({ tracking_code: filters[:tracking_code], carrier: filters[:carrier] }, api_key)
19
-
20
- collection
21
- end
22
-
23
- # Get the next page of trackers.
24
- def self.get_next_page(collection, page_size = nil)
25
- get_next_page_exec(method(:all), collection, collection.trackers, page_size)
26
- end
27
-
28
- # Build the next page parameters.
29
- def self.build_next_page_params(collection, current_page_items, page_size = nil)
30
- params = {}
31
- params[:before_id] = current_page_items.last.id
32
- unless page_size.nil?
33
- params[:page_size] = page_size
34
- end
35
- unless collection.tracking_code.nil?
36
- params[:tracking_code] = collection.tracking_code
37
- end
38
- unless collection.carrier.nil?
39
- params[:carrier] = collection.carrier
40
- end
41
- params
42
- end
43
- end
data/lib/easypost/user.rb DELETED
@@ -1,74 +0,0 @@
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::User < EasyPost::Resource
5
- # Create a child User.
6
- def self.create(params = {}, api_key = nil)
7
- response = EasyPost.make_request(:post, url, api_key, { class_name.to_sym => params })
8
- EasyPost::Util.convert_to_easypost_object(response, api_key)
9
- end
10
-
11
- # Save (update) a User.
12
- def save
13
- if @unsaved_values.length.positive?
14
- values = {}
15
- @unsaved_values.each { |k| values[k] = @values[k] }
16
-
17
- wrapped_params = { user: values }
18
-
19
- response = EasyPost.make_request(:patch, url, @api_key, wrapped_params)
20
- refresh_from(response, api_key)
21
- end
22
- self
23
- end
24
-
25
- # Delete a User.
26
- def delete
27
- EasyPost.make_request(:delete, url, @api_key)
28
- self
29
- end
30
-
31
- # Retrieve the authenticated User.
32
- def self.retrieve_me
33
- all
34
- end
35
-
36
- # Retrieve a list of all ApiKey objects.
37
- def self.all_api_keys
38
- EasyPost::ApiKey.all
39
- end
40
-
41
- # Retrieve a list of ApiKey objects (works for the authenticated user or a child user).
42
- def api_keys
43
- api_keys = EasyPost::User.all_api_keys
44
-
45
- if api_keys.id == id
46
- # This function was called on the authenticated user
47
- my_api_keys = api_keys.keys
48
- else
49
- # This function was called on a child user (authenticated as parent, only return this child user's details).
50
- my_api_keys = []
51
- api_keys.children.each do |child|
52
- if child.id == id
53
- my_api_keys = child.keys
54
- break
55
- end
56
- end
57
- end
58
-
59
- my_api_keys
60
- end
61
-
62
- # Update the Brand of a User.
63
- def update_brand(**attrs)
64
- brand = EasyPost::Brand.new
65
- data = { object: 'Brand', user_id: id, **attrs }
66
- # Add accessors manually because there's no API to retrieve a brand
67
- brand.add_accessors(data.keys)
68
- # Assigning values with accessors defined above
69
- data.each do |key, val|
70
- brand.send("#{key}=", val)
71
- end
72
- brand.save
73
- end
74
- end