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
@@ -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,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ class EasyPost::Services::Rate < EasyPost::Services::Service
4
+ # Retrieve a Rate
5
+ def retrieve(id)
6
+ @client.make_request(:get, "rates/#{id}", EasyPost::Models::Rate)
7
+ end
8
+ 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
@@ -0,0 +1,106 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'set'
4
+
5
+ class EasyPost::Services::Shipment < EasyPost::Services::Service
6
+ MODEL_CLASS = EasyPost::Models::Shipment
7
+
8
+ # Create a Shipment.
9
+ def create(params = {}, with_carbon_offset = false)
10
+ wrapped_params = {
11
+ shipment: params,
12
+ carbon_offset: with_carbon_offset,
13
+ }
14
+
15
+ @client.make_request(:post, 'shipments', MODEL_CLASS, wrapped_params)
16
+ end
17
+
18
+ # Retrieve a Shipment.
19
+ def retrieve(id)
20
+ @client.make_request(:get, "shipments/#{id}", MODEL_CLASS)
21
+ end
22
+
23
+ # Retrieve a list of Shipments
24
+ def all(params = {})
25
+ response = @client.make_request(:get, 'shipments', MODEL_CLASS, params)
26
+ response.define_singleton_method(:purchased) { params[:purchased] }
27
+ response.define_singleton_method(:include_children) { params[:include_children] }
28
+ response
29
+ end
30
+
31
+ # Get the next page of shipments.
32
+ def get_next_page(collection, page_size = nil)
33
+ get_next_page_helper(collection, collection.shipments, 'shipments', MODEL_CLASS, page_size)
34
+ end
35
+
36
+ # Regenerate the rates of a Shipment.
37
+ def regenerate_rates(id, with_carbon_offset = false)
38
+ params = { carbon_offset: with_carbon_offset }
39
+
40
+ @client.make_request(:post, "shipments/#{id}/rerate", MODEL_CLASS, params)
41
+ end
42
+
43
+ # Get the SmartRates of a Shipment.
44
+ def get_smart_rates(id)
45
+ @client.make_request(:get, "shipments/#{id}/smartrate", MODEL_CLASS).result || []
46
+ end
47
+
48
+ # Buy a Shipment.
49
+ def buy(id, params = {}, with_carbon_offset = false, end_shipper_id = nil)
50
+ if params.instance_of?(EasyPost::Models::Rate)
51
+ params = { rate: params.clone }
52
+ end
53
+
54
+ params[:carbon_offset] = params[:with_carbon_offset] || with_carbon_offset
55
+ params.delete(:with_carbon_offset)
56
+
57
+ params[:end_shipper_id] = end_shipper_id if end_shipper_id
58
+
59
+ @client.make_request(:post, "shipments/#{id}/buy", MODEL_CLASS, params)
60
+ end
61
+
62
+ # Insure a Shipment.
63
+ def insure(id, params = {})
64
+ params = { amount: params } if params.is_a?(Integer) || params.is_a?(Float)
65
+
66
+ @client.make_request(:post, "shipments/#{id}/insure", MODEL_CLASS, params)
67
+ end
68
+
69
+ # Refund a Shipment.
70
+ def refund(id, params = {})
71
+ @client.make_request(:post, "shipments/#{id}/refund", MODEL_CLASS, params)
72
+ end
73
+
74
+ # Convert the label format of a Shipment.
75
+ def label(id, params = {})
76
+ params = { file_format: params } if params.is_a?(String)
77
+
78
+ @client.make_request(:get, "shipments/#{id}/label", MODEL_CLASS, params)
79
+ end
80
+
81
+ # Get the lowest SmartRate of a Shipment.
82
+ def lowest_smart_rate(id, delivery_days, delivery_accuracy)
83
+ smart_rates = get_smart_rates(id)
84
+ EasyPost::Util.get_lowest_smart_rate(smart_rates, delivery_days, delivery_accuracy)
85
+ end
86
+
87
+ # Generate a form for a Shipment.
88
+ def generate_form(id, form_type, form_options = {})
89
+ params = {}
90
+ params[:type] = form_type
91
+ merged_params = params.merge(form_options)
92
+ wrapped_params = {
93
+ form: merged_params,
94
+ }
95
+
96
+ @client.make_request(:post, "shipments/#{id}/forms", MODEL_CLASS, wrapped_params)
97
+ end
98
+
99
+ # Retrieves the estimated delivery date of each Rate via SmartRate.
100
+ def retrieve_estimated_delivery_date(id, planned_ship_date)
101
+ url = "shipments/#{id}/smartrate/delivery_date"
102
+ params = { planned_ship_date: planned_ship_date }
103
+
104
+ @client.make_request(:get, url, MODEL_CLASS, params).rates
105
+ end
106
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ class EasyPost::Services::Tracker < EasyPost::Services::Service
4
+ MODEL_CLASS = EasyPost::Models::Tracker
5
+
6
+ # Create a Tracker
7
+ def create(params = {})
8
+ wrapped_params = { tracker: params }
9
+
10
+ @client.make_request(:post, 'trackers', MODEL_CLASS, wrapped_params)
11
+ end
12
+
13
+ # Retrieve a Tracker
14
+ def retrieve(id)
15
+ @client.make_request(:get, "trackers/#{id}", MODEL_CLASS)
16
+ end
17
+
18
+ # Retrieve a list of Trackers
19
+ def all(params)
20
+ response = @client.make_request(:get, 'trackers', MODEL_CLASS, params)
21
+ response.define_singleton_method(:tracking_code) { params[:tracking_code] }
22
+ response.define_singleton_method(:carrier) { params[:carrier] }
23
+ response
24
+ end
25
+
26
+ # Create multiple Tracker objects in bulk.
27
+ def create_list(params = {})
28
+ wrapped_params = { 'trackers' => params }
29
+
30
+ @client.make_request(:post, 'trackers/create_list', MODEL_CLASS, wrapped_params)
31
+ true # This endpoint does not return a response so we return true here instead
32
+ end
33
+
34
+ # Get the next page of trackers.
35
+ def get_next_page(collection, page_size = nil)
36
+ get_next_page_helper(collection, collection.trackers, 'trackers', MODEL_CLASS, page_size)
37
+ end
38
+ end
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ class EasyPost::Services::User < EasyPost::Services::Service
4
+ MODEL_CLASS = EasyPost::Models::User
5
+
6
+ # Create a child User.
7
+ def create(params = {})
8
+ @client.make_request(:post, 'users', MODEL_CLASS, params)
9
+ end
10
+
11
+ # Retrieve a user
12
+ def retrieve(id)
13
+ @client.make_request(:get, "users/#{id}", MODEL_CLASS)
14
+ end
15
+
16
+ # Retrieve the authenticated User.
17
+ def retrieve_me
18
+ @client.make_request(:get, 'users', MODEL_CLASS)
19
+ end
20
+
21
+ # Update a User
22
+ def update(id, params = {})
23
+ @client.make_request(:put, "users/#{id}", MODEL_CLASS, params)
24
+ end
25
+
26
+ # Delete a User
27
+ def delete(id)
28
+ @client.make_request(:delete, "users/#{id}")
29
+
30
+ # Return true if succeeds, an error will be thrown if it fails
31
+ true
32
+ end
33
+
34
+ # Retrieve a list of all ApiKey objects.
35
+ def all_api_keys
36
+ @client.make_request(:get, 'api_keys', EasyPost::Models::ApiKey)
37
+ end
38
+
39
+ # Retrieve a list of ApiKey objects (works for the authenticated user or a child user).
40
+ def api_keys(id)
41
+ api_keys = all_api_keys
42
+
43
+ if api_keys.id == id
44
+ # This function was called on the authenticated user
45
+ my_api_keys = api_keys.keys
46
+ else
47
+ # This function was called on a child user (authenticated as parent, only return this child user's details).
48
+ my_api_keys = []
49
+ api_keys.children.each do |child|
50
+ if child.id == id
51
+ my_api_keys = child.keys
52
+ break
53
+ end
54
+ end
55
+ end
56
+
57
+ my_api_keys
58
+ end
59
+
60
+ # Update the Brand of a User.
61
+ def update_brand(id, params = {})
62
+ wrapped_params = { brand: params }
63
+
64
+ @client.make_request(:get, "users/#{id}/brand", EasyPost::Models::Brand, wrapped_params)
65
+ end
66
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ class EasyPost::Services::Webhook < EasyPost::Services::Service
4
+ MODEL_CLASS = EasyPost::Models::Webhook
5
+
6
+ # Create a Webhook.
7
+ def create(params = {})
8
+ wrapped_params = { webhook: params }
9
+ @client.make_request(:post, 'webhooks', MODEL_CLASS, wrapped_params)
10
+ end
11
+
12
+ # Retrieve a Webhook
13
+ def retrieve(id)
14
+ @client.make_request(:get, "webhooks/#{id}", MODEL_CLASS)
15
+ end
16
+
17
+ # Retrieve a list of Webhooks
18
+ def all(params = {})
19
+ @client.make_request(:get, 'webhooks', MODEL_CLASS, params)
20
+ end
21
+
22
+ # Update a Webhook.
23
+ def update(id, params = {})
24
+ @client.make_request(:patch, "webhooks/#{id}", MODEL_CLASS, params)
25
+ end
26
+
27
+ # Delete a Webhook.
28
+ def delete(id)
29
+ @client.make_request(:delete, "webhooks/#{id}")
30
+
31
+ # Return true if succeeds, an error will be thrown if it fails
32
+ true
33
+ end
34
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EasyPost::Services
4
+ end
5
+
6
+ require_relative 'models'
7
+ require_relative 'services/base' # Must be imported first before the rest of child services
8
+ require_relative 'services/address'
9
+ require_relative 'services/api_key'
10
+ require_relative 'services/batch'
11
+ require_relative 'services/beta_rate'
12
+ require_relative 'services/beta_referral_customer'
13
+ require_relative 'services/billing'
14
+ require_relative 'services/carrier_account'
15
+ require_relative 'services/carrier_metadata'
16
+ require_relative 'services/customs_info'
17
+ require_relative 'services/customs_item'
18
+ require_relative 'services/end_shipper'
19
+ require_relative 'services/event'
20
+ require_relative 'services/insurance'
21
+ require_relative 'services/order'
22
+ require_relative 'services/parcel'
23
+ require_relative 'services/pickup'
24
+ require_relative 'services/rate'
25
+ require_relative 'services/referral_customer'
26
+ require_relative 'services/refund'
27
+ require_relative 'services/report'
28
+ require_relative 'services/scan_form'
29
+ require_relative 'services/shipment'
30
+ require_relative 'services/tracker'
31
+ require_relative 'services/user'
32
+ require_relative 'services/webhook'