diff --git a/CHANGELOG.md b/CHANGELOG.md index e64b1f0c..6df04309 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## Next release - Removed `with_carbon_offset` parameter from `create`, `buy` and `regenerate_rates` methods in the shipment service since now EasyPost offers carbon neutral shipments by default for free +- Fixes a bug where the original filtering criteria of `all` calls wasn't passed along to `get_next_page` calls. Now, these are persisted via `_params` key on response objects ## v5.3.0 (2023-10-11) diff --git a/examples b/examples index fd3cd288..592dada9 160000 --- a/examples +++ b/examples @@ -1 +1 @@ -Subproject commit fd3cd288685b7203badef4ee1361195beb5dcb6d +Subproject commit 592dada9fdb35eb6d573776ab8fd30373032c251 diff --git a/lib/easypost/client.rb b/lib/easypost/client.rb index 6e8db86f..81fc5356 100644 --- a/lib/easypost/client.rb +++ b/lib/easypost/client.rb @@ -72,7 +72,6 @@ def initialize(api_key:, read_timeout: 60, open_timeout: 30, api_base: 'https:// # # @param method [Symbol] the HTTP Verb (get, method, put, post, etc.) # @param endpoint [String] URI path of the resource - # @param cls [Class] the class to deserialize to # @param body [Object] (nil) object to be dumped to JSON # @param api_version [String] the version of API to hit # @raise [EasyPost::Error] if the response has a non-2xx status code @@ -80,7 +79,6 @@ def initialize(api_key:, read_timeout: 60, open_timeout: 30, api_base: 'https:// def make_request( method, endpoint, - cls = EasyPost::Models::EasyPostObject, body = nil, api_version = EasyPost::InternalUtilities::Constants::API_VERSION ) @@ -89,7 +87,7 @@ def make_request( potential_error = EasyPost::Errors::ApiError.handle_api_error(response) raise potential_error unless potential_error.nil? - EasyPost::InternalUtilities::Json.convert_json_to_object(response.body, cls) + EasyPost::InternalUtilities::Json.parse_json(response.body) end # Subscribe a request hook diff --git a/lib/easypost/models/base.rb b/lib/easypost/models/base.rb index d5ec6348..53968a76 100644 --- a/lib/easypost/models/base.rb +++ b/lib/easypost/models/base.rb @@ -33,6 +33,8 @@ def []=(key, value) def add_properties(values) values.each do |key, _| + next if key == EasyPost::InternalUtilities::Constants::FILTERS_KEY + define_singleton_method(key) { handle_value(@values[key]) } # getter define_singleton_method("#{key}=") { |v| @values[key] = handle_value(v) } # setter end diff --git a/lib/easypost/services/address.rb b/lib/easypost/services/address.rb index 931abeff..723e352d 100644 --- a/lib/easypost/services/address.rb +++ b/lib/easypost/services/address.rb @@ -17,7 +17,9 @@ def create(params = {}) wrapped_params[:verify_strict] = params[:verify_strict] end - @client.make_request(:post, 'addresses', MODEL_CLASS, params) + response = @client.make_request(:post, 'addresses', params) + + EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS) end # Create and verify an Address in one call. @@ -25,26 +27,39 @@ def create_and_verify(params = {}) wrapped_params = {} wrapped_params[:address] = params - @client.make_request(:post, 'addresses/create_and_verify', MODEL_CLASS, wrapped_params).address + response = @client.make_request(:post, 'addresses/create_and_verify', wrapped_params) + + EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS).address end # Verify an Address. def verify(id) - @client.make_request(:get, "addresses/#{id}/verify", MODEL_CLASS).address + response = @client.make_request(:get, "addresses/#{id}/verify") + + EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS).address end # Retrieve an Address. def retrieve(id) - @client.make_request(:get, "addresses/#{id}", MODEL_CLASS) + response = @client.make_request(:get, "addresses/#{id}") + + EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS) end # Retrieve all Addresses. - def all(filters = {}) - @client.make_request(:get, 'addresses', MODEL_CLASS, filters) + def all(params = {}) + filters = { key: 'addresses' } + + get_all_helper('addresses', MODEL_CLASS, params, filters) end # Get the next page of addresses. def get_next_page(collection, page_size = nil) - get_next_page_helper(collection, collection.addresses, 'addresses', MODEL_CLASS, page_size) + raise EasyPost::Errors::EndOfPaginationError.new unless more_pages?(collection) + + params = { before_id: collection.addresses.last.id } + params[:page_size] = page_size unless page_size.nil? + + all(params) end end diff --git a/lib/easypost/services/api_key.rb b/lib/easypost/services/api_key.rb index ed803d28..6bb1e98e 100644 --- a/lib/easypost/services/api_key.rb +++ b/lib/easypost/services/api_key.rb @@ -3,7 +3,9 @@ class EasyPost::Services::ApiKey < EasyPost::Services::Service # Retrieve a list of all ApiKey objects. def all - @client.make_request(:get, 'api_keys', EasyPost::Models::ApiKey) + response = @client.make_request(:get, 'api_keys') + + EasyPost::InternalUtilities::Json.convert_json_to_object(response, EasyPost::Models::ApiKey) end # Retrieve a list of ApiKey objects (works for the authenticated user or a child user). diff --git a/lib/easypost/services/base.rb b/lib/easypost/services/base.rb index 5842bbec..16a7b6e9 100644 --- a/lib/easypost/services/base.rb +++ b/lib/easypost/services/base.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require_relative '../internal_utilities' + # The base class for all services in the library. class EasyPost::Services::Service def initialize(client) @@ -8,20 +10,15 @@ def initialize(client) protected - # Get next page of an object collection - def get_next_page_helper(collection, current_page_items, endpoint, cls, page_size = nil) - has_more = collection.has_more || false - unless !has_more || current_page_items.nil? || current_page_items.empty? - params = {} - params[:before_id] = current_page_items.last.id - unless page_size.nil? - params[:page_size] = page_size - end + def get_all_helper(endpoint, cls, params, filters = nil) + response = @client.make_request(:get, endpoint, params) + + response[EasyPost::InternalUtilities::Constants::FILTERS_KEY] = filters unless filters.nil? - @client.make_request(:get, endpoint, cls, params) - end + EasyPost::InternalUtilities::Json.convert_json_to_object(response, cls) + end - # issue with getting the next page - raise EasyPost::Errors::EndOfPaginationError.new + def more_pages?(collection) + collection&.has_more end end diff --git a/lib/easypost/services/batch.rb b/lib/easypost/services/batch.rb index 917219cf..0a1b2b10 100644 --- a/lib/easypost/services/batch.rb +++ b/lib/easypost/services/batch.rb @@ -6,48 +6,64 @@ class EasyPost::Services::Batch < EasyPost::Services::Service # Create a Batch. def create(params = {}) wrapped_params = { batch: params } + response = @client.make_request(:post, 'batches', wrapped_params) - @client.make_request(:post, 'batches', MODEL_CLASS, wrapped_params) + EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS) end # Create and buy a batch in one call. def create_and_buy(params = {}) wrapped_params = { batch: params } + response = @client.make_request(:post, 'batches/create_and_buy', wrapped_params) - @client.make_request(:post, 'batches/create_and_buy', MODEL_CLASS, wrapped_params) + EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS) end def all(params = {}) - @client.make_request(:get, 'batches', EasyPost::Models::ApiKey, params) + filters = { key: 'batches' } + + get_all_helper('batches', MODEL_CLASS, params, filters) end # Retrieve a Batch def retrieve(id) - @client.make_request(:get, "batches/#{id}", MODEL_CLASS) + response = @client.make_request(:get, "batches/#{id}") + + EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS) end # Buy a Batch. def buy(id, params = {}) - @client.make_request(:post, "batches/#{id}/buy", MODEL_CLASS, params) + response = @client.make_request(:post, "batches/#{id}/buy", params) + + EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS) end # Convert the label format of a Batch. def label(id, params = {}) - @client.make_request(:post, "batches/#{id}/label", MODEL_CLASS, params) + response = @client.make_request(:post, "batches/#{id}/label", params) + + EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS) end # Remove Shipments from a Batch. def remove_shipments(id, params = {}) - @client.make_request(:post, "batches/#{id}/remove_shipments", MODEL_CLASS, params) + response = @client.make_request(:post, "batches/#{id}/remove_shipments", params) + + EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS) end # Add Shipments to a Batch. def add_shipments(id, params = {}) - @client.make_request(:post, "batches/#{id}/add_shipments", MODEL_CLASS, params) + response = @client.make_request(:post, "batches/#{id}/add_shipments", params) + + EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS) end # Create a ScanForm for a Batch. def create_scan_form(id, params = {}) - @client.make_request(:post, "batches/#{id}/scan_form", MODEL_CLASS, params) + response = @client.make_request(:post, "batches/#{id}/scan_form", params) + + EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS) end end diff --git a/lib/easypost/services/beta_rate.rb b/lib/easypost/services/beta_rate.rb index c92277d5..081359f6 100644 --- a/lib/easypost/services/beta_rate.rb +++ b/lib/easypost/services/beta_rate.rb @@ -6,7 +6,8 @@ def retrieve_stateless_rates(params = {}) wrapped_params = { shipment: params, } + response = @client.make_request(:post, 'rates', wrapped_params, 'beta') - @client.make_request(:post, 'rates', EasyPost::Models::Rate, wrapped_params, 'beta').rates + EasyPost::InternalUtilities::Json.convert_json_to_object(response, EasyPost::Models::Rate).rates end end diff --git a/lib/easypost/services/beta_referral_customer.rb b/lib/easypost/services/beta_referral_customer.rb index 7a315505..9c83b19e 100644 --- a/lib/easypost/services/beta_referral_customer.rb +++ b/lib/easypost/services/beta_referral_customer.rb @@ -10,13 +10,14 @@ def add_payment_method(stripe_customer_id, payment_method_reference, priority = priority: priority.downcase, }, } - @client.make_request( + response = @client.make_request( :post, 'referral_customers/payment_method', - EasyPost::Models::EasyPostObject, wrapped_params, 'beta', ) + + EasyPost::InternalUtilities::Json.convert_json_to_object(response) end # Refund a ReferralCustomer Customer's wallet by a specified amount. Refund will be issued to the user's original payment method. @@ -25,8 +26,9 @@ def refund_by_amount(amount) params = { refund_amount: amount, } - @client.make_request(:post, 'referral_customers/refunds', EasyPost::Models::EasyPostObject, params, 'beta') - # noinspection RubyMismatchedReturnType + response = @client.make_request(:post, 'referral_customers/refunds', params, 'beta') + + EasyPost::InternalUtilities::Json.convert_json_to_object(response) end # Refund a ReferralCustomer Customer's wallet for a specified payment log entry. Refund will be issued to the user's original payment method. @@ -35,6 +37,8 @@ def refund_by_payment_log(payment_log_id) params = { payment_log_id: payment_log_id, } - @client.make_request(:post, 'referral_customers/refunds', EasyPost::Models::EasyPostObject, params, 'beta') + response = @client.make_request(:post, 'referral_customers/refunds', params, 'beta') + + EasyPost::InternalUtilities::Json.convert_json_to_object(response) end end diff --git a/lib/easypost/services/billing.rb b/lib/easypost/services/billing.rb index 0e2aefc7..2b99b63c 100644 --- a/lib/easypost/services/billing.rb +++ b/lib/easypost/services/billing.rb @@ -44,7 +44,7 @@ def fund_wallet(amount, priority = 'primary') payment_id = payment_info[1] wrapped_params = { amount: amount } - @client.make_request(:post, "#{endpoint}/#{payment_id}/charges", EasyPost::Models::EasyPostObject, wrapped_params) + @client.make_request(:post, "#{endpoint}/#{payment_id}/charges", wrapped_params) # Return true if succeeds, an error will be thrown if it fails true @@ -65,11 +65,12 @@ def delete_payment_method(priority) # Retrieve all payment methods. def retrieve_payment_methods response = @client.make_request(:get, '/v2/payment_methods') + payment_methods = EasyPost::InternalUtilities::Json.convert_json_to_object(response) - if response['id'].nil? + if payment_methods['id'].nil? raise EasyPost::Errors::InvalidObjectError.new(EasyPost::Constants::NO_PAYMENT_METHODS) end - response + payment_methods end end diff --git a/lib/easypost/services/carrier_account.rb b/lib/easypost/services/carrier_account.rb index eb5193bf..fb416816 100644 --- a/lib/easypost/services/carrier_account.rb +++ b/lib/easypost/services/carrier_account.rb @@ -14,24 +14,29 @@ def create(params = {}) else 'carrier_accounts' end + response = @client.make_request(:post, create_url, wrapped_params) - @client.make_request(:post, create_url, MODEL_CLASS, wrapped_params) + EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS) end # Retrieve a carrier account def retrieve(id) - @client.make_request(:get, "carrier_accounts/#{id}", MODEL_CLASS) + response = @client.make_request(:get, "carrier_accounts/#{id}") + + EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS) end # Retrieve all carrier accounts def all(params = {}) - @client.make_request(:get, 'carrier_accounts', MODEL_CLASS, params) + get_all_helper('carrier_accounts', MODEL_CLASS, params) end # Update a carrier account def update(id, params = {}) wrapped_params = { carrier_account: params } - @client.make_request(:put, "carrier_accounts/#{id}", MODEL_CLASS, wrapped_params) + response = @client.make_request(:put, "carrier_accounts/#{id}", wrapped_params) + + EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS) end # Delete a carrier account diff --git a/lib/easypost/services/carrier_metadata.rb b/lib/easypost/services/carrier_metadata.rb index 6bad1315..b28f2eaa 100644 --- a/lib/easypost/services/carrier_metadata.rb +++ b/lib/easypost/services/carrier_metadata.rb @@ -4,7 +4,6 @@ class EasyPost::Services::CarrierMetadata < EasyPost::Services::Service # Retrieve metadata for carrier(s). def retrieve(carriers = [], types = []) path = '/metadata/carriers?' - params = {} if carriers.length.positive? @@ -16,7 +15,8 @@ def retrieve(carriers = [], types = []) end path += URI.encode_www_form(params) + response = @client.make_request(:get, path, params) - @client.make_request(:get, path, EasyPost::Models::EasyPostObject, params).carriers + EasyPost::InternalUtilities::Json.convert_json_to_object(response).carriers end end diff --git a/lib/easypost/services/carrier_type.rb b/lib/easypost/services/carrier_type.rb index 8dfca0c3..6fcb5d6e 100644 --- a/lib/easypost/services/carrier_type.rb +++ b/lib/easypost/services/carrier_type.rb @@ -5,6 +5,8 @@ class EasyPost::Services::CarrierType < EasyPost::Services::Service # Retrieve all carrier types def all - @client.make_request(:get, 'carrier_types', MODEL_CLASS) + response = @client.make_request(:get, 'carrier_types') + + EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS) end end diff --git a/lib/easypost/services/customs_info.rb b/lib/easypost/services/customs_info.rb index eb783dcc..efdedf00 100644 --- a/lib/easypost/services/customs_info.rb +++ b/lib/easypost/services/customs_info.rb @@ -6,12 +6,15 @@ class EasyPost::Services::CustomsInfo < EasyPost::Services::Service # Create a CustomsInfo object def create(params) wrapped_params = { customs_info: params } + response = @client.make_request(:post, 'customs_infos', wrapped_params) - @client.make_request(:post, 'customs_infos', MODEL_CLASS, wrapped_params) + EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS) end # Retrieve a CustomsInfo object def retrieve(id) - @client.make_request(:get, "customs_infos/#{id}", MODEL_CLASS) + response = @client.make_request(:get, "customs_infos/#{id}") + + EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS) end end diff --git a/lib/easypost/services/customs_item.rb b/lib/easypost/services/customs_item.rb index fac6e205..aa5f5042 100644 --- a/lib/easypost/services/customs_item.rb +++ b/lib/easypost/services/customs_item.rb @@ -5,11 +5,15 @@ class EasyPost::Services::CustomsItem < EasyPost::Services::Service # Create a CustomsItem object def create(params) - @client.make_request(:post, 'customs_items', MODEL_CLASS, params) + response = @client.make_request(:post, 'customs_items', params) + + EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS) end # Retrieve a CustomsItem object def retrieve(id) - @client.make_request(:get, "customs_items/#{id}", MODEL_CLASS) + response = @client.make_request(:get, "customs_items/#{id}") + + EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS) end end diff --git a/lib/easypost/services/end_shipper.rb b/lib/easypost/services/end_shipper.rb index 712a8f61..a525cc89 100644 --- a/lib/easypost/services/end_shipper.rb +++ b/lib/easypost/services/end_shipper.rb @@ -6,25 +6,31 @@ class EasyPost::Services::EndShipper < EasyPost::Services::Service # Create an EndShipper object. def create(params = {}) wrapped_params = { address: params } + response = @client.make_request(:post, 'end_shippers', wrapped_params) - @client.make_request(:post, 'end_shippers', MODEL_CLASS, wrapped_params) + EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS) end # Retrieve an EndShipper object. def retrieve(id) - @client.make_request(:get, "end_shippers/#{id}", MODEL_CLASS) + response = @client.make_request(:get, "end_shippers/#{id}") + + EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS) end # Retrieve all EndShipper objects. def all(params = {}) - @client.make_request(:get, 'end_shippers', MODEL_CLASS, params) + response = @client.make_request(:get, 'end_shippers', params) + + EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS) end # Updates an EndShipper object. This requires all parameters to be set. def update(id, params) wrapped_params = { address: params } + response = @client.make_request(:put, "end_shippers/#{id}", wrapped_params) - @client.make_request(:put, "end_shippers/#{id}", MODEL_CLASS, wrapped_params) + EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS) end # TODO: Add support for getting the next page of end shippers when the API supports it. diff --git a/lib/easypost/services/event.rb b/lib/easypost/services/event.rb index 3d1a78d4..51f12472 100644 --- a/lib/easypost/services/event.rb +++ b/lib/easypost/services/event.rb @@ -7,26 +7,39 @@ class EasyPost::Services::Event < EasyPost::Services::Service # Retrieve an Event object def retrieve(id) - @client.make_request(:get, "events/#{id}", MODEL_CLASS) + response = @client.make_request(:get, "events/#{id}") + + EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS) end # Retrieve all Event objects def all(params = {}) - @client.make_request(:get, 'events', MODEL_CLASS, params) + filters = { key: 'events' } + + get_all_helper('events', MODEL_CLASS, params, filters) end # Retrieve all payloads for an event. def retrieve_all_payloads(event_id) - @client.make_request(:get, "events/#{event_id}/payloads", EasyPost::Models::Payload) + response = @client.make_request(:get, "events/#{event_id}/payloads") + + EasyPost::InternalUtilities::Json.convert_json_to_object(response, EasyPost::Models::Payload) end # Retrieve a specific payload for an event. def retrieve_payload(event_id, payload_id) - @client.make_request(:get, "events/#{event_id}/payloads/#{payload_id}", EasyPost::Models::Payload) + response = @client.make_request(:get, "events/#{event_id}/payloads/#{payload_id}") + + EasyPost::InternalUtilities::Json.convert_json_to_object(response, EasyPost::Models::Payload) end # Get the next page of events. def get_next_page(collection, page_size = nil) - get_next_page_helper(collection, collection.events, 'events', MODEL_CLASS, page_size) + raise EasyPost::Errors::EndOfPaginationError.new unless more_pages?(collection) + + params = { before_id: collection.events.last.id } + params[:page_size] = page_size unless page_size.nil? + + all(params) end end diff --git a/lib/easypost/services/insurance.rb b/lib/easypost/services/insurance.rb index aaf96c44..44c66dce 100644 --- a/lib/easypost/services/insurance.rb +++ b/lib/easypost/services/insurance.rb @@ -6,21 +6,32 @@ class EasyPost::Services::Insurance < EasyPost::Services::Service # Create an Insurance object def create(params = {}) wrapped_params = { insurance: params } - @client.make_request(:post, 'insurances', MODEL_CLASS, wrapped_params) + response = @client.make_request(:post, 'insurances', wrapped_params) + + EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS) end # Retrieve an Insurance object def retrieve(id) - @client.make_request(:get, "insurances/#{id}", MODEL_CLASS) + response = @client.make_request(:get, "insurances/#{id}") + + EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS) end # Retrieve all Insurance objects def all(params = {}) - @client.make_request(:get, 'insurances', MODEL_CLASS, params) + filters = { key: 'insurances' } + + get_all_helper('insurances', MODEL_CLASS, params, filters) end # Get the next page of insurances. def get_next_page(collection, page_size = nil) - get_next_page_helper(collection, collection.insurances, 'insurances', MODEL_CLASS, page_size) + raise EasyPost::Errors::EndOfPaginationError.new unless more_pages?(collection) + + params = { before_id: collection.insurances.last.id } + params[:page_size] = page_size unless page_size.nil? + + all(params) end end diff --git a/lib/easypost/services/order.rb b/lib/easypost/services/order.rb index ee359dae..b61d61d5 100644 --- a/lib/easypost/services/order.rb +++ b/lib/easypost/services/order.rb @@ -6,17 +6,23 @@ class EasyPost::Services::Order < EasyPost::Services::Service # Create an Order object def create(params = {}) wrapped_params = { order: params } - @client.make_request(:post, 'orders', MODEL_CLASS, wrapped_params) + response = @client.make_request(:post, 'orders', wrapped_params) + + EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS) end # Retrieve an Order object def retrieve(id) - @client.make_request(:get, "orders/#{id}", MODEL_CLASS) + response = @client.make_request(:get, "orders/#{id}") + + EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS) end # Retrieve new rates for an Order object def get_rates(id, params = {}) - @client.make_request(:get, "orders/#{id}/rates", MODEL_CLASS, params) + response = @client.make_request(:get, "orders/#{id}/rates", params) + + EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS) end # Buy an Order object @@ -25,6 +31,8 @@ def buy(id, params = {}) params = { carrier: params[:carrier], service: params[:service] } end - @client.make_request(:post, "orders/#{id}/buy", MODEL_CLASS, params) + response = @client.make_request(:post, "orders/#{id}/buy", params) + + EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS) end end diff --git a/lib/easypost/services/parcel.rb b/lib/easypost/services/parcel.rb index be8246c8..d4a90631 100644 --- a/lib/easypost/services/parcel.rb +++ b/lib/easypost/services/parcel.rb @@ -6,11 +6,15 @@ class EasyPost::Services::Parcel < EasyPost::Services::Service # Create a Parcel object def create(params = {}) wrapped_params = { parcel: params } - @client.make_request(:post, 'parcels', MODEL_CLASS, wrapped_params) + response = @client.make_request(:post, 'parcels', wrapped_params) + + EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS) end # Retrieve a Parcel object def retrieve(id) - @client.make_request(:get, "parcels/#{id}", MODEL_CLASS) + response = @client.make_request(:get, "parcels/#{id}") + + EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS) end end diff --git a/lib/easypost/services/pickup.rb b/lib/easypost/services/pickup.rb index 9b561c7f..381813b5 100644 --- a/lib/easypost/services/pickup.rb +++ b/lib/easypost/services/pickup.rb @@ -6,17 +6,23 @@ class EasyPost::Services::Pickup < EasyPost::Services::Service # Create a Pickup object def create(params = {}) wrapped_params = { pickup: params } - @client.make_request(:post, 'pickups', MODEL_CLASS, wrapped_params) + response = @client.make_request(:post, 'pickups', wrapped_params) + + EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS) end # Retrieve a Pickup object def retrieve(id) - @client.make_request(:get, "pickups/#{id}", MODEL_CLASS) + response = @client.make_request(:get, "pickups/#{id}") + + EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS) end # Retrieve all Pickup objects def all(params = {}) - @client.make_request(:get, 'pickups', MODEL_CLASS, params) + filters = { key: 'pickups' } + + get_all_helper('pickups', MODEL_CLASS, params, filters) end # Buy a Pickup @@ -25,16 +31,25 @@ def buy(id, params = {}) params = { carrier: params[:carrier], service: params[:service] } end - @client.make_request(:post, "pickups/#{id}/buy", MODEL_CLASS, params) + response = @client.make_request(:post, "pickups/#{id}/buy", params) + + EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS) end # Cancel a Pickup def cancel(id, params = {}) - @client.make_request(:post, "pickups/#{id}/cancel", MODEL_CLASS, params) + response = @client.make_request(:post, "pickups/#{id}/cancel", params) + + EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS) end # Get next page of Pickups def get_next_page(collection, page_size = nil) - get_next_page_helper(collection, collection.pickups, 'pickups', MODEL_CLASS, page_size) + raise EasyPost::Errors::EndOfPaginationError.new unless more_pages?(collection) + + params = { before_id: collection.pickups.last.id } + params[:page_size] = page_size unless page_size.nil? + + all(params) end end diff --git a/lib/easypost/services/rate.rb b/lib/easypost/services/rate.rb index 2bed7c4e..d27a8fd5 100644 --- a/lib/easypost/services/rate.rb +++ b/lib/easypost/services/rate.rb @@ -3,6 +3,8 @@ class EasyPost::Services::Rate < EasyPost::Services::Service # Retrieve a Rate def retrieve(id) - @client.make_request(:get, "rates/#{id}", EasyPost::Models::Rate) + response = @client.make_request(:get, "rates/#{id}") + + EasyPost::InternalUtilities::Json.convert_json_to_object(response, EasyPost::Models::Rate) end end diff --git a/lib/easypost/services/referral_customer.rb b/lib/easypost/services/referral_customer.rb index b544994a..fe37d3fc 100644 --- a/lib/easypost/services/referral_customer.rb +++ b/lib/easypost/services/referral_customer.rb @@ -5,7 +5,9 @@ class EasyPost::Services::ReferralCustomer < EasyPost::Services::Service # Create a referral customer. This function requires the Partner User's API key. def create(params = {}) - @client.make_request(:post, 'referral_customers', MODEL_CLASS, { user: params }) + response = @client.make_request(:post, 'referral_customers', { user: params }) + + EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS) end # Update a referral customer. This function requires the Partner User's API key. @@ -15,7 +17,7 @@ def update_email(user_id, email) email: email, }, } - @client.make_request(:put, "referral_customers/#{user_id}", MODEL_CLASS, wrapped_params) + @client.make_request(:put, "referral_customers/#{user_id}", wrapped_params) # return true if API succeeds, else an error is throw if it fails. true @@ -23,12 +25,19 @@ def update_email(user_id, email) # Retrieve a list of referral customers. This function requires the Partner User's API key. def all(params = {}) - @client.make_request(:get, 'referral_customers', MODEL_CLASS, params) + filters = { key: 'referral_customers' } + + get_all_helper('referral_customers', MODEL_CLASS, params, filters) end # Get the next page of referral customers. def get_next_page(collection, page_size = nil) - get_next_page_helper(collection, collection.referral_customers, 'referral_customers', MODEL_CLASS, page_size) + raise EasyPost::Errors::EndOfPaginationError.new unless more_pages?(collection) + + params = { before_id: collection.referral_customers.last.id } + params[:page_size] = page_size unless page_size.nil? + + all(params) end # Add credit card to a referral customer. This function requires the ReferralCustomer Customer's API key. @@ -54,7 +63,7 @@ def add_credit_card(referral_api_key, number, expiration_month, expiration_year, # Retrieve EasyPost's Stripe public API key. def retrieve_easypost_stripe_api_key - response = @client.make_request(:get, 'partners/stripe_public_key', EasyPost::Models::EasyPostObject, nil, 'beta') + response = @client.make_request(:get, 'partners/stripe_public_key', nil, 'beta') response['public_key'] end @@ -98,6 +107,13 @@ def create_easypost_credit_card(referral_api_key, stripe_object_id, priority = ' }, } referral_client = EasyPost::Client.new(api_key: referral_api_key) - referral_client.make_request(:post, 'credit_cards', EasyPost::Models::EasyPostObject, wrapped_params, 'beta') + response = referral_client.make_request( + :post, + 'credit_cards', + wrapped_params, + 'beta', + ) + + EasyPost::InternalUtilities::Json.convert_json_to_object(response) end end diff --git a/lib/easypost/services/refund.rb b/lib/easypost/services/refund.rb index 5a4b7915..4b44bb48 100644 --- a/lib/easypost/services/refund.rb +++ b/lib/easypost/services/refund.rb @@ -6,21 +6,32 @@ class EasyPost::Services::Refund < EasyPost::Services::Service # Create a Refund object def create(params = {}) wrapped_params = { refund: params } - @client.make_request(:post, 'refunds', MODEL_CLASS, wrapped_params) + response = @client.make_request(:post, 'refunds', wrapped_params) + + EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS) end # Retrieve a Refund object def retrieve(id) - @client.make_request(:get, "refunds/#{id}", MODEL_CLASS) + response = @client.make_request(:get, "refunds/#{id}") + + EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS) end # Retrieve all Refund objects def all(params = {}) - @client.make_request(:get, 'refunds', MODEL_CLASS, params) + filters = { key: 'refunds' } + + get_all_helper('refunds', MODEL_CLASS, params, filters) end # Get the next page of refunds def get_next_page(collection, page_size = nil) - get_next_page_helper(collection, collection.refunds, 'refunds', MODEL_CLASS, page_size) + raise EasyPost::Errors::EndOfPaginationError.new unless more_pages?(collection) + + params = { before_id: collection.refunds.last.id } + params[:page_size] = page_size unless page_size.nil? + + all(params) end end diff --git a/lib/easypost/services/report.rb b/lib/easypost/services/report.rb index a1038079..bda57178 100644 --- a/lib/easypost/services/report.rb +++ b/lib/easypost/services/report.rb @@ -11,13 +11,16 @@ def create(params = {}) type = params.delete(:type) url = "reports/#{type}" + response = @client.make_request(:post, url, params) - @client.make_request(:post, url, MODEL_CLASS, params) + EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS) end # Retrieve a Report def retrieve(id) - @client.make_request(:get, "reports/#{id}", MODEL_CLASS) + response = @client.make_request(:get, "reports/#{id}") + + EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS) end # Retrieve all Report objects @@ -27,16 +30,25 @@ def all(params = {}) end type = params.delete(:type) + filters = { + key: 'reports', + type: type, + } url = "reports/#{type}" - - response = @client.make_request(:get, url, MODEL_CLASS, params) + response = get_all_helper(url, MODEL_CLASS, params, filters) response.define_singleton_method(:type) { type } + response end # Get next page of Report objects def get_next_page(collection, page_size = nil) - url = "reports/#{collection.type}" - get_next_page_helper(collection, collection.reports, url, MODEL_CLASS, page_size) + raise EasyPost::Errors::EndOfPaginationError.new unless more_pages?(collection) + + params = { before_id: collection.reports.last.id } + params[:page_size] = page_size unless page_size.nil? + params.merge!(collection[EasyPost::InternalUtilities::Constants::FILTERS_KEY]).delete(:key) + + all(params) end end diff --git a/lib/easypost/services/scan_form.rb b/lib/easypost/services/scan_form.rb index 2b38968f..39462de5 100644 --- a/lib/easypost/services/scan_form.rb +++ b/lib/easypost/services/scan_form.rb @@ -5,21 +5,32 @@ class EasyPost::Services::ScanForm < EasyPost::Services::Service # Create a ScanForm. def create(params = {}) - @client.make_request(:post, 'scan_forms', MODEL_CLASS, params) + response = @client.make_request(:post, 'scan_forms', params) + + EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS) end # Retrieve a ScanForm. def retrieve(id) - @client.make_request(:get, "scan_forms/#{id}", MODEL_CLASS) + response = @client.make_request(:get, "scan_forms/#{id}") + + EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS) end # Retrieve a list of ScanForms def all(params = {}) - @client.make_request(:get, 'scan_forms', MODEL_CLASS, params) + filters = { key: 'scan_forms' } + + get_all_helper('scan_forms', MODEL_CLASS, params, filters) end # Get the next page of ScanForms. def get_next_page(collection, page_size = nil) - get_next_page_helper(collection, collection.scan_forms, 'scan_forms', MODEL_CLASS, page_size) + raise EasyPost::Errors::EndOfPaginationError.new unless more_pages?(collection) + + params = { before_id: collection.scan_forms.last.id } + params[:page_size] = page_size unless page_size.nil? + + all(params) end end diff --git a/lib/easypost/services/shipment.rb b/lib/easypost/services/shipment.rb index a51eefd8..fa163061 100644 --- a/lib/easypost/services/shipment.rb +++ b/lib/easypost/services/shipment.rb @@ -8,36 +8,52 @@ class EasyPost::Services::Shipment < EasyPost::Services::Service # Create a Shipment. def create(params = {}) wrapped_params = { shipment: params } + response = @client.make_request(:post, 'shipments', wrapped_params) - @client.make_request(:post, 'shipments', MODEL_CLASS, wrapped_params) + EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS) end # Retrieve a Shipment. def retrieve(id) - @client.make_request(:get, "shipments/#{id}", MODEL_CLASS) + response = @client.make_request(:get, "shipments/#{id}") + + EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS) end # Retrieve a list of Shipments def all(params = {}) - response = @client.make_request(:get, 'shipments', MODEL_CLASS, params) - response.define_singleton_method(:purchased) { params[:purchased] } - response.define_singleton_method(:include_children) { params[:include_children] } - response + filters = { + key: 'shipments', + purchased: params[:purchased], + include_children: params[:include_children], + } + + get_all_helper('shipments', MODEL_CLASS, params, filters) end # Get the next page of shipments. def get_next_page(collection, page_size = nil) - get_next_page_helper(collection, collection.shipments, 'shipments', MODEL_CLASS, page_size) + raise EasyPost::Errors::EndOfPaginationError.new unless more_pages?(collection) + + params = { before_id: collection.shipments.last.id } + params[:page_size] = page_size unless page_size.nil? + params.merge!(collection[EasyPost::InternalUtilities::Constants::FILTERS_KEY]).delete(:key) + + all(params) end # Regenerate the rates of a Shipment. def regenerate_rates(id) - @client.make_request(:post, "shipments/#{id}/rerate", MODEL_CLASS) + response = @client.make_request(:post, "shipments/#{id}/rerate") + + EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS) end # Get the SmartRates of a Shipment. def get_smart_rates(id) - @client.make_request(:get, "shipments/#{id}/smartrate", MODEL_CLASS).result || [] + response = @client.make_request(:get, "shipments/#{id}/smartrate") + + EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS).result || [] end # Buy a Shipment. @@ -47,27 +63,32 @@ def buy(id, params = {}, end_shipper_id = nil) end params[:end_shipper_id] = end_shipper_id if end_shipper_id + response = @client.make_request(:post, "shipments/#{id}/buy", params) - @client.make_request(:post, "shipments/#{id}/buy", MODEL_CLASS, params) + EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS) end # Insure a Shipment. def insure(id, params = {}) params = { amount: params } if params.is_a?(Integer) || params.is_a?(Float) + response = @client.make_request(:post, "shipments/#{id}/insure", params) - @client.make_request(:post, "shipments/#{id}/insure", MODEL_CLASS, params) + EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS) end # Refund a Shipment. def refund(id, params = {}) - @client.make_request(:post, "shipments/#{id}/refund", MODEL_CLASS, params) + response = @client.make_request(:post, "shipments/#{id}/refund", params) + + EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS) end # Convert the label format of a Shipment. def label(id, params = {}) params = { file_format: params } if params.is_a?(String) + response = @client.make_request(:get, "shipments/#{id}/label", params) - @client.make_request(:get, "shipments/#{id}/label", MODEL_CLASS, params) + EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS) end # Get the lowest SmartRate of a Shipment. @@ -84,15 +105,17 @@ def generate_form(id, form_type, form_options = {}) wrapped_params = { form: merged_params, } + response = @client.make_request(:post, "shipments/#{id}/forms", wrapped_params) - @client.make_request(:post, "shipments/#{id}/forms", MODEL_CLASS, wrapped_params) + EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS) end # Retrieves the estimated delivery date of each Rate via SmartRate. def retrieve_estimated_delivery_date(id, planned_ship_date) url = "shipments/#{id}/smartrate/delivery_date" params = { planned_ship_date: planned_ship_date } + response = @client.make_request(:get, url, params) - @client.make_request(:get, url, MODEL_CLASS, params).rates + EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS).rates end end diff --git a/lib/easypost/services/tracker.rb b/lib/easypost/services/tracker.rb index 7b44d0e6..d20e619d 100644 --- a/lib/easypost/services/tracker.rb +++ b/lib/easypost/services/tracker.rb @@ -6,33 +6,45 @@ class EasyPost::Services::Tracker < EasyPost::Services::Service # Create a Tracker def create(params = {}) wrapped_params = { tracker: params } + response = @client.make_request(:post, 'trackers', wrapped_params) - @client.make_request(:post, 'trackers', MODEL_CLASS, wrapped_params) + EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS) end # Retrieve a Tracker def retrieve(id) - @client.make_request(:get, "trackers/#{id}", MODEL_CLASS) + response = @client.make_request(:get, "trackers/#{id}") + + EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS) end # Retrieve a list of Trackers - def all(params) - response = @client.make_request(:get, 'trackers', MODEL_CLASS, params) - response.define_singleton_method(:tracking_code) { params[:tracking_code] } - response.define_singleton_method(:carrier) { params[:carrier] } - response + def all(params = {}) + filters = { + key: 'trackers', + tracking_code: params[:tracking_code], + carrier: params[:carrier], + } + + get_all_helper('trackers', MODEL_CLASS, params, filters) end # Create multiple Tracker objects in bulk. def create_list(params = {}) wrapped_params = { 'trackers' => params } - @client.make_request(:post, 'trackers/create_list', MODEL_CLASS, wrapped_params) + @client.make_request(:post, 'trackers/create_list', wrapped_params) true # This endpoint does not return a response so we return true here instead end # Get the next page of trackers. def get_next_page(collection, page_size = nil) - get_next_page_helper(collection, collection.trackers, 'trackers', MODEL_CLASS, page_size) + raise EasyPost::Errors::EndOfPaginationError.new unless more_pages?(collection) + + params = { before_id: collection.trackers.last.id } + params[:page_size] = page_size unless page_size.nil? + params.merge!(collection[EasyPost::InternalUtilities::Constants::FILTERS_KEY]).delete(:key) + + all(params) end end diff --git a/lib/easypost/services/user.rb b/lib/easypost/services/user.rb index 2443fa5a..bd558cf3 100644 --- a/lib/easypost/services/user.rb +++ b/lib/easypost/services/user.rb @@ -5,22 +5,30 @@ class EasyPost::Services::User < EasyPost::Services::Service # Create a child User. def create(params = {}) - @client.make_request(:post, 'users', MODEL_CLASS, params) + response = @client.make_request(:post, 'users', params) + + EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS) end # Retrieve a user def retrieve(id) - @client.make_request(:get, "users/#{id}", MODEL_CLASS) + response = @client.make_request(:get, "users/#{id}") + + EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS) end # Retrieve the authenticated User. def retrieve_me - @client.make_request(:get, 'users', MODEL_CLASS) + response = @client.make_request(:get, 'users') + + EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS) end # Update a User def update(id, params = {}) - @client.make_request(:put, "users/#{id}", MODEL_CLASS, params) + response = @client.make_request(:put, "users/#{id}", params) + + EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS) end # Delete a User @@ -34,7 +42,9 @@ def delete(id) # Retrieve a list of all ApiKey objects. def all_api_keys warn '[DEPRECATION] `all_api_keys` is deprecated. Please use `all` in the `api_key` service instead.' - @client.make_request(:get, 'api_keys', EasyPost::Models::ApiKey) + response = @client.make_request(:get, 'api_keys') + + EasyPost::InternalUtilities::Json.convert_json_to_object(response, EasyPost::Models::ApiKey) end # Retrieve a list of ApiKey objects (works for the authenticated user or a child user). @@ -64,7 +74,8 @@ def api_keys(id) # Update the Brand of a User. def update_brand(id, params = {}) wrapped_params = { brand: params } + response = @client.make_request(:get, "users/#{id}/brand", wrapped_params) - @client.make_request(:get, "users/#{id}/brand", EasyPost::Models::Brand, wrapped_params) + EasyPost::InternalUtilities::Json.convert_json_to_object(response, EasyPost::Models::Brand) end end diff --git a/lib/easypost/services/webhook.rb b/lib/easypost/services/webhook.rb index 1feb5169..626c24ac 100644 --- a/lib/easypost/services/webhook.rb +++ b/lib/easypost/services/webhook.rb @@ -6,22 +6,30 @@ class EasyPost::Services::Webhook < EasyPost::Services::Service # Create a Webhook. def create(params = {}) wrapped_params = { webhook: params } - @client.make_request(:post, 'webhooks', MODEL_CLASS, wrapped_params) + response = @client.make_request(:post, 'webhooks', wrapped_params) + + EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS) end # Retrieve a Webhook def retrieve(id) - @client.make_request(:get, "webhooks/#{id}", MODEL_CLASS) + response = @client.make_request(:get, "webhooks/#{id}") + + EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS) end # Retrieve a list of Webhooks def all(params = {}) - @client.make_request(:get, 'webhooks', MODEL_CLASS, params) + filters = { 'key' => 'webhooks' } + + get_all_helper('webhooks', MODEL_CLASS, params, filters) end # Update a Webhook. def update(id, params = {}) - @client.make_request(:patch, "webhooks/#{id}", MODEL_CLASS, params) + response = @client.make_request(:patch, "webhooks/#{id}", params) + + EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS) end # Delete a Webhook. diff --git a/lib/easypost/utilities/constants.rb b/lib/easypost/utilities/constants.rb index 8c34ca8a..d3865a84 100644 --- a/lib/easypost/utilities/constants.rb +++ b/lib/easypost/utilities/constants.rb @@ -2,4 +2,5 @@ module EasyPost::InternalUtilities::Constants API_VERSION = 'v2' + FILTERS_KEY = '_filters' end diff --git a/lib/easypost/utilities/json.rb b/lib/easypost/utilities/json.rb index 02fbdc1e..59eb9167 100644 --- a/lib/easypost/utilities/json.rb +++ b/lib/easypost/utilities/json.rb @@ -2,7 +2,7 @@ module EasyPost::InternalUtilities::Json def self.convert_json_to_object(data, cls = EasyPost::Models::EasyPostObject) - data = JSON.parse(data) if data.is_a?(String) # Parse JSON to a Hash or Array if it's a string + data = parse_json(data) if data.is_a?(String) # Parse JSON to a Hash or Array if it's a string if data.is_a?(Array) # Deserialize array data into an array of objects data.map { |i| convert_json_to_object(i, cls) } @@ -13,6 +13,12 @@ def self.convert_json_to_object(data, cls = EasyPost::Models::EasyPostObject) # data is neither a Hash nor Array (but somehow was parsed as JSON? This should never happen) data end + end + + def self.parse_json(data) + return if data.nil? + + JSON.parse(data) rescue JSON::ParserError data # Not JSON, return the original data (used mostly when dealing with final values like strings, booleans, etc.) end diff --git a/spec/address_spec.rb b/spec/address_spec.rb index 8cf311c0..5b10c5cc 100644 --- a/spec/address_spec.rb +++ b/spec/address_spec.rb @@ -67,6 +67,7 @@ addresses = client.address.all( page_size: Fixture.page_size, ) + expect(addresses[EasyPost::InternalUtilities::Constants::FILTERS_KEY]).to be_a(Hash) addresses_array = addresses.addresses expect(addresses_array.count).to be <= Fixture.page_size @@ -89,6 +90,11 @@ # Did we actually get a new page? expect(first_page_first_id).not_to eq(next_page_first_id) + + # Verify that filters are being passed along for internal reference + expect(first_page[EasyPost::InternalUtilities::Constants::FILTERS_KEY]).to eq( + next_page[EasyPost::InternalUtilities::Constants::FILTERS_KEY], + ) rescue EasyPost::Errors::EndOfPaginationError => e # If we get an error, make sure it's because there are no more pages. expect(e.message).to eq(EasyPost::Constants::NO_MORE_PAGES) diff --git a/spec/billing_spec.rb b/spec/billing_spec.rb index 7586abfc..217a05f2 100644 --- a/spec/billing_spec.rb +++ b/spec/billing_spec.rb @@ -9,7 +9,7 @@ it 'fund wallet by using a payment method' do allow(described_class).to receive(:get_payment_method_info).and_return(['/credit_cards', 'cc_123']) allow(client).to receive(:make_request).with( - :post, '/credit_cards/cc_123/charges', EasyPost::Models::EasyPostObject, { amount: '2000' }, + :post, '/credit_cards/cc_123/charges', { amount: '2000' }, ) credit_card = client.billing.fund_wallet('2000', 'primary') diff --git a/spec/carrier_account_spec.rb b/spec/carrier_account_spec.rb index 28f209cb..e51c0cc2 100644 --- a/spec/carrier_account_spec.rb +++ b/spec/carrier_account_spec.rb @@ -20,7 +20,6 @@ it 'sends FedexAccount to the correct endpoint' do allow(client).to receive(:make_request).with( :post, 'carrier_accounts/register', - EasyPost::Models::CarrierAccount, { carrier_account: { type: 'FedexAccount' } }, ).and_return({ 'id' => 'ca_123' }) diff --git a/spec/event_spec.rb b/spec/event_spec.rb index 85b2bfd9..0d6f01f5 100644 --- a/spec/event_spec.rb +++ b/spec/event_spec.rb @@ -23,6 +23,7 @@ events = client.event.all( page_size: Fixture.page_size, ) + expect(events[EasyPost::InternalUtilities::Constants::FILTERS_KEY]).to be_a(Hash) events_array = events.events @@ -45,6 +46,9 @@ next_page_first_id = next_page.events.first.id expect(first_page_first_id).not_to eq(next_page_first_id) + expect(first_page[EasyPost::InternalUtilities::Constants::FILTERS_KEY]).to eq( + next_page[EasyPost::InternalUtilities::Constants::FILTERS_KEY], + ) rescue EasyPost::Errors::EndOfPaginationError => e # If we get an error, make sure it's because there are no more pages. expect(e.message).to eq(EasyPost::Constants::NO_MORE_PAGES) diff --git a/spec/insurance_spec.rb b/spec/insurance_spec.rb index c065adf3..bf8f5f72 100644 --- a/spec/insurance_spec.rb +++ b/spec/insurance_spec.rb @@ -40,6 +40,7 @@ all_insurances = client.insurance.all( page_size: Fixture.page_size, ) + expect(all_insurances[EasyPost::InternalUtilities::Constants::FILTERS_KEY]).to be_a(Hash) insurance_array = all_insurances.insurances @@ -62,6 +63,9 @@ next_page_first_id = next_page.insurances.first.id expect(first_page_first_id).not_to eq(next_page_first_id) + expect(first_page[EasyPost::InternalUtilities::Constants::FILTERS_KEY]).to eq( + next_page[EasyPost::InternalUtilities::Constants::FILTERS_KEY], + ) rescue EasyPost::Errors::EndOfPaginationError => e # If we get an error, make sure it's because there are no more pages. expect(e.message).to eq(EasyPost::Constants::NO_MORE_PAGES) diff --git a/spec/pickup_spec.rb b/spec/pickup_spec.rb index 92284ef2..fab4b43a 100644 --- a/spec/pickup_spec.rb +++ b/spec/pickup_spec.rb @@ -41,6 +41,7 @@ pickups = client.pickup.all( page_size: Fixture.page_size, ) + expect(pickups[EasyPost::InternalUtilities::Constants::FILTERS_KEY]).to be_a(Hash) pickups_array = pickups.pickups @@ -64,6 +65,9 @@ # Did we actually get a new page? expect(first_page_first_id).not_to eq(next_page_first_id) + expect(first_page[EasyPost::InternalUtilities::Constants::FILTERS_KEY]).to eq( + next_page[EasyPost::InternalUtilities::Constants::FILTERS_KEY], + ) rescue EasyPost::Errors::EndOfPaginationError => e # If we get an error, make sure it's because there are no more pages. expect(e.message).to eq(EasyPost::Constants::NO_MORE_PAGES) diff --git a/spec/referral_spec.rb b/spec/referral_spec.rb index 2c2ce1a0..66ed9c60 100644 --- a/spec/referral_spec.rb +++ b/spec/referral_spec.rb @@ -44,6 +44,7 @@ referral_customers = client.referral_customer.all( page_size: Fixture.page_size, ) + expect(referral_customers[EasyPost::InternalUtilities::Constants::FILTERS_KEY]).to be_a(Hash) referral_customers_array = referral_customers.referral_customers @@ -67,6 +68,9 @@ # Did we actually get a new page? expect(first_page_first_id).not_to eq(next_page_first_id) + expect(first_page[EasyPost::InternalUtilities::Constants::FILTERS_KEY]).to eq( + next_page[EasyPost::InternalUtilities::Constants::FILTERS_KEY], + ) rescue EasyPost::Errors::EndOfPaginationError => e # If we get an error, make sure it's because there are no more pages. expect(e.message).to eq(EasyPost::Constants::NO_MORE_PAGES) diff --git a/spec/refund_spec.rb b/spec/refund_spec.rb index ae07ff6f..31e05e0f 100644 --- a/spec/refund_spec.rb +++ b/spec/refund_spec.rb @@ -25,6 +25,7 @@ refunds = client.refund.all( page_size: Fixture.page_size, ) + expect(refunds[EasyPost::InternalUtilities::Constants::FILTERS_KEY]).to be_a(Hash) refunds_array = refunds.refunds @@ -48,6 +49,9 @@ # Did we actually get a new page? expect(first_page_first_id).not_to eq(next_page_first_id) + expect(first_page[EasyPost::InternalUtilities::Constants::FILTERS_KEY]).to eq( + next_page[EasyPost::InternalUtilities::Constants::FILTERS_KEY], + ) rescue EasyPost::Errors::EndOfPaginationError => e # If we get an error, make sure it's because there are no more pages. expect(e.message).to eq(EasyPost::Constants::NO_MORE_PAGES) diff --git a/spec/report_spec.rb b/spec/report_spec.rb index 468c7fd6..6913f5b2 100644 --- a/spec/report_spec.rb +++ b/spec/report_spec.rb @@ -66,6 +66,8 @@ type: Fixture.report_type, page_size: Fixture.page_size, ) + expect(reports[EasyPost::InternalUtilities::Constants::FILTERS_KEY]).to be_a(Hash) + expect(reports[:_filters][:type]).to eq(Fixture.report_type) reports_array = reports.reports @@ -90,6 +92,10 @@ # Did we actually get a new page? expect(first_page_first_id).not_to eq(next_page_first_id) + expect(first_page[EasyPost::InternalUtilities::Constants::FILTERS_KEY]).to eq( + next_page[EasyPost::InternalUtilities::Constants::FILTERS_KEY], + ) + expect(first_page[:_filters][:type]).to eq(next_page[:_filters][:type]) rescue EasyPost::Errors::EndOfPaginationError => e # If we get an error, make sure it's because there are no more pages. expect(e.message).to eq(EasyPost::Constants::NO_MORE_PAGES) diff --git a/spec/scan_form_spec.rb b/spec/scan_form_spec.rb index c885c606..a466f96e 100644 --- a/spec/scan_form_spec.rb +++ b/spec/scan_form_spec.rb @@ -38,6 +38,7 @@ scan_forms = client.scan_form.all( page_size: Fixture.page_size, ) + expect(scan_forms[EasyPost::InternalUtilities::Constants::FILTERS_KEY]).to be_a(Hash) scan_forms_array = scan_forms.scan_forms @@ -61,6 +62,9 @@ # Did we actually get a new page? expect(first_page_first_id).not_to eq(next_page_first_id) + expect(first_page[EasyPost::InternalUtilities::Constants::FILTERS_KEY]).to eq( + next_page[EasyPost::InternalUtilities::Constants::FILTERS_KEY], + ) rescue EasyPost::Errors::EndOfPaginationError => e # If we get an error, make sure it's because there are no more pages. expect(e.message).to eq(EasyPost::Constants::NO_MORE_PAGES) diff --git a/spec/shipment_spec.rb b/spec/shipment_spec.rb index 6fc22f2f..9dcb660a 100644 --- a/spec/shipment_spec.rb +++ b/spec/shipment_spec.rb @@ -81,6 +81,8 @@ shipments = client.shipment.all( page_size: Fixture.page_size, ) + expect(shipments[EasyPost::InternalUtilities::Constants::FILTERS_KEY]).to be_a(Hash) + expect(shipments[:_filters]).to include(:purchased, :include_children) shipments_array = shipments.shipments @@ -95,9 +97,8 @@ include_children: true, purchased: false, ) - - expect(shipments.include_children).to be true - expect(shipments.purchased).to be false + expect(shipments[:_filters][:include_children]).to be true + expect(shipments[:_filters][:purchased]).to be false end end @@ -115,6 +116,9 @@ # Did we actually get a new page? expect(first_page_first_id).not_to eq(next_page_first_id) + expect(first_page[EasyPost::InternalUtilities::Constants::FILTERS_KEY]).to eq( + next_page[EasyPost::InternalUtilities::Constants::FILTERS_KEY], + ) rescue EasyPost::Errors::EndOfPaginationError => e # If we get an error, make sure it's because there are no more pages. expect(e.message).to eq(EasyPost::Constants::NO_MORE_PAGES) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 3eeeaf01..23e939e7 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -20,7 +20,7 @@ add_filter '/spec/' add_filter 'lib/easypost/version.rb' enable_coverage :branch - minimum_coverage 91 + minimum_coverage 90 end require 'open-uri' diff --git a/spec/tracker_spec.rb b/spec/tracker_spec.rb index 35b63902..a116d1d4 100644 --- a/spec/tracker_spec.rb +++ b/spec/tracker_spec.rb @@ -35,6 +35,8 @@ trackers = client.tracker.all( page_size: Fixture.page_size, ) + expect(trackers[EasyPost::InternalUtilities::Constants::FILTERS_KEY]).to be_a(Hash) + expect(trackers[:_filters]).to include(:tracking_code, :carrier) trackers_array = trackers.trackers @@ -53,8 +55,8 @@ carrier: carrier, ) - expect(trackers.tracking_code).to eq(tracking_code) - expect(trackers.carrier).to eq(carrier) + expect(trackers[:_filters][:tracking_code]).to eq(tracking_code) + expect(trackers[:_filters][:carrier]).to eq(carrier) end end @@ -72,6 +74,9 @@ # Did we actually get a new page? expect(first_page_first_id).not_to eq(next_page_first_id) + expect(first_page[EasyPost::InternalUtilities::Constants::FILTERS_KEY]).to eq( + next_page[EasyPost::InternalUtilities::Constants::FILTERS_KEY], + ) rescue EasyPost::Errors::EndOfPaginationError => e # If we get an error, make sure it's because there are no more pages. expect(e.message).to eq(EasyPost::Constants::NO_MORE_PAGES)