diff --git a/lib/stripe/api_operations/create.rb b/lib/stripe/api_operations/create.rb index e55ba099a..140c9e971 100644 --- a/lib/stripe/api_operations/create.rb +++ b/lib/stripe/api_operations/create.rb @@ -4,8 +4,12 @@ module Stripe module APIOperations module Create def create(params = {}, opts = {}) - resp, opts = execute_resource_request(:post, resource_url, params, opts) - Util.convert_to_stripe_object(resp.data, opts) + request_stripe_object( + method: :post, + path: resource_url, + params: params, + opts: opts + ) end end end diff --git a/lib/stripe/api_operations/delete.rb b/lib/stripe/api_operations/delete.rb index 1023e90ea..59cf5a27b 100644 --- a/lib/stripe/api_operations/delete.rb +++ b/lib/stripe/api_operations/delete.rb @@ -18,17 +18,22 @@ module ClassMethods # api_key to be overwritten. See # {APIOperations::Request.execute_resource_request}. def delete(id, params = {}, opts = {}) - resp, opts = execute_resource_request(:delete, - "#{resource_url}/#{id}", - params, opts) - Util.convert_to_stripe_object(resp.data, opts) + request_stripe_object( + method: :delete, + path: "#{resource_url}/#{id}", + params: params, + opts: opts + ) end end def delete(params = {}, opts = {}) - resp, opts = execute_resource_request(:delete, resource_url, - params, opts) - initialize_from(resp.data, opts) + request_stripe_object( + method: :delete, + path: resource_url, + params: params, + opts: opts + ) end def self.included(base) diff --git a/lib/stripe/api_operations/list.rb b/lib/stripe/api_operations/list.rb index 939c09389..fba636407 100644 --- a/lib/stripe/api_operations/list.rb +++ b/lib/stripe/api_operations/list.rb @@ -4,15 +4,12 @@ module Stripe module APIOperations module List def list(filters = {}, opts = {}) - opts = Util.normalize_opts(opts) - - resp, opts = execute_resource_request(:get, resource_url, filters, opts) - obj = ListObject.construct_from(resp.data, opts) - - # set filters so that we can fetch the same limit, expansions, and - # predicates when accessing the next and previous pages - obj.filters = filters.dup - obj + request_stripe_object( + method: :get, + path: resource_url, + params: filters, + opts: opts + ) end end end diff --git a/lib/stripe/api_operations/nested_resource.rb b/lib/stripe/api_operations/nested_resource.rb index 29dc8a8b6..09b570944 100644 --- a/lib/stripe/api_operations/nested_resource.rb +++ b/lib/stripe/api_operations/nested_resource.rb @@ -26,46 +26,74 @@ def nested_resource_class_methods(resource, path: nil, operations: nil, end operations.each do |operation| - case operation - when :create - define_singleton_method(:"create_#{resource}") \ + define_operation( + resource, + operation, + resource_url_method, + resource_plural + ) + end + end + + private def define_operation( + resource, + operation, + resource_url_method, + resource_plural + ) + case operation + when :create + define_singleton_method(:"create_#{resource}") \ do |id, params = {}, opts = {}| - url = send(resource_url_method, id) - resp, opts = execute_resource_request(:post, url, params, opts) - Util.convert_to_stripe_object(resp.data, opts) - end - when :retrieve - define_singleton_method(:"retrieve_#{resource}") \ + request_stripe_object( + method: :post, + path: send(resource_url_method, id), + params: params, + opts: opts + ) + end + when :retrieve + define_singleton_method(:"retrieve_#{resource}") \ do |id, nested_id, opts = {}| - url = send(resource_url_method, id, nested_id) - resp, opts = execute_resource_request(:get, url, {}, opts) - Util.convert_to_stripe_object(resp.data, opts) - end - when :update - define_singleton_method(:"update_#{resource}") \ + request_stripe_object( + method: :get, + path: send(resource_url_method, id, nested_id), + params: {}, + opts: opts + ) + end + when :update + define_singleton_method(:"update_#{resource}") \ do |id, nested_id, params = {}, opts = {}| - url = send(resource_url_method, id, nested_id) - resp, opts = execute_resource_request(:post, url, params, opts) - Util.convert_to_stripe_object(resp.data, opts) - end - when :delete - define_singleton_method(:"delete_#{resource}") \ + request_stripe_object( + method: :post, + path: send(resource_url_method, id, nested_id), + params: params, + opts: opts + ) + end + when :delete + define_singleton_method(:"delete_#{resource}") \ do |id, nested_id, params = {}, opts = {}| - url = send(resource_url_method, id, nested_id) - resp, opts = execute_resource_request(:delete, url, params, - opts) - Util.convert_to_stripe_object(resp.data, opts) - end - when :list - define_singleton_method(:"list_#{resource_plural}") \ + request_stripe_object( + method: :delete, + path: send(resource_url_method, id, nested_id), + params: params, + opts: opts + ) + end + when :list + define_singleton_method(:"list_#{resource_plural}") \ do |id, params = {}, opts = {}| - url = send(resource_url_method, id) - resp, opts = execute_resource_request(:get, url, params, opts) - Util.convert_to_stripe_object(resp.data, opts) - end - else - raise ArgumentError, "Unknown operation: #{operation.inspect}" + request_stripe_object( + method: :get, + path: send(resource_url_method, id), + params: params, + opts: opts + ) end + else + raise ArgumentError, "Unknown operation: #{operation.inspect}" end end end diff --git a/lib/stripe/api_operations/request.rb b/lib/stripe/api_operations/request.rb index f4dcf3d42..8544dedd6 100644 --- a/lib/stripe/api_operations/request.rb +++ b/lib/stripe/api_operations/request.rb @@ -24,6 +24,11 @@ def execute_resource_request_stream(method, url, ) end + private def request_stripe_object(method:, path:, params:, opts: {}) + resp, opts = execute_resource_request(method, path, params, opts) + Util.convert_to_stripe_object_with_params(resp.data, params, opts) + end + private def execute_resource_request_internal(client_request_method_sym, method, url, params, opts, @@ -122,6 +127,11 @@ def self.included(base) ) end + private def request_stripe_object(method:, path:, params:, opts: {}) + resp, opts = execute_resource_request(method, path, params, opts) + Util.convert_to_stripe_object_with_params(resp.data, params, opts) + end + # See notes on `alias` above. alias request execute_resource_request end diff --git a/lib/stripe/api_operations/save.rb b/lib/stripe/api_operations/save.rb index 18ba642f9..8b08c88b5 100644 --- a/lib/stripe/api_operations/save.rb +++ b/lib/stripe/api_operations/save.rb @@ -24,9 +24,12 @@ def update(id, params = {}, opts = {}) end end - resp, opts = execute_resource_request(:post, "#{resource_url}/#{id}", - params, opts) - Util.convert_to_stripe_object(resp.data, opts) + request_stripe_object( + method: :post, + path: "#{resource_url}/#{id}", + params: params, + opts: opts + ) end end diff --git a/lib/stripe/api_operations/search.rb b/lib/stripe/api_operations/search.rb index b13b47ac0..ac9dc26b7 100644 --- a/lib/stripe/api_operations/search.rb +++ b/lib/stripe/api_operations/search.rb @@ -4,15 +4,12 @@ module Stripe module APIOperations module Search def _search(search_url, filters = {}, opts = {}) - opts = Util.normalize_opts(opts) - - resp, opts = execute_resource_request(:get, search_url, filters, opts) - obj = SearchResultObject.construct_from(resp.data, opts) - - # set filters so that we can fetch the same limit and query - # when accessing the next page - obj.filters = filters.dup - obj + request_stripe_object( + method: :get, + path: search_url, + params: filters, + opts: opts + ) end end end diff --git a/lib/stripe/api_resource.rb b/lib/stripe/api_resource.rb index 288655a1a..a1bac6ae9 100644 --- a/lib/stripe/api_resource.rb +++ b/lib/stripe/api_resource.rb @@ -97,7 +97,7 @@ def request_stripe_object(method:, path:, params:, opts: {}) if Util.object_name_matches_class?(resp.data[:object], self.class) initialize_from(resp.data, opts) else - Util.convert_to_stripe_object(resp.data, opts) + Util.convert_to_stripe_object_with_params(resp.data, params, opts) end end diff --git a/lib/stripe/resources/account.rb b/lib/stripe/resources/account.rb index 00d95f2c3..2f05ab265 100644 --- a/lib/stripe/resources/account.rb +++ b/lib/stripe/resources/account.rb @@ -37,23 +37,21 @@ def reject(params = {}, opts = {}) end def self.persons(account, params = {}, opts = {}) - resp, opts = execute_resource_request( - :get, - format("/v1/accounts/%s/persons", { account: CGI.escape(account) }), - params, - opts + request_stripe_object( + method: :get, + path: format("/v1/accounts/%s/persons", { account: CGI.escape(account) }), + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end def self.reject(account, params = {}, opts = {}) - resp, opts = execute_resource_request( - :post, - format("/v1/accounts/%s/reject", { account: CGI.escape(account) }), - params, - opts + request_stripe_object( + method: :post, + path: format("/v1/accounts/%s/reject", { account: CGI.escape(account) }), + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end save_nested_resource :external_account diff --git a/lib/stripe/resources/apps/secret.rb b/lib/stripe/resources/apps/secret.rb index 994e9cefb..a37666f3e 100644 --- a/lib/stripe/resources/apps/secret.rb +++ b/lib/stripe/resources/apps/secret.rb @@ -10,23 +10,21 @@ class Secret < APIResource OBJECT_NAME = "apps.secret" def self.delete_where(params = {}, opts = {}) - resp, opts = execute_resource_request( - :post, - "/v1/apps/secrets/delete", - params, - opts + request_stripe_object( + method: :post, + path: "/v1/apps/secrets/delete", + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end def self.find(params = {}, opts = {}) - resp, opts = execute_resource_request( - :get, - "/v1/apps/secrets/find", - params, - opts + request_stripe_object( + method: :get, + path: "/v1/apps/secrets/find", + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end end end diff --git a/lib/stripe/resources/charge.rb b/lib/stripe/resources/charge.rb index 358940ca4..b7603443a 100644 --- a/lib/stripe/resources/charge.rb +++ b/lib/stripe/resources/charge.rb @@ -20,13 +20,12 @@ def capture(params = {}, opts = {}) end def self.capture(charge, params = {}, opts = {}) - resp, opts = execute_resource_request( - :post, - format("/v1/charges/%s/capture", { charge: CGI.escape(charge) }), - params, - opts + request_stripe_object( + method: :post, + path: format("/v1/charges/%s/capture", { charge: CGI.escape(charge) }), + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end def self.search(params = {}, opts = {}) diff --git a/lib/stripe/resources/checkout/session.rb b/lib/stripe/resources/checkout/session.rb index 5731545c4..c499c88b2 100644 --- a/lib/stripe/resources/checkout/session.rb +++ b/lib/stripe/resources/checkout/session.rb @@ -22,13 +22,12 @@ def expire(params = {}, opts = {}) end def self.expire(session, params = {}, opts = {}) - resp, opts = execute_resource_request( - :post, - format("/v1/checkout/sessions/%s/expire", { session: CGI.escape(session) }), - params, - opts + request_stripe_object( + method: :post, + path: format("/v1/checkout/sessions/%s/expire", { session: CGI.escape(session) }), + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end end end diff --git a/lib/stripe/resources/credit_note.rb b/lib/stripe/resources/credit_note.rb index b0141ab31..9779b6dae 100644 --- a/lib/stripe/resources/credit_note.rb +++ b/lib/stripe/resources/credit_note.rb @@ -19,23 +19,30 @@ def void_credit_note(params = {}, opts = {}) end def self.void_credit_note(id, params = {}, opts = {}) - resp, opts = execute_resource_request( - :post, - format("/v1/credit_notes/%s/void", { id: CGI.escape(id) }), - params, - opts + request_stripe_object( + method: :post, + path: format("/v1/credit_notes/%s/void", { id: CGI.escape(id) }), + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end def self.preview(params, opts = {}) - resp, opts = execute_resource_request(:get, resource_url + "/preview", params, opts) - Util.convert_to_stripe_object(resp.data, opts) + request_stripe_object( + method: :get, + path: resource_url + "/preview", + params: params, + opts: opts + ) end def self.list_preview_line_items(params, opts = {}) - resp, opts = execute_resource_request(:get, resource_url + "/preview/lines", params, opts) - Util.convert_to_stripe_object(resp.data, opts) + request_stripe_object( + method: :get, + path: resource_url + "/preview/lines", + params: params, + opts: opts + ) end end end diff --git a/lib/stripe/resources/customer.rb b/lib/stripe/resources/customer.rb index 40deca823..7690f19c1 100644 --- a/lib/stripe/resources/customer.rb +++ b/lib/stripe/resources/customer.rb @@ -45,23 +45,21 @@ def retrieve_payment_method(payment_method, params = {}, opts = {}) end def self.create_funding_instructions(customer, params = {}, opts = {}) - resp, opts = execute_resource_request( - :post, - format("/v1/customers/%s/funding_instructions", { customer: CGI.escape(customer) }), - params, - opts + request_stripe_object( + method: :post, + path: format("/v1/customers/%s/funding_instructions", { customer: CGI.escape(customer) }), + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end def self.list_payment_methods(customer, params = {}, opts = {}) - resp, opts = execute_resource_request( - :get, - format("/v1/customers/%s/payment_methods", { customer: CGI.escape(customer) }), - params, - opts + request_stripe_object( + method: :get, + path: format("/v1/customers/%s/payment_methods", { customer: CGI.escape(customer) }), + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end def self.retrieve_payment_method( @@ -70,13 +68,12 @@ def self.retrieve_payment_method( params = {}, opts = {} ) - resp, opts = execute_resource_request( - :get, - format("/v1/customers/%s/payment_methods/%s", { customer: CGI.escape(customer), payment_method: CGI.escape(payment_method) }), - params, - opts + request_stripe_object( + method: :get, + path: format("/v1/customers/%s/payment_methods/%s", { customer: CGI.escape(customer), payment_method: CGI.escape(payment_method) }), + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end custom_method :delete_discount, http_verb: :delete, http_path: "discount" @@ -97,8 +94,11 @@ class << self # so you must call `refresh` on it to get a new version with the # discount removed. def delete_discount - resp, opts = execute_resource_request(:delete, resource_url + "/discount") - Util.convert_to_stripe_object(resp.data, opts) + request_stripe_object( + method: :delete, + path: resource_url + "/discount", + params: {} + ) end def self.search(params = {}, opts = {}) @@ -120,13 +120,12 @@ def self.retrieve_cash_balance( if !opts_or_unused_nested_id.nil? && opts_or_unused_nested_id.class == Hash && opts.empty? opts = opts_or_unused_nested_id end - resp, opts = execute_resource_request( - :get, - format("/v1/customers/%s/cash_balance", { customer: CGI.escape(customer) }), - {}, - opts + request_stripe_object( + method: :get, + path: format("/v1/customers/%s/cash_balance", { customer: CGI.escape(customer) }), + params: {}, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end def self.update_cash_balance( @@ -140,13 +139,12 @@ def self.update_cash_balance( raise ArgumentError, "update_cash_balance requires the second argument always be nil for legacy reasons." end - resp, opts = execute_resource_request( - :post, - format("/v1/customers/%s/cash_balance", { customer: CGI.escape(customer) }), - params, - opts + request_stripe_object( + method: :post, + path: format("/v1/customers/%s/cash_balance", { customer: CGI.escape(customer) }), + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end end end diff --git a/lib/stripe/resources/dispute.rb b/lib/stripe/resources/dispute.rb index 492aa1d44..9689b1e15 100644 --- a/lib/stripe/resources/dispute.rb +++ b/lib/stripe/resources/dispute.rb @@ -18,13 +18,12 @@ def close(params = {}, opts = {}) end def self.close(dispute, params = {}, opts = {}) - resp, opts = execute_resource_request( - :post, - format("/v1/disputes/%s/close", { dispute: CGI.escape(dispute) }), - params, - opts + request_stripe_object( + method: :post, + path: format("/v1/disputes/%s/close", { dispute: CGI.escape(dispute) }), + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end end end diff --git a/lib/stripe/resources/financial_connections/account.rb b/lib/stripe/resources/financial_connections/account.rb index 90b387e0b..6d7f466c3 100644 --- a/lib/stripe/resources/financial_connections/account.rb +++ b/lib/stripe/resources/financial_connections/account.rb @@ -36,33 +36,30 @@ def refresh_account(params = {}, opts = {}) end def self.disconnect(account, params = {}, opts = {}) - resp, opts = execute_resource_request( - :post, - format("/v1/financial_connections/accounts/%s/disconnect", { account: CGI.escape(account) }), - params, - opts + request_stripe_object( + method: :post, + path: format("/v1/financial_connections/accounts/%s/disconnect", { account: CGI.escape(account) }), + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end def self.list_owners(account, params = {}, opts = {}) - resp, opts = execute_resource_request( - :get, - format("/v1/financial_connections/accounts/%s/owners", { account: CGI.escape(account) }), - params, - opts + request_stripe_object( + method: :get, + path: format("/v1/financial_connections/accounts/%s/owners", { account: CGI.escape(account) }), + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end def self.refresh_account(account, params = {}, opts = {}) - resp, opts = execute_resource_request( - :post, - format("/v1/financial_connections/accounts/%s/refresh", { account: CGI.escape(account) }), - params, - opts + request_stripe_object( + method: :post, + path: format("/v1/financial_connections/accounts/%s/refresh", { account: CGI.escape(account) }), + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end end end diff --git a/lib/stripe/resources/identity/verification_session.rb b/lib/stripe/resources/identity/verification_session.rb index ee51c8fd0..6af101979 100644 --- a/lib/stripe/resources/identity/verification_session.rb +++ b/lib/stripe/resources/identity/verification_session.rb @@ -29,23 +29,21 @@ def redact(params = {}, opts = {}) end def self.cancel(session, params = {}, opts = {}) - resp, opts = execute_resource_request( - :post, - format("/v1/identity/verification_sessions/%s/cancel", { session: CGI.escape(session) }), - params, - opts + request_stripe_object( + method: :post, + path: format("/v1/identity/verification_sessions/%s/cancel", { session: CGI.escape(session) }), + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end def self.redact(session, params = {}, opts = {}) - resp, opts = execute_resource_request( - :post, - format("/v1/identity/verification_sessions/%s/redact", { session: CGI.escape(session) }), - params, - opts + request_stripe_object( + method: :post, + path: format("/v1/identity/verification_sessions/%s/redact", { session: CGI.escape(session) }), + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end end end diff --git a/lib/stripe/resources/invoice.rb b/lib/stripe/resources/invoice.rb index 42bb9407f..e6bfd88e6 100644 --- a/lib/stripe/resources/invoice.rb +++ b/lib/stripe/resources/invoice.rb @@ -57,63 +57,66 @@ def void_invoice(params = {}, opts = {}) end def self.finalize_invoice(invoice, params = {}, opts = {}) - resp, opts = execute_resource_request( - :post, - format("/v1/invoices/%s/finalize", { invoice: CGI.escape(invoice) }), - params, - opts + request_stripe_object( + method: :post, + path: format("/v1/invoices/%s/finalize", { invoice: CGI.escape(invoice) }), + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end def self.mark_uncollectible(invoice, params = {}, opts = {}) - resp, opts = execute_resource_request( - :post, - format("/v1/invoices/%s/mark_uncollectible", { invoice: CGI.escape(invoice) }), - params, - opts + request_stripe_object( + method: :post, + path: format("/v1/invoices/%s/mark_uncollectible", { invoice: CGI.escape(invoice) }), + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end def self.pay(invoice, params = {}, opts = {}) - resp, opts = execute_resource_request( - :post, - format("/v1/invoices/%s/pay", { invoice: CGI.escape(invoice) }), - params, - opts + request_stripe_object( + method: :post, + path: format("/v1/invoices/%s/pay", { invoice: CGI.escape(invoice) }), + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end def self.send_invoice(invoice, params = {}, opts = {}) - resp, opts = execute_resource_request( - :post, - format("/v1/invoices/%s/send", { invoice: CGI.escape(invoice) }), - params, - opts + request_stripe_object( + method: :post, + path: format("/v1/invoices/%s/send", { invoice: CGI.escape(invoice) }), + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end def self.void_invoice(invoice, params = {}, opts = {}) - resp, opts = execute_resource_request( - :post, - format("/v1/invoices/%s/void", { invoice: CGI.escape(invoice) }), - params, - opts + request_stripe_object( + method: :post, + path: format("/v1/invoices/%s/void", { invoice: CGI.escape(invoice) }), + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end def self.upcoming(params, opts = {}) - resp, opts = execute_resource_request(:get, resource_url + "/upcoming", params, opts) - Util.convert_to_stripe_object(resp.data, opts) + request_stripe_object( + method: :get, + path: resource_url + "/upcoming", + params: params, + opts: opts + ) end def self.list_upcoming_line_items(params, opts = {}) - resp, opts = execute_resource_request(:get, resource_url + "/upcoming/lines", params, opts) - Util.convert_to_stripe_object(resp.data, opts) + request_stripe_object( + method: :get, + path: resource_url + "/upcoming/lines", + params: params, + opts: opts + ) end def self.search(params = {}, opts = {}) diff --git a/lib/stripe/resources/issuing/authorization.rb b/lib/stripe/resources/issuing/authorization.rb index 3361c8f21..a90dc6d85 100644 --- a/lib/stripe/resources/issuing/authorization.rb +++ b/lib/stripe/resources/issuing/authorization.rb @@ -28,23 +28,21 @@ def decline(params = {}, opts = {}) end def self.approve(authorization, params = {}, opts = {}) - resp, opts = execute_resource_request( - :post, - format("/v1/issuing/authorizations/%s/approve", { authorization: CGI.escape(authorization) }), - params, - opts + request_stripe_object( + method: :post, + path: format("/v1/issuing/authorizations/%s/approve", { authorization: CGI.escape(authorization) }), + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end def self.decline(authorization, params = {}, opts = {}) - resp, opts = execute_resource_request( - :post, - format("/v1/issuing/authorizations/%s/decline", { authorization: CGI.escape(authorization) }), - params, - opts + request_stripe_object( + method: :post, + path: format("/v1/issuing/authorizations/%s/decline", { authorization: CGI.escape(authorization) }), + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end end end diff --git a/lib/stripe/resources/issuing/card.rb b/lib/stripe/resources/issuing/card.rb index 7ccdf38cb..49f5b3acb 100644 --- a/lib/stripe/resources/issuing/card.rb +++ b/lib/stripe/resources/issuing/card.rb @@ -20,13 +20,12 @@ def details(params = {}, opts = {}) end def self.details(card, params = {}, opts = {}) - resp, opts = execute_resource_request( - :get, - format("/v1/issuing/cards/%s/details", { card: CGI.escape(card) }), - params, - opts + request_stripe_object( + method: :get, + path: format("/v1/issuing/cards/%s/details", { card: CGI.escape(card) }), + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end end end diff --git a/lib/stripe/resources/issuing/dispute.rb b/lib/stripe/resources/issuing/dispute.rb index 3438fbbc8..a32a44e32 100644 --- a/lib/stripe/resources/issuing/dispute.rb +++ b/lib/stripe/resources/issuing/dispute.rb @@ -20,13 +20,12 @@ def submit(params = {}, opts = {}) end def self.submit(dispute, params = {}, opts = {}) - resp, opts = execute_resource_request( - :post, - format("/v1/issuing/disputes/%s/submit", { dispute: CGI.escape(dispute) }), - params, - opts + request_stripe_object( + method: :post, + path: format("/v1/issuing/disputes/%s/submit", { dispute: CGI.escape(dispute) }), + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end end end diff --git a/lib/stripe/resources/order.rb b/lib/stripe/resources/order.rb index 824a04580..4549c9d30 100644 --- a/lib/stripe/resources/order.rb +++ b/lib/stripe/resources/order.rb @@ -46,43 +46,39 @@ def submit(params = {}, opts = {}) end def self.cancel(id, params = {}, opts = {}) - resp, opts = execute_resource_request( - :post, - format("/v1/orders/%s/cancel", { id: CGI.escape(id) }), - params, - opts + request_stripe_object( + method: :post, + path: format("/v1/orders/%s/cancel", { id: CGI.escape(id) }), + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end def self.list_line_items(id, params = {}, opts = {}) - resp, opts = execute_resource_request( - :get, - format("/v1/orders/%s/line_items", { id: CGI.escape(id) }), - params, - opts + request_stripe_object( + method: :get, + path: format("/v1/orders/%s/line_items", { id: CGI.escape(id) }), + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end def self.reopen(id, params = {}, opts = {}) - resp, opts = execute_resource_request( - :post, - format("/v1/orders/%s/reopen", { id: CGI.escape(id) }), - params, - opts + request_stripe_object( + method: :post, + path: format("/v1/orders/%s/reopen", { id: CGI.escape(id) }), + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end def self.submit(id, params = {}, opts = {}) - resp, opts = execute_resource_request( - :post, - format("/v1/orders/%s/submit", { id: CGI.escape(id) }), - params, - opts + request_stripe_object( + method: :post, + path: format("/v1/orders/%s/submit", { id: CGI.escape(id) }), + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end end end diff --git a/lib/stripe/resources/payment_intent.rb b/lib/stripe/resources/payment_intent.rb index 33e0ff7c3..05ca3e9e0 100644 --- a/lib/stripe/resources/payment_intent.rb +++ b/lib/stripe/resources/payment_intent.rb @@ -65,63 +65,57 @@ def verify_microdeposits(params = {}, opts = {}) end def self.apply_customer_balance(intent, params = {}, opts = {}) - resp, opts = execute_resource_request( - :post, - format("/v1/payment_intents/%s/apply_customer_balance", { intent: CGI.escape(intent) }), - params, - opts + request_stripe_object( + method: :post, + path: format("/v1/payment_intents/%s/apply_customer_balance", { intent: CGI.escape(intent) }), + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end def self.cancel(intent, params = {}, opts = {}) - resp, opts = execute_resource_request( - :post, - format("/v1/payment_intents/%s/cancel", { intent: CGI.escape(intent) }), - params, - opts + request_stripe_object( + method: :post, + path: format("/v1/payment_intents/%s/cancel", { intent: CGI.escape(intent) }), + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end def self.capture(intent, params = {}, opts = {}) - resp, opts = execute_resource_request( - :post, - format("/v1/payment_intents/%s/capture", { intent: CGI.escape(intent) }), - params, - opts + request_stripe_object( + method: :post, + path: format("/v1/payment_intents/%s/capture", { intent: CGI.escape(intent) }), + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end def self.confirm(intent, params = {}, opts = {}) - resp, opts = execute_resource_request( - :post, - format("/v1/payment_intents/%s/confirm", { intent: CGI.escape(intent) }), - params, - opts + request_stripe_object( + method: :post, + path: format("/v1/payment_intents/%s/confirm", { intent: CGI.escape(intent) }), + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end def self.increment_authorization(intent, params = {}, opts = {}) - resp, opts = execute_resource_request( - :post, - format("/v1/payment_intents/%s/increment_authorization", { intent: CGI.escape(intent) }), - params, - opts + request_stripe_object( + method: :post, + path: format("/v1/payment_intents/%s/increment_authorization", { intent: CGI.escape(intent) }), + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end def self.verify_microdeposits(intent, params = {}, opts = {}) - resp, opts = execute_resource_request( - :post, - format("/v1/payment_intents/%s/verify_microdeposits", { intent: CGI.escape(intent) }), - params, - opts + request_stripe_object( + method: :post, + path: format("/v1/payment_intents/%s/verify_microdeposits", { intent: CGI.escape(intent) }), + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end def self.search(params = {}, opts = {}) diff --git a/lib/stripe/resources/payment_link.rb b/lib/stripe/resources/payment_link.rb index 4a4647356..03739f566 100644 --- a/lib/stripe/resources/payment_link.rb +++ b/lib/stripe/resources/payment_link.rb @@ -19,13 +19,12 @@ def list_line_items(params = {}, opts = {}) end def self.list_line_items(payment_link, params = {}, opts = {}) - resp, opts = execute_resource_request( - :get, - format("/v1/payment_links/%s/line_items", { payment_link: CGI.escape(payment_link) }), - params, - opts + request_stripe_object( + method: :get, + path: format("/v1/payment_links/%s/line_items", { payment_link: CGI.escape(payment_link) }), + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end end end diff --git a/lib/stripe/resources/payment_method.rb b/lib/stripe/resources/payment_method.rb index 86cddc17d..18966b427 100644 --- a/lib/stripe/resources/payment_method.rb +++ b/lib/stripe/resources/payment_method.rb @@ -28,23 +28,21 @@ def detach(params = {}, opts = {}) end def self.attach(payment_method, params = {}, opts = {}) - resp, opts = execute_resource_request( - :post, - format("/v1/payment_methods/%s/attach", { payment_method: CGI.escape(payment_method) }), - params, - opts + request_stripe_object( + method: :post, + path: format("/v1/payment_methods/%s/attach", { payment_method: CGI.escape(payment_method) }), + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end def self.detach(payment_method, params = {}, opts = {}) - resp, opts = execute_resource_request( - :post, - format("/v1/payment_methods/%s/detach", { payment_method: CGI.escape(payment_method) }), - params, - opts + request_stripe_object( + method: :post, + path: format("/v1/payment_methods/%s/detach", { payment_method: CGI.escape(payment_method) }), + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end end end diff --git a/lib/stripe/resources/payout.rb b/lib/stripe/resources/payout.rb index 38a505b07..ede6d244f 100644 --- a/lib/stripe/resources/payout.rb +++ b/lib/stripe/resources/payout.rb @@ -28,23 +28,21 @@ def reverse(params = {}, opts = {}) end def self.cancel(payout, params = {}, opts = {}) - resp, opts = execute_resource_request( - :post, - format("/v1/payouts/%s/cancel", { payout: CGI.escape(payout) }), - params, - opts + request_stripe_object( + method: :post, + path: format("/v1/payouts/%s/cancel", { payout: CGI.escape(payout) }), + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end def self.reverse(payout, params = {}, opts = {}) - resp, opts = execute_resource_request( - :post, - format("/v1/payouts/%s/reverse", { payout: CGI.escape(payout) }), - params, - opts + request_stripe_object( + method: :post, + path: format("/v1/payouts/%s/reverse", { payout: CGI.escape(payout) }), + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end end end diff --git a/lib/stripe/resources/quote.rb b/lib/stripe/resources/quote.rb index fc394304e..195e0c3ba 100644 --- a/lib/stripe/resources/quote.rb +++ b/lib/stripe/resources/quote.rb @@ -55,53 +55,48 @@ def list_line_items(params = {}, opts = {}) end def self.accept(quote, params = {}, opts = {}) - resp, opts = execute_resource_request( - :post, - format("/v1/quotes/%s/accept", { quote: CGI.escape(quote) }), - params, - opts + request_stripe_object( + method: :post, + path: format("/v1/quotes/%s/accept", { quote: CGI.escape(quote) }), + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end def self.cancel(quote, params = {}, opts = {}) - resp, opts = execute_resource_request( - :post, - format("/v1/quotes/%s/cancel", { quote: CGI.escape(quote) }), - params, - opts + request_stripe_object( + method: :post, + path: format("/v1/quotes/%s/cancel", { quote: CGI.escape(quote) }), + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end def self.finalize_quote(quote, params = {}, opts = {}) - resp, opts = execute_resource_request( - :post, - format("/v1/quotes/%s/finalize", { quote: CGI.escape(quote) }), - params, - opts + request_stripe_object( + method: :post, + path: format("/v1/quotes/%s/finalize", { quote: CGI.escape(quote) }), + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end def self.list_computed_upfront_line_items(quote, params = {}, opts = {}) - resp, opts = execute_resource_request( - :get, - format("/v1/quotes/%s/computed_upfront_line_items", { quote: CGI.escape(quote) }), - params, - opts + request_stripe_object( + method: :get, + path: format("/v1/quotes/%s/computed_upfront_line_items", { quote: CGI.escape(quote) }), + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end def self.list_line_items(quote, params = {}, opts = {}) - resp, opts = execute_resource_request( - :get, - format("/v1/quotes/%s/line_items", { quote: CGI.escape(quote) }), - params, - opts + request_stripe_object( + method: :get, + path: format("/v1/quotes/%s/line_items", { quote: CGI.escape(quote) }), + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end def pdf(params = {}, opts = {}, &read_body_chunk_block) diff --git a/lib/stripe/resources/refund.rb b/lib/stripe/resources/refund.rb index 767f69afb..c896d0ef6 100644 --- a/lib/stripe/resources/refund.rb +++ b/lib/stripe/resources/refund.rb @@ -19,13 +19,12 @@ def cancel(params = {}, opts = {}) end def self.cancel(refund, params = {}, opts = {}) - resp, opts = execute_resource_request( - :post, - format("/v1/refunds/%s/cancel", { refund: CGI.escape(refund) }), - params, - opts + request_stripe_object( + method: :post, + path: format("/v1/refunds/%s/cancel", { refund: CGI.escape(refund) }), + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end def test_helpers @@ -36,13 +35,12 @@ class TestHelpers < APIResourceTestHelpers RESOURCE_CLASS = Refund def self.expire(refund, params = {}, opts = {}) - resp, opts = execute_resource_request( - :post, - format("/v1/test_helpers/refunds/%s/expire", { refund: CGI.escape(refund) }), - params, - opts + request_stripe_object( + method: :post, + path: format("/v1/test_helpers/refunds/%s/expire", { refund: CGI.escape(refund) }), + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end def expire(params = {}, opts = {}) diff --git a/lib/stripe/resources/review.rb b/lib/stripe/resources/review.rb index 9a93b46f9..b961bc838 100644 --- a/lib/stripe/resources/review.rb +++ b/lib/stripe/resources/review.rb @@ -17,13 +17,12 @@ def approve(params = {}, opts = {}) end def self.approve(review, params = {}, opts = {}) - resp, opts = execute_resource_request( - :post, - format("/v1/reviews/%s/approve", { review: CGI.escape(review) }), - params, - opts + request_stripe_object( + method: :post, + path: format("/v1/reviews/%s/approve", { review: CGI.escape(review) }), + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end end end diff --git a/lib/stripe/resources/setup_intent.rb b/lib/stripe/resources/setup_intent.rb index b3b9d3320..dff2d33b5 100644 --- a/lib/stripe/resources/setup_intent.rb +++ b/lib/stripe/resources/setup_intent.rb @@ -37,33 +37,30 @@ def verify_microdeposits(params = {}, opts = {}) end def self.cancel(intent, params = {}, opts = {}) - resp, opts = execute_resource_request( - :post, - format("/v1/setup_intents/%s/cancel", { intent: CGI.escape(intent) }), - params, - opts + request_stripe_object( + method: :post, + path: format("/v1/setup_intents/%s/cancel", { intent: CGI.escape(intent) }), + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end def self.confirm(intent, params = {}, opts = {}) - resp, opts = execute_resource_request( - :post, - format("/v1/setup_intents/%s/confirm", { intent: CGI.escape(intent) }), - params, - opts + request_stripe_object( + method: :post, + path: format("/v1/setup_intents/%s/confirm", { intent: CGI.escape(intent) }), + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end def self.verify_microdeposits(intent, params = {}, opts = {}) - resp, opts = execute_resource_request( - :post, - format("/v1/setup_intents/%s/verify_microdeposits", { intent: CGI.escape(intent) }), - params, - opts + request_stripe_object( + method: :post, + path: format("/v1/setup_intents/%s/verify_microdeposits", { intent: CGI.escape(intent) }), + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end end end diff --git a/lib/stripe/resources/source.rb b/lib/stripe/resources/source.rb index 019427a92..feaf54c4a 100644 --- a/lib/stripe/resources/source.rb +++ b/lib/stripe/resources/source.rb @@ -22,13 +22,12 @@ def verify(params = {}, opts = {}) end def self.verify(source, params = {}, opts = {}) - resp, opts = execute_resource_request( - :post, - format("/v1/sources/%s/verify", { source: CGI.escape(source) }), - params, - opts + request_stripe_object( + method: :post, + path: format("/v1/sources/%s/verify", { source: CGI.escape(source) }), + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end def detach(params = {}, opts = {}) @@ -45,9 +44,12 @@ def detach(params = {}, opts = {}) end def source_transactions(params = {}, opts = {}) - resp, opts = execute_resource_request(:get, resource_url + "/source_transactions", params, - opts) - Util.convert_to_stripe_object(resp.data, opts) + request_stripe_object( + method: :get, + path: resource_url + "/source_transactions", + params: params, + opts: opts + ) end extend Gem::Deprecate deprecate :source_transactions, :"Source.list_source_transactions", 2020, 1 diff --git a/lib/stripe/resources/subscription.rb b/lib/stripe/resources/subscription.rb index 87b67e6e5..986f5c8de 100644 --- a/lib/stripe/resources/subscription.rb +++ b/lib/stripe/resources/subscription.rb @@ -21,13 +21,12 @@ def delete_discount(params = {}, opts = {}) end def self.delete_discount(subscription_exposed_id, params = {}, opts = {}) - resp, opts = execute_resource_request( - :delete, - format("/v1/subscriptions/%s/discount", { subscription_exposed_id: CGI.escape(subscription_exposed_id) }), - params, - opts + request_stripe_object( + method: :delete, + path: format("/v1/subscriptions/%s/discount", { subscription_exposed_id: CGI.escape(subscription_exposed_id) }), + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end save_nested_resource :source diff --git a/lib/stripe/resources/subscription_item.rb b/lib/stripe/resources/subscription_item.rb index 9857dce48..16235e105 100644 --- a/lib/stripe/resources/subscription_item.rb +++ b/lib/stripe/resources/subscription_item.rb @@ -17,8 +17,12 @@ class SubscriptionItem < APIResource resource_plural: "usage_record_summaries" def usage_record_summaries(params = {}, opts = {}) - resp, opts = execute_resource_request(:get, resource_url + "/usage_record_summaries", params, opts) - Util.convert_to_stripe_object(resp.data, opts) + request_stripe_object( + method: :get, + path: resource_url + "/usage_record_summaries", + params: params, + opts: opts + ) end extend Gem::Deprecate deprecate :usage_record_summaries, :"SubscriptionItem.list_usage_record_summaries", 2020, 1 diff --git a/lib/stripe/resources/subscription_schedule.rb b/lib/stripe/resources/subscription_schedule.rb index 18afee171..d56c118e2 100644 --- a/lib/stripe/resources/subscription_schedule.rb +++ b/lib/stripe/resources/subscription_schedule.rb @@ -28,23 +28,21 @@ def release(params = {}, opts = {}) end def self.cancel(schedule, params = {}, opts = {}) - resp, opts = execute_resource_request( - :post, - format("/v1/subscription_schedules/%s/cancel", { schedule: CGI.escape(schedule) }), - params, - opts + request_stripe_object( + method: :post, + path: format("/v1/subscription_schedules/%s/cancel", { schedule: CGI.escape(schedule) }), + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end def self.release(schedule, params = {}, opts = {}) - resp, opts = execute_resource_request( - :post, - format("/v1/subscription_schedules/%s/release", { schedule: CGI.escape(schedule) }), - params, - opts + request_stripe_object( + method: :post, + path: format("/v1/subscription_schedules/%s/release", { schedule: CGI.escape(schedule) }), + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end end end diff --git a/lib/stripe/resources/terminal/reader.rb b/lib/stripe/resources/terminal/reader.rb index 3a9d9f2c6..52d088977 100644 --- a/lib/stripe/resources/terminal/reader.rb +++ b/lib/stripe/resources/terminal/reader.rb @@ -48,43 +48,39 @@ def set_reader_display(params = {}, opts = {}) end def self.cancel_action(reader, params = {}, opts = {}) - resp, opts = execute_resource_request( - :post, - format("/v1/terminal/readers/%s/cancel_action", { reader: CGI.escape(reader) }), - params, - opts + request_stripe_object( + method: :post, + path: format("/v1/terminal/readers/%s/cancel_action", { reader: CGI.escape(reader) }), + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end def self.process_payment_intent(reader, params = {}, opts = {}) - resp, opts = execute_resource_request( - :post, - format("/v1/terminal/readers/%s/process_payment_intent", { reader: CGI.escape(reader) }), - params, - opts + request_stripe_object( + method: :post, + path: format("/v1/terminal/readers/%s/process_payment_intent", { reader: CGI.escape(reader) }), + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end def self.process_setup_intent(reader, params = {}, opts = {}) - resp, opts = execute_resource_request( - :post, - format("/v1/terminal/readers/%s/process_setup_intent", { reader: CGI.escape(reader) }), - params, - opts + request_stripe_object( + method: :post, + path: format("/v1/terminal/readers/%s/process_setup_intent", { reader: CGI.escape(reader) }), + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end def self.set_reader_display(reader, params = {}, opts = {}) - resp, opts = execute_resource_request( - :post, - format("/v1/terminal/readers/%s/set_reader_display", { reader: CGI.escape(reader) }), - params, - opts + request_stripe_object( + method: :post, + path: format("/v1/terminal/readers/%s/set_reader_display", { reader: CGI.escape(reader) }), + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end def test_helpers @@ -95,13 +91,12 @@ class TestHelpers < APIResourceTestHelpers RESOURCE_CLASS = Reader def self.present_payment_method(reader, params = {}, opts = {}) - resp, opts = execute_resource_request( - :post, - format("/v1/test_helpers/terminal/readers/%s/present_payment_method", { reader: CGI.escape(reader) }), - params, - opts + request_stripe_object( + method: :post, + path: format("/v1/test_helpers/terminal/readers/%s/present_payment_method", { reader: CGI.escape(reader) }), + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end def present_payment_method(params = {}, opts = {}) diff --git a/lib/stripe/resources/test_helpers/test_clock.rb b/lib/stripe/resources/test_helpers/test_clock.rb index 83131139c..3f93e56ec 100644 --- a/lib/stripe/resources/test_helpers/test_clock.rb +++ b/lib/stripe/resources/test_helpers/test_clock.rb @@ -20,13 +20,12 @@ def advance(params = {}, opts = {}) end def self.advance(test_clock, params = {}, opts = {}) - resp, opts = execute_resource_request( - :post, - format("/v1/test_helpers/test_clocks/%s/advance", { test_clock: CGI.escape(test_clock) }), - params, - opts + request_stripe_object( + method: :post, + path: format("/v1/test_helpers/test_clocks/%s/advance", { test_clock: CGI.escape(test_clock) }), + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end end end diff --git a/lib/stripe/resources/topup.rb b/lib/stripe/resources/topup.rb index 128e0bc9d..22e6c9079 100644 --- a/lib/stripe/resources/topup.rb +++ b/lib/stripe/resources/topup.rb @@ -19,13 +19,12 @@ def cancel(params = {}, opts = {}) end def self.cancel(topup, params = {}, opts = {}) - resp, opts = execute_resource_request( - :post, - format("/v1/topups/%s/cancel", { topup: CGI.escape(topup) }), - params, - opts + request_stripe_object( + method: :post, + path: format("/v1/topups/%s/cancel", { topup: CGI.escape(topup) }), + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end end end diff --git a/lib/stripe/resources/transfer.rb b/lib/stripe/resources/transfer.rb index 816621229..df65a2672 100644 --- a/lib/stripe/resources/transfer.rb +++ b/lib/stripe/resources/transfer.rb @@ -23,13 +23,12 @@ def cancel(params = {}, opts = {}) end def self.cancel(id, params = {}, opts = {}) - resp, opts = execute_resource_request( - :post, - format("/v1/transfers/%s/cancel", { id: CGI.escape(id) }), - params, - opts + request_stripe_object( + method: :post, + path: format("/v1/transfers/%s/cancel", { id: CGI.escape(id) }), + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end end end diff --git a/lib/stripe/resources/treasury/financial_account.rb b/lib/stripe/resources/treasury/financial_account.rb index f10ca20ae..9a9d42773 100644 --- a/lib/stripe/resources/treasury/financial_account.rb +++ b/lib/stripe/resources/treasury/financial_account.rb @@ -29,23 +29,21 @@ def update_features(params = {}, opts = {}) end def self.retrieve_features(financial_account, params = {}, opts = {}) - resp, opts = execute_resource_request( - :get, - format("/v1/treasury/financial_accounts/%s/features", { financial_account: CGI.escape(financial_account) }), - params, - opts + request_stripe_object( + method: :get, + path: format("/v1/treasury/financial_accounts/%s/features", { financial_account: CGI.escape(financial_account) }), + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end def self.update_features(financial_account, params = {}, opts = {}) - resp, opts = execute_resource_request( - :post, - format("/v1/treasury/financial_accounts/%s/features", { financial_account: CGI.escape(financial_account) }), - params, - opts + request_stripe_object( + method: :post, + path: format("/v1/treasury/financial_accounts/%s/features", { financial_account: CGI.escape(financial_account) }), + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end end end diff --git a/lib/stripe/resources/treasury/inbound_transfer.rb b/lib/stripe/resources/treasury/inbound_transfer.rb index 2ac258ee9..57f23e027 100644 --- a/lib/stripe/resources/treasury/inbound_transfer.rb +++ b/lib/stripe/resources/treasury/inbound_transfer.rb @@ -19,13 +19,12 @@ def cancel(params = {}, opts = {}) end def self.cancel(inbound_transfer, params = {}, opts = {}) - resp, opts = execute_resource_request( - :post, - format("/v1/treasury/inbound_transfers/%s/cancel", { inbound_transfer: CGI.escape(inbound_transfer) }), - params, - opts + request_stripe_object( + method: :post, + path: format("/v1/treasury/inbound_transfers/%s/cancel", { inbound_transfer: CGI.escape(inbound_transfer) }), + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end def test_helpers @@ -36,33 +35,30 @@ class TestHelpers < APIResourceTestHelpers RESOURCE_CLASS = InboundTransfer def self.fail(id, params = {}, opts = {}) - resp, opts = execute_resource_request( - :post, - format("/v1/test_helpers/treasury/inbound_transfers/%s/fail", { id: CGI.escape(id) }), - params, - opts + request_stripe_object( + method: :post, + path: format("/v1/test_helpers/treasury/inbound_transfers/%s/fail", { id: CGI.escape(id) }), + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end def self.return_inbound_transfer(id, params = {}, opts = {}) - resp, opts = execute_resource_request( - :post, - format("/v1/test_helpers/treasury/inbound_transfers/%s/return", { id: CGI.escape(id) }), - params, - opts + request_stripe_object( + method: :post, + path: format("/v1/test_helpers/treasury/inbound_transfers/%s/return", { id: CGI.escape(id) }), + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end def self.succeed(id, params = {}, opts = {}) - resp, opts = execute_resource_request( - :post, - format("/v1/test_helpers/treasury/inbound_transfers/%s/succeed", { id: CGI.escape(id) }), - params, - opts + request_stripe_object( + method: :post, + path: format("/v1/test_helpers/treasury/inbound_transfers/%s/succeed", { id: CGI.escape(id) }), + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end def fail(params = {}, opts = {}) diff --git a/lib/stripe/resources/treasury/outbound_payment.rb b/lib/stripe/resources/treasury/outbound_payment.rb index 6d90574be..d87e666f1 100644 --- a/lib/stripe/resources/treasury/outbound_payment.rb +++ b/lib/stripe/resources/treasury/outbound_payment.rb @@ -19,13 +19,12 @@ def cancel(params = {}, opts = {}) end def self.cancel(id, params = {}, opts = {}) - resp, opts = execute_resource_request( - :post, - format("/v1/treasury/outbound_payments/%s/cancel", { id: CGI.escape(id) }), - params, - opts + request_stripe_object( + method: :post, + path: format("/v1/treasury/outbound_payments/%s/cancel", { id: CGI.escape(id) }), + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end def test_helpers @@ -36,33 +35,30 @@ class TestHelpers < APIResourceTestHelpers RESOURCE_CLASS = OutboundPayment def self.fail(id, params = {}, opts = {}) - resp, opts = execute_resource_request( - :post, - format("/v1/test_helpers/treasury/outbound_payments/%s/fail", { id: CGI.escape(id) }), - params, - opts + request_stripe_object( + method: :post, + path: format("/v1/test_helpers/treasury/outbound_payments/%s/fail", { id: CGI.escape(id) }), + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end def self.post(id, params = {}, opts = {}) - resp, opts = execute_resource_request( - :post, - format("/v1/test_helpers/treasury/outbound_payments/%s/post", { id: CGI.escape(id) }), - params, - opts + request_stripe_object( + method: :post, + path: format("/v1/test_helpers/treasury/outbound_payments/%s/post", { id: CGI.escape(id) }), + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end def self.return_outbound_payment(id, params = {}, opts = {}) - resp, opts = execute_resource_request( - :post, - format("/v1/test_helpers/treasury/outbound_payments/%s/return", { id: CGI.escape(id) }), - params, - opts + request_stripe_object( + method: :post, + path: format("/v1/test_helpers/treasury/outbound_payments/%s/return", { id: CGI.escape(id) }), + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end def fail(params = {}, opts = {}) diff --git a/lib/stripe/resources/treasury/outbound_transfer.rb b/lib/stripe/resources/treasury/outbound_transfer.rb index 6ad95bbf1..48e74f663 100644 --- a/lib/stripe/resources/treasury/outbound_transfer.rb +++ b/lib/stripe/resources/treasury/outbound_transfer.rb @@ -19,13 +19,12 @@ def cancel(params = {}, opts = {}) end def self.cancel(outbound_transfer, params = {}, opts = {}) - resp, opts = execute_resource_request( - :post, - format("/v1/treasury/outbound_transfers/%s/cancel", { outbound_transfer: CGI.escape(outbound_transfer) }), - params, - opts + request_stripe_object( + method: :post, + path: format("/v1/treasury/outbound_transfers/%s/cancel", { outbound_transfer: CGI.escape(outbound_transfer) }), + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end def test_helpers @@ -36,23 +35,21 @@ class TestHelpers < APIResourceTestHelpers RESOURCE_CLASS = OutboundTransfer def self.fail(outbound_transfer, params = {}, opts = {}) - resp, opts = execute_resource_request( - :post, - format("/v1/test_helpers/treasury/outbound_transfers/%s/fail", { outbound_transfer: CGI.escape(outbound_transfer) }), - params, - opts + request_stripe_object( + method: :post, + path: format("/v1/test_helpers/treasury/outbound_transfers/%s/fail", { outbound_transfer: CGI.escape(outbound_transfer) }), + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end def self.post(outbound_transfer, params = {}, opts = {}) - resp, opts = execute_resource_request( - :post, - format("/v1/test_helpers/treasury/outbound_transfers/%s/post", { outbound_transfer: CGI.escape(outbound_transfer) }), - params, - opts + request_stripe_object( + method: :post, + path: format("/v1/test_helpers/treasury/outbound_transfers/%s/post", { outbound_transfer: CGI.escape(outbound_transfer) }), + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end def self.return_outbound_transfer( @@ -60,13 +57,12 @@ def self.return_outbound_transfer( params = {}, opts = {} ) - resp, opts = execute_resource_request( - :post, - format("/v1/test_helpers/treasury/outbound_transfers/%s/return", { outbound_transfer: CGI.escape(outbound_transfer) }), - params, - opts + request_stripe_object( + method: :post, + path: format("/v1/test_helpers/treasury/outbound_transfers/%s/return", { outbound_transfer: CGI.escape(outbound_transfer) }), + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end def fail(params = {}, opts = {}) diff --git a/lib/stripe/resources/treasury/received_credit.rb b/lib/stripe/resources/treasury/received_credit.rb index 5dbe399c1..525ba434d 100644 --- a/lib/stripe/resources/treasury/received_credit.rb +++ b/lib/stripe/resources/treasury/received_credit.rb @@ -16,13 +16,12 @@ class TestHelpers < APIResourceTestHelpers RESOURCE_CLASS = ReceivedCredit def self.create(params = {}, opts = {}) - resp, opts = execute_resource_request( - :post, - "/v1/test_helpers/treasury/received_credits", - params, - opts + request_stripe_object( + method: :post, + path: "/v1/test_helpers/treasury/received_credits", + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end end end diff --git a/lib/stripe/resources/treasury/received_debit.rb b/lib/stripe/resources/treasury/received_debit.rb index c4a3bef34..3889c6f05 100644 --- a/lib/stripe/resources/treasury/received_debit.rb +++ b/lib/stripe/resources/treasury/received_debit.rb @@ -16,13 +16,12 @@ class TestHelpers < APIResourceTestHelpers RESOURCE_CLASS = ReceivedDebit def self.create(params = {}, opts = {}) - resp, opts = execute_resource_request( - :post, - "/v1/test_helpers/treasury/received_debits", - params, - opts + request_stripe_object( + method: :post, + path: "/v1/test_helpers/treasury/received_debits", + params: params, + opts: opts ) - Util.convert_to_stripe_object(resp.data, opts) end end end diff --git a/lib/stripe/util.rb b/lib/stripe/util.rb index 6243bb391..ece84a040 100644 --- a/lib/stripe/util.rb +++ b/lib/stripe/util.rb @@ -90,7 +90,7 @@ def self.custom_method(resource, target, name, http_verb, http_path) opts ) - Util.convert_to_stripe_object(resp.data, opts) + Util.convert_to_stripe_object_with_params(resp.data, params, opts) end end @@ -104,9 +104,27 @@ def self.custom_method(resource, target, name, http_verb, http_path) # ==== Attributes # # * +data+ - Hash of fields and values to be converted into a StripeObject. + # * +params+ - Params for +StripeObject+ like filters used in search that + # will be reused on subsequent API calls. # * +opts+ - Options for +StripeObject+ like an API key that will be reused # on subsequent API calls. def self.convert_to_stripe_object(data, opts = {}) + convert_to_stripe_object_with_params(data, {}, opts) + end + + # Converts a hash of fields or an array of hashes into a +StripeObject+ or + # array of +StripeObject+s. These new objects will be created as a concrete + # type as dictated by their `object` field (e.g. an `object` value of + # `charge` would create an instance of +Charge+), but if `object` is not + # present or of an unknown type, the newly created instance will fall back + # to being a +StripeObject+. + # + # ==== Attributes + # + # * +data+ - Hash of fields and values to be converted into a StripeObject. + # * +opts+ - Options for +StripeObject+ like an API key that will be reused + # on subsequent API calls. + def self.convert_to_stripe_object_with_params(data, params, opts = {}) opts = normalize_opts(opts) case data @@ -115,8 +133,16 @@ def self.convert_to_stripe_object(data, opts = {}) when Hash # Try converting to a known object class. If none available, fall back # to generic StripeObject - object_classes.fetch(data[:object], StripeObject) - .construct_from(data, opts) + obj = object_classes.fetch(data[:object], StripeObject) + .construct_from(data, opts) + + # set filters so that we can fetch the same limit, expansions, and + # predicates when accessing the next and previous pages + if obj && (obj.is_a?(SearchResultObject) || obj.is_a?(ListObject)) + obj.filters = params.dup + end + + obj else data end diff --git a/test/stripe/api_resource_test.rb b/test/stripe/api_resource_test.rb index f9ea1e448..706801eee 100644 --- a/test/stripe/api_resource_test.rb +++ b/test/stripe/api_resource_test.rb @@ -187,13 +187,15 @@ class NestedTestAPIResource < APIResource stub_request(:get, "#{Stripe.api_base}/v1/charges") .with(query: { customer: "cus_123" }) .to_return(body: JSON.generate(data: [charge_fixture], - url: "/v1/charges")) + url: "/v1/charges", + object: "list")) charges = Stripe::Charge.list(customer: "cus_123") stub_request(:get, "#{Stripe.api_base}/v1/charges") .with(query: { customer: "cus_123", created: "123" }) .to_return(body: JSON.generate(data: [charge_fixture], - url: "/v1/charges")) + url: "/v1/charges", + object: "list")) charges.list(created: 123) end diff --git a/test/stripe/list_object_test.rb b/test/stripe/list_object_test.rb index 09f842ba2..f65ee5ee1 100644 --- a/test/stripe/list_object_test.rb +++ b/test/stripe/list_object_test.rb @@ -51,7 +51,8 @@ class ListObjectTest < Test::Unit::TestCase # used as a cursor to fetch the next page. list = TestListObject.construct_from(data: [{ id: 1 }], has_more: true, - url: "/things") + url: "/things", + object: "list") list.filters = { limit: 3 } # The test will start with the synthetic list object above, and use it as @@ -60,10 +61,12 @@ class ListObjectTest < Test::Unit::TestCase # iteration stops. stub_request(:get, "#{Stripe.api_base}/things") .with(query: { starting_after: "1", limit: "3" }) - .to_return(body: JSON.generate(data: [{ id: 2 }, { id: 3 }, { id: 4 }], has_more: true, url: "/things")) + .to_return(body: JSON.generate(data: [{ id: 2 }, { id: 3 }, { id: 4 }], has_more: true, url: "/things", + object: "list")) stub_request(:get, "#{Stripe.api_base}/things") .with(query: { starting_after: "4", limit: "3" }) - .to_return(body: JSON.generate(data: [{ id: 5 }, { id: 6 }], has_more: false, url: "/things")) + .to_return(body: JSON.generate(data: [{ id: 5 }, { id: 6 }], has_more: false, url: "/things", + object: "list")) assert_equal expected, list.auto_paging_each.to_a end @@ -83,7 +86,8 @@ class ListObjectTest < Test::Unit::TestCase # used as a cursor to fetch the next page. list = TestListObject.construct_from(data: [{ id: 6 }], has_more: true, - url: "/things") + url: "/things", + object: "list") # We also add an `ending_before` filter on the list to simulate backwards # pagination. @@ -95,10 +99,12 @@ class ListObjectTest < Test::Unit::TestCase # iteration stops. stub_request(:get, "#{Stripe.api_base}/things") .with(query: { ending_before: "6", limit: "3" }) - .to_return(body: JSON.generate(data: [{ id: 3 }, { id: 4 }, { id: 5 }], has_more: true, url: "/things")) + .to_return(body: JSON.generate(data: [{ id: 3 }, { id: 4 }, { id: 5 }], has_more: true, url: "/things", + object: "list")) stub_request(:get, "#{Stripe.api_base}/things") .with(query: { ending_before: "3", limit: "3" }) - .to_return(body: JSON.generate(data: [{ id: 1 }, { id: 2 }], has_more: false, url: "/things")) + .to_return(body: JSON.generate(data: [{ id: 1 }, { id: 2 }], has_more: false, url: "/things", + object: "list")) assert_equal expected, list.auto_paging_each.to_a end @@ -113,10 +119,11 @@ class ListObjectTest < Test::Unit::TestCase list = TestListObject.construct_from(data: [{ id: 1 }], has_more: true, - url: "/things") + url: "/things", + object: "list") stub_request(:get, "#{Stripe.api_base}/things") .with(query: { starting_after: "1" }) - .to_return(body: JSON.generate(data: [{ id: 2 }, { id: 3 }], has_more: false)) + .to_return(body: JSON.generate(data: [{ id: 2 }, { id: 3 }], has_more: false, object: "list")) actual = [] list.auto_paging_each do |obj| @@ -140,10 +147,11 @@ class ListObjectTest < Test::Unit::TestCase should "fetch a next page through #next_page" do list = TestListObject.construct_from(data: [{ id: 1 }], has_more: true, - url: "/things") + url: "/things", + object: "list") stub_request(:get, "#{Stripe.api_base}/things") .with(query: { starting_after: "1" }) - .to_return(body: JSON.generate(data: [{ id: 2 }], has_more: false)) + .to_return(body: JSON.generate(data: [{ id: 2 }], has_more: false, object: "list")) next_list = list.next_page refute next_list.empty? end @@ -151,11 +159,12 @@ class ListObjectTest < Test::Unit::TestCase should "fetch a next page through #next_page and respect limit" do list = TestListObject.construct_from(data: [{ id: 1 }], has_more: true, - url: "/things") + url: "/things", + object: "list") list.filters = { expand: ["data.source"], limit: 3 } stub_request(:get, "#{Stripe.api_base}/things") .with(query: { "expand[]" => "data.source", "limit" => "3", "starting_after" => "1" }) - .to_return(body: JSON.generate(data: [{ id: 2 }], has_more: false)) + .to_return(body: JSON.generate(data: [{ id: 2 }], has_more: false, object: "list")) next_list = list.next_page assert_equal({ expand: ["data.source"], limit: 3, starting_after: 1 }, next_list.filters) end @@ -163,7 +172,8 @@ class ListObjectTest < Test::Unit::TestCase should "fetch an empty page through #next_page" do list = TestListObject.construct_from(data: [{ id: 1 }], has_more: false, - url: "/things") + url: "/things", + object: "list") next_list = list.next_page assert_equal Stripe::ListObject.empty_list, next_list end @@ -175,10 +185,11 @@ class ListObjectTest < Test::Unit::TestCase should "fetch a next page through #previous_page" do list = TestListObject.construct_from(data: [{ id: 2 }], has_more: true, - url: "/things") + url: "/things", + object: "list") stub_request(:get, "#{Stripe.api_base}/things") .with(query: { ending_before: "2" }) - .to_return(body: JSON.generate(data: [{ id: 1 }], has_more: false)) + .to_return(body: JSON.generate(data: [{ id: 1 }], has_more: false, object: "list")) next_list = list.previous_page refute next_list.empty? end @@ -186,11 +197,12 @@ class ListObjectTest < Test::Unit::TestCase should "fetch a next page through #previous_page and respect limit" do list = TestListObject.construct_from(data: [{ id: 2 }], has_more: true, - url: "/things") + url: "/things", + object: "list") list.filters = { expand: ["data.source"], limit: 3 } stub_request(:get, "#{Stripe.api_base}/things") .with(query: { "expand[]" => "data.source", "limit" => "3", "ending_before" => "2" }) - .to_return(body: JSON.generate(data: [{ id: 1 }], has_more: false)) + .to_return(body: JSON.generate(data: [{ id: 1 }], has_more: false, object: "list")) next_list = list.previous_page assert_equal({ ending_before: 2, expand: ["data.source"], limit: 3 }, next_list.filters) end diff --git a/test/stripe/search_result_object_test.rb b/test/stripe/search_result_object_test.rb index 706fcbd79..fda3a78e6 100644 --- a/test/stripe/search_result_object_test.rb +++ b/test/stripe/search_result_object_test.rb @@ -38,7 +38,8 @@ class SearchResultObjectTest < Test::Unit::TestCase list = TestSearchResultObject.construct_from({ data: [{ id: 1 }], has_more: true, next_page: "next_page_token_1", - url: "/things", }) + url: "/things", + object: "search_result", }) list.filters = { limit: 3 } # The test will start with the synthetic search result object above, and uses the @@ -47,10 +48,10 @@ class SearchResultObjectTest < Test::Unit::TestCase # iteration stops. stub_request(:get, "#{Stripe.api_base}/things") .with(query: { limit: 3, page: "next_page_token_1" }) - .to_return(body: JSON.generate(data: [{ id: 2 }, { id: 3 }, { id: 4 }], has_more: true, url: "/things", next_page: "next_page_token_2")) + .to_return(body: JSON.generate(data: [{ id: 2 }, { id: 3 }, { id: 4 }], has_more: true, url: "/things", next_page: "next_page_token_2", object: "search_result")) stub_request(:get, "#{Stripe.api_base}/things") .with(query: { limit: 3, page: "next_page_token_2" }) - .to_return(body: JSON.generate(data: [{ id: 5 }, { id: 6 }], has_more: false, url: "/things", next_page: nil)) + .to_return(body: JSON.generate(data: [{ id: 5 }, { id: 6 }], has_more: false, url: "/things", next_page: nil, object: "search_result")) assert_equal arr, list.auto_paging_each.to_a.map(&:to_hash) end @@ -70,7 +71,7 @@ class SearchResultObjectTest < Test::Unit::TestCase stub_request(:get, "#{Stripe.api_base}/things") .with(query: { page: "next_page_token_1" }) - .to_return(body: JSON.generate(data: [{ id: 2 }, { id: 3 }], has_more: false)) + .to_return(body: JSON.generate(data: [{ id: 2 }, { id: 3 }], has_more: false, object: "search_result")) actual = [] list.auto_paging_each do |obj| @@ -98,7 +99,7 @@ class SearchResultObjectTest < Test::Unit::TestCase url: "/things") stub_request(:get, "#{Stripe.api_base}/things") .with(query: { page: "next_page_token_1" }) - .to_return(body: JSON.generate(data: [{ id: 2 }], has_more: false)) + .to_return(body: JSON.generate(data: [{ id: 2 }], has_more: false, object: "search_result")) next_list = list.next_search_result_page refute next_list.empty? assert_equal [{ id: 2 }], next_list.auto_paging_each.to_a.map(&:to_hash) @@ -108,11 +109,12 @@ class SearchResultObjectTest < Test::Unit::TestCase list = TestSearchResultObject.construct_from(data: [{ id: 1 }], has_more: true, next_page: "next_page_token_1", - url: "/things") + url: "/things", + object: "search_result") list.filters = { limit: 3 } stub_request(:get, "#{Stripe.api_base}/things") .with(query: { "limit": 3, page: "next_page_token_1" }) - .to_return(body: JSON.generate(data: [{ id: 2 }], has_more: false)) + .to_return(body: JSON.generate(data: [{ id: 2 }], has_more: false, object: "search_result")) next_list = list.next_search_result_page assert_equal({ limit: 3, page: "next_page_token_1" }, next_list.filters) end