From bc7d9e4628099c8e5e0478617c9cf989e07a5217 Mon Sep 17 00:00:00 2001 From: Pavel Krymets Date: Thu, 9 Jun 2022 10:32:11 -0700 Subject: [PATCH 1/7] Use request_stripe_object for all requests --- lib/stripe/api_operations/create.rb | 8 +- lib/stripe/api_operations/delete.rb | 19 +++-- lib/stripe/api_operations/list.rb | 15 ++-- lib/stripe/api_operations/nested_resource.rb | 10 +-- lib/stripe/api_operations/request.rb | 10 +++ lib/stripe/api_operations/save.rb | 9 ++- lib/stripe/api_operations/search.rb | 15 ++-- lib/stripe/api_resource.rb | 2 +- lib/stripe/list_object.rb | 2 +- lib/stripe/oauth.rb | 4 +- lib/stripe/resources/account.rb | 25 ++++-- lib/stripe/resources/apps/secret.rb | 22 +++--- lib/stripe/resources/charge.rb | 13 +++- lib/stripe/resources/checkout/session.rb | 13 +++- lib/stripe/resources/credit_note.rb | 29 +++++-- lib/stripe/resources/customer.rb | 65 ++++++++++------ lib/stripe/resources/dispute.rb | 13 +++- .../financial_connections/account.rb | 37 +++++++-- .../identity/verification_session.rb | 25 ++++-- lib/stripe/resources/invoice.rb | 77 +++++++++++++++---- lib/stripe/resources/issuing/authorization.rb | 25 ++++-- lib/stripe/resources/issuing/card.rb | 13 +++- lib/stripe/resources/issuing/dispute.rb | 13 +++- lib/stripe/resources/order.rb | 49 +++++++++--- lib/stripe/resources/payment_intent.rb | 73 ++++++++++++++---- lib/stripe/resources/payment_link.rb | 13 +++- lib/stripe/resources/payment_method.rb | 25 ++++-- lib/stripe/resources/payout.rb | 25 ++++-- lib/stripe/resources/quote.rb | 61 ++++++++++++--- lib/stripe/resources/refund.rb | 24 ++++-- lib/stripe/resources/review.rb | 13 +++- lib/stripe/resources/setup_intent.rb | 37 +++++++-- lib/stripe/resources/source.rb | 22 ++++-- lib/stripe/resources/subscription.rb | 13 +++- lib/stripe/resources/subscription_item.rb | 8 +- lib/stripe/resources/subscription_schedule.rb | 25 ++++-- lib/stripe/resources/terminal/reader.rb | 60 ++++++++++++--- .../resources/test_helpers/test_clock.rb | 13 +++- lib/stripe/resources/topup.rb | 13 +++- lib/stripe/resources/transfer.rb | 13 +++- .../resources/treasury/financial_account.rb | 25 ++++-- .../resources/treasury/inbound_transfer.rb | 48 +++++++++--- .../resources/treasury/outbound_payment.rb | 48 +++++++++--- .../resources/treasury/outbound_transfer.rb | 52 ++++++++++--- .../resources/treasury/received_credit.rb | 11 ++- .../resources/treasury/received_debit.rb | 11 ++- lib/stripe/stripe_object.rb | 6 +- lib/stripe/util.rb | 20 +++-- test/stripe/account_test.rb | 22 +++--- test/stripe/api_resource_test.rb | 6 +- test/stripe/file_test.rb | 4 +- test/stripe/list_object_test.rb | 56 ++++++++------ test/stripe/search_result_object_test.rb | 20 ++--- test/stripe/util_test.rb | 10 +-- 54 files changed, 955 insertions(+), 335 deletions(-) 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..e31313015 100644 --- a/lib/stripe/api_operations/nested_resource.rb +++ b/lib/stripe/api_operations/nested_resource.rb @@ -32,21 +32,21 @@ def nested_resource_class_methods(resource, path: nil, operations: nil, 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) + Util.convert_to_stripe_object(resp.data, {}, 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) + Util.convert_to_stripe_object(resp.data, {}, 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) + Util.convert_to_stripe_object(resp.data, {}, opts) end when :delete define_singleton_method(:"delete_#{resource}") \ @@ -54,14 +54,14 @@ def nested_resource_class_methods(resource, path: nil, operations: nil, 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) + Util.convert_to_stripe_object(resp.data, {}, 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) + Util.convert_to_stripe_object(resp.data, {}, opts) end else raise ArgumentError, "Unknown operation: #{operation.inspect}" diff --git a/lib/stripe/api_operations/request.rb b/lib/stripe/api_operations/request.rb index f4dcf3d42..7960f2b58 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(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(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..69416515d 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(resp.data, params, opts) end end diff --git a/lib/stripe/list_object.rb b/lib/stripe/list_object.rb index 3813a3603..d5d74f055 100644 --- a/lib/stripe/list_object.rb +++ b/lib/stripe/list_object.rb @@ -94,7 +94,7 @@ def retrieve(id, opts = {}) id, retrieve_params = Util.normalize_id(id) url = "#{resource_url}/#{CGI.escape(id)}" resp, opts = execute_resource_request(:get, url, retrieve_params, opts) - Util.convert_to_stripe_object(resp.data, opts) + Util.convert_to_stripe_object(resp.data, {}, opts) end # Fetches the next page in the resource list (if there is one). diff --git a/lib/stripe/oauth.rb b/lib/stripe/oauth.rb index 580b424c8..b89117755 100644 --- a/lib/stripe/oauth.rb +++ b/lib/stripe/oauth.rb @@ -49,7 +49,7 @@ def self.token(params = {}, opts = {}) :post, "/oauth/token", params, opts ) # This is just going to return a generic StripeObject, but that's okay - Util.convert_to_stripe_object(resp.data, opts) + Util.convert_to_stripe_object(resp.data, {}, opts) end def self.deauthorize(params = {}, opts = {}) @@ -59,7 +59,7 @@ def self.deauthorize(params = {}, opts = {}) :post, "/oauth/deauthorize", params, opts ) # This is just going to return a generic StripeObject, but that's okay - Util.convert_to_stripe_object(resp.data, opts) + Util.convert_to_stripe_object(resp.data, {}, opts) end end end diff --git a/lib/stripe/resources/account.rb b/lib/stripe/resources/account.rb index f94af1545..2f05ab265 100644 --- a/lib/stripe/resources/account.rb +++ b/lib/stripe/resources/account.rb @@ -12,9 +12,6 @@ class Account < APIResource OBJECT_NAME = "account" - custom_method :persons, http_verb: :get - custom_method :reject, http_verb: :post - nested_resource_class_methods :capability, operations: %i[retrieve update list], resource_plural: "capabilities" @@ -24,7 +21,7 @@ class Account < APIResource def persons(params = {}, opts = {}) request_stripe_object( method: :get, - path: resource_url + "/persons", + path: format("/v1/accounts/%s/persons", { account: CGI.escape(self["id"]) }), params: params, opts: opts ) @@ -33,7 +30,25 @@ def persons(params = {}, opts = {}) def reject(params = {}, opts = {}) request_stripe_object( method: :post, - path: resource_url + "/reject", + path: format("/v1/accounts/%s/reject", { account: CGI.escape(self["id"]) }), + params: params, + opts: opts + ) + end + + def self.persons(account, params = {}, opts = {}) + request_stripe_object( + method: :get, + path: format("/v1/accounts/%s/persons", { account: CGI.escape(account) }), + params: params, + opts: opts + ) + end + + def self.reject(account, params = {}, opts = {}) + request_stripe_object( + method: :post, + path: format("/v1/accounts/%s/reject", { account: CGI.escape(account) }), params: params, opts: opts ) diff --git a/lib/stripe/resources/apps/secret.rb b/lib/stripe/resources/apps/secret.rb index 3c28afbd3..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, - resource_url + "/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, - resource_url + "/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 bd0091679..b7603443a 100644 --- a/lib/stripe/resources/charge.rb +++ b/lib/stripe/resources/charge.rb @@ -10,12 +10,19 @@ class Charge < APIResource OBJECT_NAME = "charge" - custom_method :capture, http_verb: :post - def capture(params = {}, opts = {}) request_stripe_object( method: :post, - path: resource_url + "/capture", + path: format("/v1/charges/%s/capture", { charge: CGI.escape(self["id"]) }), + params: params, + opts: opts + ) + end + + def self.capture(charge, params = {}, opts = {}) + request_stripe_object( + method: :post, + path: format("/v1/charges/%s/capture", { charge: CGI.escape(charge) }), params: params, opts: opts ) diff --git a/lib/stripe/resources/checkout/session.rb b/lib/stripe/resources/checkout/session.rb index 4c60bcc0f..c499c88b2 100644 --- a/lib/stripe/resources/checkout/session.rb +++ b/lib/stripe/resources/checkout/session.rb @@ -10,14 +10,21 @@ class Session < APIResource OBJECT_NAME = "checkout.session" - custom_method :expire, http_verb: :post - nested_resource_class_methods :line_item, operations: %i[list] def expire(params = {}, opts = {}) request_stripe_object( method: :post, - path: resource_url + "/expire", + path: format("/v1/checkout/sessions/%s/expire", { session: CGI.escape(self["id"]) }), + params: params, + opts: opts + ) + end + + def self.expire(session, params = {}, opts = {}) + request_stripe_object( + method: :post, + path: format("/v1/checkout/sessions/%s/expire", { session: CGI.escape(session) }), params: params, opts: opts ) diff --git a/lib/stripe/resources/credit_note.rb b/lib/stripe/resources/credit_note.rb index 5ea64a8a4..9779b6dae 100644 --- a/lib/stripe/resources/credit_note.rb +++ b/lib/stripe/resources/credit_note.rb @@ -9,25 +9,40 @@ class CreditNote < APIResource OBJECT_NAME = "credit_note" - custom_method :void_credit_note, http_verb: :post, http_path: "void" - def void_credit_note(params = {}, opts = {}) request_stripe_object( method: :post, - path: resource_url + "/void", + path: format("/v1/credit_notes/%s/void", { id: CGI.escape(self["id"]) }), + params: params, + opts: opts + ) + end + + def self.void_credit_note(id, params = {}, opts = {}) + request_stripe_object( + method: :post, + path: format("/v1/credit_notes/%s/void", { id: CGI.escape(id) }), params: params, opts: 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 e57c85be3..7690f19c1 100644 --- a/lib/stripe/resources/customer.rb +++ b/lib/stripe/resources/customer.rb @@ -12,9 +12,6 @@ class Customer < APIResource OBJECT_NAME = "customer" - custom_method :create_funding_instructions, http_verb: :post, http_path: "funding_instructions" - custom_method :list_payment_methods, http_verb: :get, http_path: "payment_methods" - nested_resource_class_methods :balance_transaction, operations: %i[create retrieve update list] nested_resource_class_methods :tax_id, @@ -23,7 +20,7 @@ class Customer < APIResource def create_funding_instructions(params = {}, opts = {}) request_stripe_object( method: :post, - path: resource_url + "/funding_instructions", + path: format("/v1/customers/%s/funding_instructions", { customer: CGI.escape(self["id"]) }), params: params, opts: opts ) @@ -32,7 +29,7 @@ def create_funding_instructions(params = {}, opts = {}) def list_payment_methods(params = {}, opts = {}) request_stripe_object( method: :get, - path: resource_url + "/payment_methods", + path: format("/v1/customers/%s/payment_methods", { customer: CGI.escape(self["id"]) }), params: params, opts: opts ) @@ -47,19 +44,36 @@ def retrieve_payment_method(payment_method, params = {}, opts = {}) ) end + def self.create_funding_instructions(customer, params = {}, opts = {}) + request_stripe_object( + method: :post, + path: format("/v1/customers/%s/funding_instructions", { customer: CGI.escape(customer) }), + params: params, + opts: opts + ) + end + + def self.list_payment_methods(customer, params = {}, opts = {}) + request_stripe_object( + method: :get, + path: format("/v1/customers/%s/payment_methods", { customer: CGI.escape(customer) }), + params: params, + opts: opts + ) + end + def self.retrieve_payment_method( customer, 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" @@ -80,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 = {}) @@ -103,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( @@ -123,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 952b82707..9689b1e15 100644 --- a/lib/stripe/resources/dispute.rb +++ b/lib/stripe/resources/dispute.rb @@ -8,12 +8,19 @@ class Dispute < APIResource OBJECT_NAME = "dispute" - custom_method :close, http_verb: :post - def close(params = {}, opts = {}) request_stripe_object( method: :post, - path: resource_url + "/close", + path: format("/v1/disputes/%s/close", { dispute: CGI.escape(self["id"]) }), + params: params, + opts: opts + ) + end + + def self.close(dispute, params = {}, opts = {}) + request_stripe_object( + method: :post, + path: format("/v1/disputes/%s/close", { dispute: CGI.escape(dispute) }), params: params, opts: opts ) diff --git a/lib/stripe/resources/financial_connections/account.rb b/lib/stripe/resources/financial_connections/account.rb index 96c179647..6d7f466c3 100644 --- a/lib/stripe/resources/financial_connections/account.rb +++ b/lib/stripe/resources/financial_connections/account.rb @@ -8,14 +8,10 @@ class Account < APIResource OBJECT_NAME = "financial_connections.account" - custom_method :disconnect, http_verb: :post - custom_method :list_owners, http_verb: :get, http_path: "owners" - custom_method :refresh_account, http_verb: :post, http_path: "refresh" - def disconnect(params = {}, opts = {}) request_stripe_object( method: :post, - path: resource_url + "/disconnect", + path: format("/v1/financial_connections/accounts/%s/disconnect", { account: CGI.escape(self["id"]) }), params: params, opts: opts ) @@ -24,7 +20,7 @@ def disconnect(params = {}, opts = {}) def list_owners(params = {}, opts = {}) request_stripe_object( method: :get, - path: resource_url + "/owners", + path: format("/v1/financial_connections/accounts/%s/owners", { account: CGI.escape(self["id"]) }), params: params, opts: opts ) @@ -33,7 +29,34 @@ def list_owners(params = {}, opts = {}) def refresh_account(params = {}, opts = {}) request_stripe_object( method: :post, - path: resource_url + "/refresh", + path: format("/v1/financial_connections/accounts/%s/refresh", { account: CGI.escape(self["id"]) }), + params: params, + opts: opts + ) + end + + def self.disconnect(account, params = {}, opts = {}) + request_stripe_object( + method: :post, + path: format("/v1/financial_connections/accounts/%s/disconnect", { account: CGI.escape(account) }), + params: params, + opts: opts + ) + end + + def self.list_owners(account, params = {}, opts = {}) + request_stripe_object( + method: :get, + path: format("/v1/financial_connections/accounts/%s/owners", { account: CGI.escape(account) }), + params: params, + opts: opts + ) + end + + def self.refresh_account(account, params = {}, opts = {}) + request_stripe_object( + method: :post, + path: format("/v1/financial_connections/accounts/%s/refresh", { account: CGI.escape(account) }), params: params, opts: opts ) diff --git a/lib/stripe/resources/identity/verification_session.rb b/lib/stripe/resources/identity/verification_session.rb index 2170d8985..6af101979 100644 --- a/lib/stripe/resources/identity/verification_session.rb +++ b/lib/stripe/resources/identity/verification_session.rb @@ -10,13 +10,10 @@ class VerificationSession < APIResource OBJECT_NAME = "identity.verification_session" - custom_method :cancel, http_verb: :post - custom_method :redact, http_verb: :post - def cancel(params = {}, opts = {}) request_stripe_object( method: :post, - path: resource_url + "/cancel", + path: format("/v1/identity/verification_sessions/%s/cancel", { session: CGI.escape(self["id"]) }), params: params, opts: opts ) @@ -25,7 +22,25 @@ def cancel(params = {}, opts = {}) def redact(params = {}, opts = {}) request_stripe_object( method: :post, - path: resource_url + "/redact", + path: format("/v1/identity/verification_sessions/%s/redact", { session: CGI.escape(self["id"]) }), + params: params, + opts: opts + ) + end + + def self.cancel(session, params = {}, opts = {}) + request_stripe_object( + method: :post, + path: format("/v1/identity/verification_sessions/%s/cancel", { session: CGI.escape(session) }), + params: params, + opts: opts + ) + end + + def self.redact(session, params = {}, opts = {}) + request_stripe_object( + method: :post, + path: format("/v1/identity/verification_sessions/%s/redact", { session: CGI.escape(session) }), params: params, opts: opts ) diff --git a/lib/stripe/resources/invoice.rb b/lib/stripe/resources/invoice.rb index 14c8695df..e6bfd88e6 100644 --- a/lib/stripe/resources/invoice.rb +++ b/lib/stripe/resources/invoice.rb @@ -11,16 +11,10 @@ class Invoice < APIResource OBJECT_NAME = "invoice" - custom_method :finalize_invoice, http_verb: :post, http_path: "finalize" - custom_method :mark_uncollectible, http_verb: :post - custom_method :pay, http_verb: :post - custom_method :send_invoice, http_verb: :post, http_path: "send" - custom_method :void_invoice, http_verb: :post, http_path: "void" - def finalize_invoice(params = {}, opts = {}) request_stripe_object( method: :post, - path: resource_url + "/finalize", + path: format("/v1/invoices/%s/finalize", { invoice: CGI.escape(self["id"]) }), params: params, opts: opts ) @@ -29,7 +23,7 @@ def finalize_invoice(params = {}, opts = {}) def mark_uncollectible(params = {}, opts = {}) request_stripe_object( method: :post, - path: resource_url + "/mark_uncollectible", + path: format("/v1/invoices/%s/mark_uncollectible", { invoice: CGI.escape(self["id"]) }), params: params, opts: opts ) @@ -38,7 +32,7 @@ def mark_uncollectible(params = {}, opts = {}) def pay(params = {}, opts = {}) request_stripe_object( method: :post, - path: resource_url + "/pay", + path: format("/v1/invoices/%s/pay", { invoice: CGI.escape(self["id"]) }), params: params, opts: opts ) @@ -47,7 +41,7 @@ def pay(params = {}, opts = {}) def send_invoice(params = {}, opts = {}) request_stripe_object( method: :post, - path: resource_url + "/send", + path: format("/v1/invoices/%s/send", { invoice: CGI.escape(self["id"]) }), params: params, opts: opts ) @@ -56,20 +50,73 @@ def send_invoice(params = {}, opts = {}) def void_invoice(params = {}, opts = {}) request_stripe_object( method: :post, - path: resource_url + "/void", + path: format("/v1/invoices/%s/void", { invoice: CGI.escape(self["id"]) }), + params: params, + opts: opts + ) + end + + def self.finalize_invoice(invoice, params = {}, opts = {}) + request_stripe_object( + method: :post, + path: format("/v1/invoices/%s/finalize", { invoice: CGI.escape(invoice) }), + params: params, + opts: opts + ) + end + + def self.mark_uncollectible(invoice, params = {}, opts = {}) + request_stripe_object( + method: :post, + path: format("/v1/invoices/%s/mark_uncollectible", { invoice: CGI.escape(invoice) }), + params: params, + opts: opts + ) + end + + def self.pay(invoice, params = {}, opts = {}) + request_stripe_object( + method: :post, + path: format("/v1/invoices/%s/pay", { invoice: CGI.escape(invoice) }), + params: params, + opts: opts + ) + end + + def self.send_invoice(invoice, params = {}, opts = {}) + request_stripe_object( + method: :post, + path: format("/v1/invoices/%s/send", { invoice: CGI.escape(invoice) }), + params: params, + opts: opts + ) + end + + def self.void_invoice(invoice, params = {}, opts = {}) + request_stripe_object( + method: :post, + path: format("/v1/invoices/%s/void", { invoice: CGI.escape(invoice) }), params: params, opts: 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 8f8bf566e..a90dc6d85 100644 --- a/lib/stripe/resources/issuing/authorization.rb +++ b/lib/stripe/resources/issuing/authorization.rb @@ -9,13 +9,10 @@ class Authorization < APIResource OBJECT_NAME = "issuing.authorization" - custom_method :approve, http_verb: :post - custom_method :decline, http_verb: :post - def approve(params = {}, opts = {}) request_stripe_object( method: :post, - path: resource_url + "/approve", + path: format("/v1/issuing/authorizations/%s/approve", { authorization: CGI.escape(self["id"]) }), params: params, opts: opts ) @@ -24,7 +21,25 @@ def approve(params = {}, opts = {}) def decline(params = {}, opts = {}) request_stripe_object( method: :post, - path: resource_url + "/decline", + path: format("/v1/issuing/authorizations/%s/decline", { authorization: CGI.escape(self["id"]) }), + params: params, + opts: opts + ) + end + + def self.approve(authorization, params = {}, opts = {}) + request_stripe_object( + method: :post, + path: format("/v1/issuing/authorizations/%s/approve", { authorization: CGI.escape(authorization) }), + params: params, + opts: opts + ) + end + + def self.decline(authorization, params = {}, opts = {}) + request_stripe_object( + method: :post, + path: format("/v1/issuing/authorizations/%s/decline", { authorization: CGI.escape(authorization) }), params: params, opts: opts ) diff --git a/lib/stripe/resources/issuing/card.rb b/lib/stripe/resources/issuing/card.rb index 6dbffeed1..49f5b3acb 100644 --- a/lib/stripe/resources/issuing/card.rb +++ b/lib/stripe/resources/issuing/card.rb @@ -10,12 +10,19 @@ class Card < APIResource OBJECT_NAME = "issuing.card" - custom_method :details, http_verb: :get - def details(params = {}, opts = {}) request_stripe_object( method: :get, - path: resource_url + "/details", + path: format("/v1/issuing/cards/%s/details", { card: CGI.escape(self["id"]) }), + params: params, + opts: opts + ) + end + + def self.details(card, params = {}, opts = {}) + request_stripe_object( + method: :get, + path: format("/v1/issuing/cards/%s/details", { card: CGI.escape(card) }), params: params, opts: opts ) diff --git a/lib/stripe/resources/issuing/dispute.rb b/lib/stripe/resources/issuing/dispute.rb index 4989ef5e9..a32a44e32 100644 --- a/lib/stripe/resources/issuing/dispute.rb +++ b/lib/stripe/resources/issuing/dispute.rb @@ -10,12 +10,19 @@ class Dispute < APIResource OBJECT_NAME = "issuing.dispute" - custom_method :submit, http_verb: :post - def submit(params = {}, opts = {}) request_stripe_object( method: :post, - path: resource_url + "/submit", + path: format("/v1/issuing/disputes/%s/submit", { dispute: CGI.escape(self["id"]) }), + params: params, + opts: opts + ) + end + + def self.submit(dispute, params = {}, opts = {}) + request_stripe_object( + method: :post, + path: format("/v1/issuing/disputes/%s/submit", { dispute: CGI.escape(dispute) }), params: params, opts: opts ) diff --git a/lib/stripe/resources/order.rb b/lib/stripe/resources/order.rb index f8b9345e5..4549c9d30 100644 --- a/lib/stripe/resources/order.rb +++ b/lib/stripe/resources/order.rb @@ -9,15 +9,10 @@ class Order < APIResource OBJECT_NAME = "order" - custom_method :cancel, http_verb: :post - custom_method :list_line_items, http_verb: :get, http_path: "line_items" - custom_method :reopen, http_verb: :post - custom_method :submit, http_verb: :post - def cancel(params = {}, opts = {}) request_stripe_object( method: :post, - path: resource_url + "/cancel", + path: format("/v1/orders/%s/cancel", { id: CGI.escape(self["id"]) }), params: params, opts: opts ) @@ -26,7 +21,7 @@ def cancel(params = {}, opts = {}) def list_line_items(params = {}, opts = {}) request_stripe_object( method: :get, - path: resource_url + "/line_items", + path: format("/v1/orders/%s/line_items", { id: CGI.escape(self["id"]) }), params: params, opts: opts ) @@ -35,7 +30,7 @@ def list_line_items(params = {}, opts = {}) def reopen(params = {}, opts = {}) request_stripe_object( method: :post, - path: resource_url + "/reopen", + path: format("/v1/orders/%s/reopen", { id: CGI.escape(self["id"]) }), params: params, opts: opts ) @@ -44,7 +39,43 @@ def reopen(params = {}, opts = {}) def submit(params = {}, opts = {}) request_stripe_object( method: :post, - path: resource_url + "/submit", + path: format("/v1/orders/%s/submit", { id: CGI.escape(self["id"]) }), + params: params, + opts: opts + ) + end + + def self.cancel(id, params = {}, opts = {}) + request_stripe_object( + method: :post, + path: format("/v1/orders/%s/cancel", { id: CGI.escape(id) }), + params: params, + opts: opts + ) + end + + def self.list_line_items(id, params = {}, opts = {}) + request_stripe_object( + method: :get, + path: format("/v1/orders/%s/line_items", { id: CGI.escape(id) }), + params: params, + opts: opts + ) + end + + def self.reopen(id, params = {}, opts = {}) + request_stripe_object( + method: :post, + path: format("/v1/orders/%s/reopen", { id: CGI.escape(id) }), + params: params, + opts: opts + ) + end + + def self.submit(id, params = {}, opts = {}) + request_stripe_object( + method: :post, + path: format("/v1/orders/%s/submit", { id: CGI.escape(id) }), params: params, opts: opts ) diff --git a/lib/stripe/resources/payment_intent.rb b/lib/stripe/resources/payment_intent.rb index c9ef121dc..05ca3e9e0 100644 --- a/lib/stripe/resources/payment_intent.rb +++ b/lib/stripe/resources/payment_intent.rb @@ -10,17 +10,10 @@ class PaymentIntent < APIResource OBJECT_NAME = "payment_intent" - custom_method :apply_customer_balance, http_verb: :post - custom_method :cancel, http_verb: :post - custom_method :capture, http_verb: :post - custom_method :confirm, http_verb: :post - custom_method :increment_authorization, http_verb: :post - custom_method :verify_microdeposits, http_verb: :post - def apply_customer_balance(params = {}, opts = {}) request_stripe_object( method: :post, - path: resource_url + "/apply_customer_balance", + path: format("/v1/payment_intents/%s/apply_customer_balance", { intent: CGI.escape(self["id"]) }), params: params, opts: opts ) @@ -29,7 +22,7 @@ def apply_customer_balance(params = {}, opts = {}) def cancel(params = {}, opts = {}) request_stripe_object( method: :post, - path: resource_url + "/cancel", + path: format("/v1/payment_intents/%s/cancel", { intent: CGI.escape(self["id"]) }), params: params, opts: opts ) @@ -38,7 +31,7 @@ def cancel(params = {}, opts = {}) def capture(params = {}, opts = {}) request_stripe_object( method: :post, - path: resource_url + "/capture", + path: format("/v1/payment_intents/%s/capture", { intent: CGI.escape(self["id"]) }), params: params, opts: opts ) @@ -47,7 +40,7 @@ def capture(params = {}, opts = {}) def confirm(params = {}, opts = {}) request_stripe_object( method: :post, - path: resource_url + "/confirm", + path: format("/v1/payment_intents/%s/confirm", { intent: CGI.escape(self["id"]) }), params: params, opts: opts ) @@ -56,7 +49,7 @@ def confirm(params = {}, opts = {}) def increment_authorization(params = {}, opts = {}) request_stripe_object( method: :post, - path: resource_url + "/increment_authorization", + path: format("/v1/payment_intents/%s/increment_authorization", { intent: CGI.escape(self["id"]) }), params: params, opts: opts ) @@ -65,7 +58,61 @@ def increment_authorization(params = {}, opts = {}) def verify_microdeposits(params = {}, opts = {}) request_stripe_object( method: :post, - path: resource_url + "/verify_microdeposits", + path: format("/v1/payment_intents/%s/verify_microdeposits", { intent: CGI.escape(self["id"]) }), + params: params, + opts: opts + ) + end + + def self.apply_customer_balance(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 + ) + end + + def self.cancel(intent, params = {}, opts = {}) + request_stripe_object( + method: :post, + path: format("/v1/payment_intents/%s/cancel", { intent: CGI.escape(intent) }), + params: params, + opts: opts + ) + end + + def self.capture(intent, params = {}, opts = {}) + request_stripe_object( + method: :post, + path: format("/v1/payment_intents/%s/capture", { intent: CGI.escape(intent) }), + params: params, + opts: opts + ) + end + + def self.confirm(intent, params = {}, opts = {}) + request_stripe_object( + method: :post, + path: format("/v1/payment_intents/%s/confirm", { intent: CGI.escape(intent) }), + params: params, + opts: opts + ) + end + + def self.increment_authorization(intent, params = {}, opts = {}) + request_stripe_object( + method: :post, + path: format("/v1/payment_intents/%s/increment_authorization", { intent: CGI.escape(intent) }), + params: params, + opts: opts + ) + end + + def self.verify_microdeposits(intent, params = {}, opts = {}) + request_stripe_object( + method: :post, + path: format("/v1/payment_intents/%s/verify_microdeposits", { intent: CGI.escape(intent) }), params: params, opts: opts ) diff --git a/lib/stripe/resources/payment_link.rb b/lib/stripe/resources/payment_link.rb index 6db8cbc19..03739f566 100644 --- a/lib/stripe/resources/payment_link.rb +++ b/lib/stripe/resources/payment_link.rb @@ -9,12 +9,19 @@ class PaymentLink < APIResource OBJECT_NAME = "payment_link" - custom_method :list_line_items, http_verb: :get, http_path: "line_items" - def list_line_items(params = {}, opts = {}) request_stripe_object( method: :get, - path: resource_url + "/line_items", + path: format("/v1/payment_links/%s/line_items", { payment_link: CGI.escape(self["id"]) }), + params: params, + opts: opts + ) + end + + def self.list_line_items(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 ) diff --git a/lib/stripe/resources/payment_method.rb b/lib/stripe/resources/payment_method.rb index 8c9ea4234..18966b427 100644 --- a/lib/stripe/resources/payment_method.rb +++ b/lib/stripe/resources/payment_method.rb @@ -9,13 +9,10 @@ class PaymentMethod < APIResource OBJECT_NAME = "payment_method" - custom_method :attach, http_verb: :post - custom_method :detach, http_verb: :post - def attach(params = {}, opts = {}) request_stripe_object( method: :post, - path: resource_url + "/attach", + path: format("/v1/payment_methods/%s/attach", { payment_method: CGI.escape(self["id"]) }), params: params, opts: opts ) @@ -24,7 +21,25 @@ def attach(params = {}, opts = {}) def detach(params = {}, opts = {}) request_stripe_object( method: :post, - path: resource_url + "/detach", + path: format("/v1/payment_methods/%s/detach", { payment_method: CGI.escape(self["id"]) }), + params: params, + opts: opts + ) + end + + def self.attach(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 + ) + end + + def self.detach(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 ) diff --git a/lib/stripe/resources/payout.rb b/lib/stripe/resources/payout.rb index 2a77bf1a6..ede6d244f 100644 --- a/lib/stripe/resources/payout.rb +++ b/lib/stripe/resources/payout.rb @@ -9,13 +9,10 @@ class Payout < APIResource OBJECT_NAME = "payout" - custom_method :cancel, http_verb: :post - custom_method :reverse, http_verb: :post - def cancel(params = {}, opts = {}) request_stripe_object( method: :post, - path: resource_url + "/cancel", + path: format("/v1/payouts/%s/cancel", { payout: CGI.escape(self["id"]) }), params: params, opts: opts ) @@ -24,7 +21,25 @@ def cancel(params = {}, opts = {}) def reverse(params = {}, opts = {}) request_stripe_object( method: :post, - path: resource_url + "/reverse", + path: format("/v1/payouts/%s/reverse", { payout: CGI.escape(self["id"]) }), + params: params, + opts: opts + ) + end + + def self.cancel(payout, params = {}, opts = {}) + request_stripe_object( + method: :post, + path: format("/v1/payouts/%s/cancel", { payout: CGI.escape(payout) }), + params: params, + opts: opts + ) + end + + def self.reverse(payout, params = {}, opts = {}) + request_stripe_object( + method: :post, + path: format("/v1/payouts/%s/reverse", { payout: CGI.escape(payout) }), params: params, opts: opts ) diff --git a/lib/stripe/resources/quote.rb b/lib/stripe/resources/quote.rb index 58d68c711..195e0c3ba 100644 --- a/lib/stripe/resources/quote.rb +++ b/lib/stripe/resources/quote.rb @@ -9,16 +9,10 @@ class Quote < APIResource OBJECT_NAME = "quote" - custom_method :accept, http_verb: :post - custom_method :cancel, http_verb: :post - custom_method :finalize_quote, http_verb: :post, http_path: "finalize" - custom_method :list_computed_upfront_line_items, http_verb: :get, http_path: "computed_upfront_line_items" - custom_method :list_line_items, http_verb: :get, http_path: "line_items" - def accept(params = {}, opts = {}) request_stripe_object( method: :post, - path: resource_url + "/accept", + path: format("/v1/quotes/%s/accept", { quote: CGI.escape(self["id"]) }), params: params, opts: opts ) @@ -27,7 +21,7 @@ def accept(params = {}, opts = {}) def cancel(params = {}, opts = {}) request_stripe_object( method: :post, - path: resource_url + "/cancel", + path: format("/v1/quotes/%s/cancel", { quote: CGI.escape(self["id"]) }), params: params, opts: opts ) @@ -36,7 +30,7 @@ def cancel(params = {}, opts = {}) def finalize_quote(params = {}, opts = {}) request_stripe_object( method: :post, - path: resource_url + "/finalize", + path: format("/v1/quotes/%s/finalize", { quote: CGI.escape(self["id"]) }), params: params, opts: opts ) @@ -45,7 +39,7 @@ def finalize_quote(params = {}, opts = {}) def list_computed_upfront_line_items(params = {}, opts = {}) request_stripe_object( method: :get, - path: resource_url + "/computed_upfront_line_items", + path: format("/v1/quotes/%s/computed_upfront_line_items", { quote: CGI.escape(self["id"]) }), params: params, opts: opts ) @@ -54,7 +48,52 @@ def list_computed_upfront_line_items(params = {}, opts = {}) def list_line_items(params = {}, opts = {}) request_stripe_object( method: :get, - path: resource_url + "/line_items", + path: format("/v1/quotes/%s/line_items", { quote: CGI.escape(self["id"]) }), + params: params, + opts: opts + ) + end + + def self.accept(quote, params = {}, opts = {}) + request_stripe_object( + method: :post, + path: format("/v1/quotes/%s/accept", { quote: CGI.escape(quote) }), + params: params, + opts: opts + ) + end + + def self.cancel(quote, params = {}, opts = {}) + request_stripe_object( + method: :post, + path: format("/v1/quotes/%s/cancel", { quote: CGI.escape(quote) }), + params: params, + opts: opts + ) + end + + def self.finalize_quote(quote, params = {}, opts = {}) + request_stripe_object( + method: :post, + path: format("/v1/quotes/%s/finalize", { quote: CGI.escape(quote) }), + params: params, + opts: opts + ) + end + + def self.list_computed_upfront_line_items(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 + ) + end + + def self.list_line_items(quote, params = {}, opts = {}) + request_stripe_object( + method: :get, + path: format("/v1/quotes/%s/line_items", { quote: CGI.escape(quote) }), params: params, opts: opts ) diff --git a/lib/stripe/resources/refund.rb b/lib/stripe/resources/refund.rb index 1b27c9396..c896d0ef6 100644 --- a/lib/stripe/resources/refund.rb +++ b/lib/stripe/resources/refund.rb @@ -9,12 +9,19 @@ class Refund < APIResource OBJECT_NAME = "refund" - custom_method :cancel, http_verb: :post - def cancel(params = {}, opts = {}) request_stripe_object( method: :post, - path: resource_url + "/cancel", + path: format("/v1/refunds/%s/cancel", { refund: CGI.escape(self["id"]) }), + params: params, + opts: opts + ) + end + + def self.cancel(refund, params = {}, opts = {}) + request_stripe_object( + method: :post, + path: format("/v1/refunds/%s/cancel", { refund: CGI.escape(refund) }), params: params, opts: opts ) @@ -27,12 +34,19 @@ def test_helpers class TestHelpers < APIResourceTestHelpers RESOURCE_CLASS = Refund - custom_method :expire, http_verb: :post + def self.expire(refund, params = {}, opts = {}) + request_stripe_object( + method: :post, + path: format("/v1/test_helpers/refunds/%s/expire", { refund: CGI.escape(refund) }), + params: params, + opts: opts + ) + end def expire(params = {}, opts = {}) @resource.request_stripe_object( method: :post, - path: resource_url + "/expire", + path: format("/v1/test_helpers/refunds/%s/expire", { refund: CGI.escape(@resource["id"]) }), params: params, opts: opts ) diff --git a/lib/stripe/resources/review.rb b/lib/stripe/resources/review.rb index fc3c82add..b961bc838 100644 --- a/lib/stripe/resources/review.rb +++ b/lib/stripe/resources/review.rb @@ -7,12 +7,19 @@ class Review < APIResource OBJECT_NAME = "review" - custom_method :approve, http_verb: :post - def approve(params = {}, opts = {}) request_stripe_object( method: :post, - path: resource_url + "/approve", + path: format("/v1/reviews/%s/approve", { review: CGI.escape(self["id"]) }), + params: params, + opts: opts + ) + end + + def self.approve(review, params = {}, opts = {}) + request_stripe_object( + method: :post, + path: format("/v1/reviews/%s/approve", { review: CGI.escape(review) }), params: params, opts: opts ) diff --git a/lib/stripe/resources/setup_intent.rb b/lib/stripe/resources/setup_intent.rb index 25c62c889..dff2d33b5 100644 --- a/lib/stripe/resources/setup_intent.rb +++ b/lib/stripe/resources/setup_intent.rb @@ -9,14 +9,10 @@ class SetupIntent < APIResource OBJECT_NAME = "setup_intent" - custom_method :cancel, http_verb: :post - custom_method :confirm, http_verb: :post - custom_method :verify_microdeposits, http_verb: :post - def cancel(params = {}, opts = {}) request_stripe_object( method: :post, - path: resource_url + "/cancel", + path: format("/v1/setup_intents/%s/cancel", { intent: CGI.escape(self["id"]) }), params: params, opts: opts ) @@ -25,7 +21,7 @@ def cancel(params = {}, opts = {}) def confirm(params = {}, opts = {}) request_stripe_object( method: :post, - path: resource_url + "/confirm", + path: format("/v1/setup_intents/%s/confirm", { intent: CGI.escape(self["id"]) }), params: params, opts: opts ) @@ -34,7 +30,34 @@ def confirm(params = {}, opts = {}) def verify_microdeposits(params = {}, opts = {}) request_stripe_object( method: :post, - path: resource_url + "/verify_microdeposits", + path: format("/v1/setup_intents/%s/verify_microdeposits", { intent: CGI.escape(self["id"]) }), + params: params, + opts: opts + ) + end + + def self.cancel(intent, params = {}, opts = {}) + request_stripe_object( + method: :post, + path: format("/v1/setup_intents/%s/cancel", { intent: CGI.escape(intent) }), + params: params, + opts: opts + ) + end + + def self.confirm(intent, params = {}, opts = {}) + request_stripe_object( + method: :post, + path: format("/v1/setup_intents/%s/confirm", { intent: CGI.escape(intent) }), + params: params, + opts: opts + ) + end + + def self.verify_microdeposits(intent, params = {}, opts = {}) + request_stripe_object( + method: :post, + path: format("/v1/setup_intents/%s/verify_microdeposits", { intent: CGI.escape(intent) }), params: params, opts: opts ) diff --git a/lib/stripe/resources/source.rb b/lib/stripe/resources/source.rb index 596c69998..feaf54c4a 100644 --- a/lib/stripe/resources/source.rb +++ b/lib/stripe/resources/source.rb @@ -9,15 +9,22 @@ class Source < APIResource OBJECT_NAME = "source" - custom_method :verify, http_verb: :post - nested_resource_class_methods :source_transaction, operations: %i[retrieve list] def verify(params = {}, opts = {}) request_stripe_object( method: :post, - path: resource_url + "/verify", + path: format("/v1/sources/%s/verify", { source: CGI.escape(self["id"]) }), + params: params, + opts: opts + ) + end + + def self.verify(source, params = {}, opts = {}) + request_stripe_object( + method: :post, + path: format("/v1/sources/%s/verify", { source: CGI.escape(source) }), params: params, opts: opts ) @@ -37,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 cd67a43bc..986f5c8de 100644 --- a/lib/stripe/resources/subscription.rb +++ b/lib/stripe/resources/subscription.rb @@ -11,12 +11,19 @@ class Subscription < APIResource OBJECT_NAME = "subscription" - custom_method :delete_discount, http_verb: :delete, http_path: "discount" - def delete_discount(params = {}, opts = {}) request_stripe_object( method: :delete, - path: resource_url + "/discount", + path: format("/v1/subscriptions/%s/discount", { subscription_exposed_id: CGI.escape(self["id"]) }), + params: params, + opts: opts + ) + end + + def self.delete_discount(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 ) 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 de25d7145..d56c118e2 100644 --- a/lib/stripe/resources/subscription_schedule.rb +++ b/lib/stripe/resources/subscription_schedule.rb @@ -9,13 +9,10 @@ class SubscriptionSchedule < APIResource OBJECT_NAME = "subscription_schedule" - custom_method :cancel, http_verb: :post - custom_method :release, http_verb: :post - def cancel(params = {}, opts = {}) request_stripe_object( method: :post, - path: resource_url + "/cancel", + path: format("/v1/subscription_schedules/%s/cancel", { schedule: CGI.escape(self["id"]) }), params: params, opts: opts ) @@ -24,7 +21,25 @@ def cancel(params = {}, opts = {}) def release(params = {}, opts = {}) request_stripe_object( method: :post, - path: resource_url + "/release", + path: format("/v1/subscription_schedules/%s/release", { schedule: CGI.escape(self["id"]) }), + params: params, + opts: opts + ) + end + + def self.cancel(schedule, params = {}, opts = {}) + request_stripe_object( + method: :post, + path: format("/v1/subscription_schedules/%s/cancel", { schedule: CGI.escape(schedule) }), + params: params, + opts: opts + ) + end + + def self.release(schedule, params = {}, opts = {}) + request_stripe_object( + method: :post, + path: format("/v1/subscription_schedules/%s/release", { schedule: CGI.escape(schedule) }), params: params, opts: opts ) diff --git a/lib/stripe/resources/terminal/reader.rb b/lib/stripe/resources/terminal/reader.rb index 9401c0ae2..52d088977 100644 --- a/lib/stripe/resources/terminal/reader.rb +++ b/lib/stripe/resources/terminal/reader.rb @@ -11,15 +11,10 @@ class Reader < APIResource OBJECT_NAME = "terminal.reader" - custom_method :cancel_action, http_verb: :post - custom_method :process_payment_intent, http_verb: :post - custom_method :process_setup_intent, http_verb: :post - custom_method :set_reader_display, http_verb: :post - def cancel_action(params = {}, opts = {}) request_stripe_object( method: :post, - path: resource_url + "/cancel_action", + path: format("/v1/terminal/readers/%s/cancel_action", { reader: CGI.escape(self["id"]) }), params: params, opts: opts ) @@ -28,7 +23,7 @@ def cancel_action(params = {}, opts = {}) def process_payment_intent(params = {}, opts = {}) request_stripe_object( method: :post, - path: resource_url + "/process_payment_intent", + path: format("/v1/terminal/readers/%s/process_payment_intent", { reader: CGI.escape(self["id"]) }), params: params, opts: opts ) @@ -37,7 +32,7 @@ def process_payment_intent(params = {}, opts = {}) def process_setup_intent(params = {}, opts = {}) request_stripe_object( method: :post, - path: resource_url + "/process_setup_intent", + path: format("/v1/terminal/readers/%s/process_setup_intent", { reader: CGI.escape(self["id"]) }), params: params, opts: opts ) @@ -46,7 +41,43 @@ def process_setup_intent(params = {}, opts = {}) def set_reader_display(params = {}, opts = {}) request_stripe_object( method: :post, - path: resource_url + "/set_reader_display", + path: format("/v1/terminal/readers/%s/set_reader_display", { reader: CGI.escape(self["id"]) }), + params: params, + opts: opts + ) + end + + def self.cancel_action(reader, params = {}, opts = {}) + request_stripe_object( + method: :post, + path: format("/v1/terminal/readers/%s/cancel_action", { reader: CGI.escape(reader) }), + params: params, + opts: opts + ) + end + + def self.process_payment_intent(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 + ) + end + + def self.process_setup_intent(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 + ) + end + + def self.set_reader_display(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 ) @@ -59,12 +90,19 @@ def test_helpers class TestHelpers < APIResourceTestHelpers RESOURCE_CLASS = Reader - custom_method :present_payment_method, http_verb: :post + def self.present_payment_method(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 + ) + end def present_payment_method(params = {}, opts = {}) @resource.request_stripe_object( method: :post, - path: resource_url + "/present_payment_method", + path: format("/v1/test_helpers/terminal/readers/%s/present_payment_method", { reader: CGI.escape(@resource["id"]) }), params: params, opts: opts ) diff --git a/lib/stripe/resources/test_helpers/test_clock.rb b/lib/stripe/resources/test_helpers/test_clock.rb index 72ef181da..3f93e56ec 100644 --- a/lib/stripe/resources/test_helpers/test_clock.rb +++ b/lib/stripe/resources/test_helpers/test_clock.rb @@ -10,12 +10,19 @@ class TestClock < APIResource OBJECT_NAME = "test_helpers.test_clock" - custom_method :advance, http_verb: :post - def advance(params = {}, opts = {}) request_stripe_object( method: :post, - path: resource_url + "/advance", + path: format("/v1/test_helpers/test_clocks/%s/advance", { test_clock: CGI.escape(self["id"]) }), + params: params, + opts: opts + ) + end + + def self.advance(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 ) diff --git a/lib/stripe/resources/topup.rb b/lib/stripe/resources/topup.rb index cf0a5574c..22e6c9079 100644 --- a/lib/stripe/resources/topup.rb +++ b/lib/stripe/resources/topup.rb @@ -9,12 +9,19 @@ class Topup < APIResource OBJECT_NAME = "topup" - custom_method :cancel, http_verb: :post - def cancel(params = {}, opts = {}) request_stripe_object( method: :post, - path: resource_url + "/cancel", + path: format("/v1/topups/%s/cancel", { topup: CGI.escape(self["id"]) }), + params: params, + opts: opts + ) + end + + def self.cancel(topup, params = {}, opts = {}) + request_stripe_object( + method: :post, + path: format("/v1/topups/%s/cancel", { topup: CGI.escape(topup) }), params: params, opts: opts ) diff --git a/lib/stripe/resources/transfer.rb b/lib/stripe/resources/transfer.rb index 8c3fa984a..df65a2672 100644 --- a/lib/stripe/resources/transfer.rb +++ b/lib/stripe/resources/transfer.rb @@ -10,15 +10,22 @@ class Transfer < APIResource OBJECT_NAME = "transfer" - custom_method :cancel, http_verb: :post - nested_resource_class_methods :reversal, operations: %i[create retrieve update list] def cancel(params = {}, opts = {}) request_stripe_object( method: :post, - path: resource_url + "/cancel", + path: format("/v1/transfers/%s/cancel", { id: CGI.escape(self["id"]) }), + params: params, + opts: opts + ) + end + + def self.cancel(id, params = {}, opts = {}) + request_stripe_object( + method: :post, + path: format("/v1/transfers/%s/cancel", { id: CGI.escape(id) }), params: params, opts: opts ) diff --git a/lib/stripe/resources/treasury/financial_account.rb b/lib/stripe/resources/treasury/financial_account.rb index bc0df0cde..9a9d42773 100644 --- a/lib/stripe/resources/treasury/financial_account.rb +++ b/lib/stripe/resources/treasury/financial_account.rb @@ -10,13 +10,10 @@ class FinancialAccount < APIResource OBJECT_NAME = "treasury.financial_account" - custom_method :retrieve_features, http_verb: :get, http_path: "features" - custom_method :update_features, http_verb: :post, http_path: "features" - def retrieve_features(params = {}, opts = {}) request_stripe_object( method: :get, - path: resource_url + "/features", + path: format("/v1/treasury/financial_accounts/%s/features", { financial_account: CGI.escape(self["id"]) }), params: params, opts: opts ) @@ -25,7 +22,25 @@ def retrieve_features(params = {}, opts = {}) def update_features(params = {}, opts = {}) request_stripe_object( method: :post, - path: resource_url + "/features", + path: format("/v1/treasury/financial_accounts/%s/features", { financial_account: CGI.escape(self["id"]) }), + params: params, + opts: opts + ) + end + + def self.retrieve_features(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 + ) + end + + def self.update_features(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 ) diff --git a/lib/stripe/resources/treasury/inbound_transfer.rb b/lib/stripe/resources/treasury/inbound_transfer.rb index d7b322030..57f23e027 100644 --- a/lib/stripe/resources/treasury/inbound_transfer.rb +++ b/lib/stripe/resources/treasury/inbound_transfer.rb @@ -9,12 +9,19 @@ class InboundTransfer < APIResource OBJECT_NAME = "treasury.inbound_transfer" - custom_method :cancel, http_verb: :post - def cancel(params = {}, opts = {}) request_stripe_object( method: :post, - path: resource_url + "/cancel", + path: format("/v1/treasury/inbound_transfers/%s/cancel", { inbound_transfer: CGI.escape(self["id"]) }), + params: params, + opts: opts + ) + end + + def self.cancel(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 ) @@ -27,14 +34,37 @@ def test_helpers class TestHelpers < APIResourceTestHelpers RESOURCE_CLASS = InboundTransfer - custom_method :fail, http_verb: :post - custom_method :return_inbound_transfer, http_verb: :post, http_path: "return" - custom_method :succeed, http_verb: :post + def self.fail(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 + ) + end + + def self.return_inbound_transfer(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 + ) + end + + def self.succeed(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 + ) + end def fail(params = {}, opts = {}) @resource.request_stripe_object( method: :post, - path: resource_url + "/fail", + path: format("/v1/test_helpers/treasury/inbound_transfers/%s/fail", { id: CGI.escape(@resource["id"]) }), params: params, opts: opts ) @@ -43,7 +73,7 @@ def fail(params = {}, opts = {}) def return_inbound_transfer(params = {}, opts = {}) @resource.request_stripe_object( method: :post, - path: resource_url + "/return", + path: format("/v1/test_helpers/treasury/inbound_transfers/%s/return", { id: CGI.escape(@resource["id"]) }), params: params, opts: opts ) @@ -52,7 +82,7 @@ def return_inbound_transfer(params = {}, opts = {}) def succeed(params = {}, opts = {}) @resource.request_stripe_object( method: :post, - path: resource_url + "/succeed", + path: format("/v1/test_helpers/treasury/inbound_transfers/%s/succeed", { id: CGI.escape(@resource["id"]) }), params: params, opts: opts ) diff --git a/lib/stripe/resources/treasury/outbound_payment.rb b/lib/stripe/resources/treasury/outbound_payment.rb index 2757ccad0..d87e666f1 100644 --- a/lib/stripe/resources/treasury/outbound_payment.rb +++ b/lib/stripe/resources/treasury/outbound_payment.rb @@ -9,12 +9,19 @@ class OutboundPayment < APIResource OBJECT_NAME = "treasury.outbound_payment" - custom_method :cancel, http_verb: :post - def cancel(params = {}, opts = {}) request_stripe_object( method: :post, - path: resource_url + "/cancel", + path: format("/v1/treasury/outbound_payments/%s/cancel", { id: CGI.escape(self["id"]) }), + params: params, + opts: opts + ) + end + + def self.cancel(id, params = {}, opts = {}) + request_stripe_object( + method: :post, + path: format("/v1/treasury/outbound_payments/%s/cancel", { id: CGI.escape(id) }), params: params, opts: opts ) @@ -27,14 +34,37 @@ def test_helpers class TestHelpers < APIResourceTestHelpers RESOURCE_CLASS = OutboundPayment - custom_method :fail, http_verb: :post - custom_method :post, http_verb: :post - custom_method :return_outbound_payment, http_verb: :post, http_path: "return" + def self.fail(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 + ) + end + + def self.post(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 + ) + end + + def self.return_outbound_payment(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 + ) + end def fail(params = {}, opts = {}) @resource.request_stripe_object( method: :post, - path: resource_url + "/fail", + path: format("/v1/test_helpers/treasury/outbound_payments/%s/fail", { id: CGI.escape(@resource["id"]) }), params: params, opts: opts ) @@ -43,7 +73,7 @@ def fail(params = {}, opts = {}) def post(params = {}, opts = {}) @resource.request_stripe_object( method: :post, - path: resource_url + "/post", + path: format("/v1/test_helpers/treasury/outbound_payments/%s/post", { id: CGI.escape(@resource["id"]) }), params: params, opts: opts ) @@ -52,7 +82,7 @@ def post(params = {}, opts = {}) def return_outbound_payment(params = {}, opts = {}) @resource.request_stripe_object( method: :post, - path: resource_url + "/return", + path: format("/v1/test_helpers/treasury/outbound_payments/%s/return", { id: CGI.escape(@resource["id"]) }), params: params, opts: opts ) diff --git a/lib/stripe/resources/treasury/outbound_transfer.rb b/lib/stripe/resources/treasury/outbound_transfer.rb index db936b851..48e74f663 100644 --- a/lib/stripe/resources/treasury/outbound_transfer.rb +++ b/lib/stripe/resources/treasury/outbound_transfer.rb @@ -9,12 +9,19 @@ class OutboundTransfer < APIResource OBJECT_NAME = "treasury.outbound_transfer" - custom_method :cancel, http_verb: :post - def cancel(params = {}, opts = {}) request_stripe_object( method: :post, - path: resource_url + "/cancel", + path: format("/v1/treasury/outbound_transfers/%s/cancel", { outbound_transfer: CGI.escape(self["id"]) }), + params: params, + opts: opts + ) + end + + def self.cancel(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 ) @@ -27,14 +34,41 @@ def test_helpers class TestHelpers < APIResourceTestHelpers RESOURCE_CLASS = OutboundTransfer - custom_method :fail, http_verb: :post - custom_method :post, http_verb: :post - custom_method :return_outbound_transfer, http_verb: :post, http_path: "return" + def self.fail(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 + ) + end + + def self.post(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 + ) + end + + def self.return_outbound_transfer( + 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 + ) + end def fail(params = {}, opts = {}) @resource.request_stripe_object( method: :post, - path: resource_url + "/fail", + path: format("/v1/test_helpers/treasury/outbound_transfers/%s/fail", { outbound_transfer: CGI.escape(@resource["id"]) }), params: params, opts: opts ) @@ -43,7 +77,7 @@ def fail(params = {}, opts = {}) def post(params = {}, opts = {}) @resource.request_stripe_object( method: :post, - path: resource_url + "/post", + path: format("/v1/test_helpers/treasury/outbound_transfers/%s/post", { outbound_transfer: CGI.escape(@resource["id"]) }), params: params, opts: opts ) @@ -52,7 +86,7 @@ def post(params = {}, opts = {}) def return_outbound_transfer(params = {}, opts = {}) @resource.request_stripe_object( method: :post, - path: resource_url + "/return", + path: format("/v1/test_helpers/treasury/outbound_transfers/%s/return", { outbound_transfer: CGI.escape(@resource["id"]) }), params: params, opts: 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/stripe_object.rb b/lib/stripe/stripe_object.rb index b9c39c69b..555143a3d 100644 --- a/lib/stripe/stripe_object.rb +++ b/lib/stripe/stripe_object.rb @@ -148,7 +148,7 @@ def inspect def update_attributes(values, opts = {}, dirty: true) values.each do |k, v| add_accessors([k], values) unless metaclass.method_defined?(k.to_sym) - @values[k] = Util.convert_to_stripe_object(v, opts) + @values[k] = Util.convert_to_stripe_object(v, {}, opts) dirty_value!(@values[k]) if dirty @unsaved_values.add(k) end @@ -354,7 +354,7 @@ class << self; self; end "We interpret empty strings as nil in requests. " \ "You may set (object).#{k} = nil to delete the property." end - @values[k] = Util.convert_to_stripe_object(v, @opts) + @values[k] = Util.convert_to_stripe_object(v, {}, @opts) dirty_value!(@values[k]) @unsaved_values.add(k) end @@ -515,7 +515,7 @@ class << self; self; end # example by appending a new hash onto `additional_owners` for an # account. elsif value.is_a?(Hash) - Util.convert_to_stripe_object(value, @opts).serialize_params + Util.convert_to_stripe_object(value, {}, @opts).serialize_params elsif value.is_a?(StripeObject) update = value.serialize_params(force: force) diff --git a/lib/stripe/util.rb b/lib/stripe/util.rb index 6243bb391..96668c580 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(resp.data, {}, opts) end end @@ -104,19 +104,29 @@ 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 = {}) + def self.convert_to_stripe_object(data, params = {}, opts = {}) opts = normalize_opts(opts) case data when Array - data.map { |i| convert_to_stripe_object(i, opts) } + data.map { |i| convert_to_stripe_object(i, {}, 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 + end + + obj else data end diff --git a/test/stripe/account_test.rb b/test/stripe/account_test.rb index e4a135e31..c95a284f3 100644 --- a/test/stripe/account_test.rb +++ b/test/stripe/account_test.rb @@ -118,7 +118,7 @@ class AccountTest < Test::Unit::TestCase object: "account", legal_entity: Stripe::StripeObject.construct_from({ }), - }, {}) + }, nil, {}) obj.legal_entity.additional_owners = [ { first_name: "Joe" }, { first_name: "Jane" }, @@ -144,7 +144,7 @@ class AccountTest < Test::Unit::TestCase Stripe::StripeObject.construct_from(first_name: "Jane"), ], }, - }, {}) + }, nil, {}) obj.legal_entity.additional_owners[1].first_name = "Stripe" expected = { @@ -166,7 +166,7 @@ class AccountTest < Test::Unit::TestCase Stripe::StripeObject.construct_from(first_name: "Jane"), ], }, - }, {}) + }, nil, {}) expected = { legal_entity: { @@ -187,7 +187,7 @@ class AccountTest < Test::Unit::TestCase Stripe::StripeObject.construct_from(first_name: "Jane"), ], }, - }, {}) + }, nil, {}) obj.legal_entity.additional_owners = nil expected = { @@ -201,7 +201,7 @@ class AccountTest < Test::Unit::TestCase should "serialize on a new individual" do obj = Stripe::Util.convert_to_stripe_object({ object: "account", - }, {}) + }, nil, {}) obj.individual = { first_name: "Jane" } expected = { individual: { first_name: "Jane" } } @@ -214,8 +214,8 @@ class AccountTest < Test::Unit::TestCase individual: Stripe::Util.convert_to_stripe_object({ object: "person", first_name: "Jenny", - }, {}), - }, {}) + }, nil, {}), + }, nil, {}) obj.individual = { first_name: "Jane" } expected = { individual: { first_name: "Jane" } } @@ -228,8 +228,8 @@ class AccountTest < Test::Unit::TestCase individual: Stripe::Util.convert_to_stripe_object({ object: "person", first_name: "Jenny", - }, {}), - }, {}) + }, nil, {}), + }, nil, {}) expected = { individual: {} } assert_equal(expected, obj.serialize_params) @@ -241,8 +241,8 @@ class AccountTest < Test::Unit::TestCase individual: Stripe::Util.convert_to_stripe_object({ object: "person", first_name: "Jenny", - }, {}), - }, {}) + }, nil, {}), + }, nil, {}) obj.individual = nil expected = { individual: "" } 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/file_test.rb b/test/stripe/file_test.rb index 1b16f5bbe..8bf93c2e7 100644 --- a/test/stripe/file_test.rb +++ b/test/stripe/file_test.rb @@ -75,12 +75,12 @@ class FileTest < Test::Unit::TestCase end should "be deserializable when `object=file`" do - file = Stripe::Util.convert_to_stripe_object({ object: "file" }, {}) + file = Stripe::Util.convert_to_stripe_object({ object: "file" }, {}, {}) assert file.is_a?(Stripe::File) end should "be deserializable when `object=file_upload`" do - file = Stripe::Util.convert_to_stripe_object({ object: "file_upload" }, {}) + file = Stripe::Util.convert_to_stripe_object({ object: "file_upload" }, {}, {}) assert file.is_a?(Stripe::File) end end diff --git a/test/stripe/list_object_test.rb b/test/stripe/list_object_test.rb index 09f842ba2..2450e5595 100644 --- a/test/stripe/list_object_test.rb +++ b/test/stripe/list_object_test.rb @@ -20,7 +20,7 @@ class ListObjectTest < Test::Unit::TestCase { id: 2 }, { id: 3 }, ] - expected = Util.convert_to_stripe_object(arr, {}) + expected = Util.convert_to_stripe_object(arr, {}, {}) list = Stripe::ListObject.construct_from(data: arr) assert_equal expected, list.each.to_a end @@ -31,7 +31,7 @@ class ListObjectTest < Test::Unit::TestCase { id: 2 }, { id: 3 }, ] - expected = Util.convert_to_stripe_object(arr.reverse, {}) + expected = Util.convert_to_stripe_object(arr.reverse, {}, {}) list = Stripe::ListObject.construct_from(data: arr) assert_equal expected, list.reverse_each.to_a end @@ -45,13 +45,14 @@ class ListObjectTest < Test::Unit::TestCase { id: 5 }, { id: 6 }, ] - expected = Util.convert_to_stripe_object(arr, {}) + expected = Util.convert_to_stripe_object(arr, {}, {}) # Initial list object to page on. Notably, its last data element will be # 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 @@ -77,13 +80,14 @@ class ListObjectTest < Test::Unit::TestCase { id: 2 }, { id: 1 }, ] - expected = Util.convert_to_stripe_object(arr, {}) + expected = Util.convert_to_stripe_object(arr, {}, {}) # Initial list object to page on. Notably, its first data element will be # 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 @@ -109,14 +115,15 @@ class ListObjectTest < Test::Unit::TestCase { id: 2 }, { id: 3 }, ] - expected = Util.convert_to_stripe_object(arr, {}) + expected = Util.convert_to_stripe_object(arr, {}, {}) 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..6fb1b372a 100644 --- a/test/stripe/search_result_object_test.rb +++ b/test/stripe/search_result_object_test.rb @@ -20,7 +20,7 @@ class SearchResultObjectTest < Test::Unit::TestCase { id: 2 }, { id: 3 }, ] - expected = Util.convert_to_stripe_object(arr, {}) + expected = Util.convert_to_stripe_object(arr, {}, {}) list = Stripe::SearchResultObject.construct_from(data: arr) assert_equal expected, list.each.to_a end @@ -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 @@ -61,7 +62,7 @@ class SearchResultObjectTest < Test::Unit::TestCase { id: 2 }, { id: 3 }, ] - expected = Util.convert_to_stripe_object(arr, {}) + expected = Util.convert_to_stripe_object(arr, {}, {}) list = TestSearchResultObject.construct_from(data: [{ id: 1 }], has_more: true, @@ -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 diff --git a/test/stripe/util_test.rb b/test/stripe/util_test.rb index 71b6435e1..772d3d469 100644 --- a/test/stripe/util_test.rb +++ b/test/stripe/util_test.rb @@ -107,28 +107,28 @@ class UtilTest < Test::Unit::TestCase end should "#convert_to_stripe_object should pass through unknown types" do - obj = Util.convert_to_stripe_object(7, {}) + obj = Util.convert_to_stripe_object(7, {}, {}) assert_equal 7, obj end should "#convert_to_stripe_object should turn hashes into StripeObjects" do - obj = Util.convert_to_stripe_object({ foo: "bar" }, {}) + obj = Util.convert_to_stripe_object({ foo: "bar" }, {}, {}) assert obj.is_a?(StripeObject) assert_equal "bar", obj.foo end should "#convert_to_stripe_object should turn lists into ListObjects" do - obj = Util.convert_to_stripe_object({ object: "list" }, {}) + obj = Util.convert_to_stripe_object({ object: "list" }, {}, {}) assert obj.is_a?(ListObject) end should "#convert_to_stripe_object should marshal other classes" do - obj = Util.convert_to_stripe_object({ object: "account" }, {}) + obj = Util.convert_to_stripe_object({ object: "account" }, {}, {}) assert obj.is_a?(Account) end should "#convert_to_stripe_object should marshal arrays" do - obj = Util.convert_to_stripe_object([1, 2, 3], {}) + obj = Util.convert_to_stripe_object([1, 2, 3], {}, {}) assert_equal [1, 2, 3], obj end From dbae531c96e10aff2bfc999bf9b34a71846e535c Mon Sep 17 00:00:00 2001 From: Pavel Krymets Date: Thu, 9 Jun 2022 10:45:40 -0700 Subject: [PATCH 2/7] say no to nil --- test/stripe/account_test.rb | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/test/stripe/account_test.rb b/test/stripe/account_test.rb index c95a284f3..b758c2c22 100644 --- a/test/stripe/account_test.rb +++ b/test/stripe/account_test.rb @@ -118,7 +118,7 @@ class AccountTest < Test::Unit::TestCase object: "account", legal_entity: Stripe::StripeObject.construct_from({ }), - }, nil, {}) + }, {}, {}) obj.legal_entity.additional_owners = [ { first_name: "Joe" }, { first_name: "Jane" }, @@ -144,7 +144,7 @@ class AccountTest < Test::Unit::TestCase Stripe::StripeObject.construct_from(first_name: "Jane"), ], }, - }, nil, {}) + }, {}, {}) obj.legal_entity.additional_owners[1].first_name = "Stripe" expected = { @@ -166,7 +166,7 @@ class AccountTest < Test::Unit::TestCase Stripe::StripeObject.construct_from(first_name: "Jane"), ], }, - }, nil, {}) + }, {}, {}) expected = { legal_entity: { @@ -187,7 +187,7 @@ class AccountTest < Test::Unit::TestCase Stripe::StripeObject.construct_from(first_name: "Jane"), ], }, - }, nil, {}) + }, {}, {}) obj.legal_entity.additional_owners = nil expected = { @@ -201,7 +201,7 @@ class AccountTest < Test::Unit::TestCase should "serialize on a new individual" do obj = Stripe::Util.convert_to_stripe_object({ object: "account", - }, nil, {}) + }, {}, {}) obj.individual = { first_name: "Jane" } expected = { individual: { first_name: "Jane" } } @@ -214,8 +214,8 @@ class AccountTest < Test::Unit::TestCase individual: Stripe::Util.convert_to_stripe_object({ object: "person", first_name: "Jenny", - }, nil, {}), - }, nil, {}) + }, {}, {}), + }, {}, {}) obj.individual = { first_name: "Jane" } expected = { individual: { first_name: "Jane" } } @@ -228,8 +228,8 @@ class AccountTest < Test::Unit::TestCase individual: Stripe::Util.convert_to_stripe_object({ object: "person", first_name: "Jenny", - }, nil, {}), - }, nil, {}) + }, {}, {}), + }, {}, {}) expected = { individual: {} } assert_equal(expected, obj.serialize_params) @@ -241,8 +241,8 @@ class AccountTest < Test::Unit::TestCase individual: Stripe::Util.convert_to_stripe_object({ object: "person", first_name: "Jenny", - }, nil, {}), - }, nil, {}) + }, {}, {}), + }, {}, {}) obj.individual = nil expected = { individual: "" } From 2ff02ceef0daa076ce49f7a775a8b053fdd3fd2d Mon Sep 17 00:00:00 2001 From: Pavel Krymets Date: Fri, 10 Jun 2022 09:34:57 -0700 Subject: [PATCH 3/7] fb --- lib/stripe/api_operations/nested_resource.rb | 41 ++++++++++++++------ lib/stripe/api_operations/request.rb | 4 +- lib/stripe/api_resource.rb | 2 +- lib/stripe/list_object.rb | 2 +- lib/stripe/oauth.rb | 4 +- lib/stripe/stripe_object.rb | 6 +-- lib/stripe/util.rb | 24 ++++++++++-- test/stripe/account_test.rb | 22 +++++------ test/stripe/file_test.rb | 4 +- test/stripe/list_object_test.rb | 10 ++--- test/stripe/search_result_object_test.rb | 4 +- test/stripe/util_test.rb | 10 ++--- 12 files changed, 84 insertions(+), 49 deletions(-) diff --git a/lib/stripe/api_operations/nested_resource.rb b/lib/stripe/api_operations/nested_resource.rb index e31313015..791d04523 100644 --- a/lib/stripe/api_operations/nested_resource.rb +++ b/lib/stripe/api_operations/nested_resource.rb @@ -31,37 +31,56 @@ def nested_resource_class_methods(resource, path: nil, operations: nil, 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) + request_stripe_object( + method: :post, + path: url, + 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) + request_stripe_object( + method: :get, + path: url, + 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) + request_stripe_object( + method: :post, + path: url, + 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) + request_stripe_object( + method: :delete, + path: url, + 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) + request_stripe_object( + method: :get, + path: url, + params: params, + opts: opts + ) end else raise ArgumentError, "Unknown operation: #{operation.inspect}" diff --git a/lib/stripe/api_operations/request.rb b/lib/stripe/api_operations/request.rb index 7960f2b58..8544dedd6 100644 --- a/lib/stripe/api_operations/request.rb +++ b/lib/stripe/api_operations/request.rb @@ -26,7 +26,7 @@ def execute_resource_request_stream(method, url, private def request_stripe_object(method:, path:, params:, opts: {}) resp, opts = execute_resource_request(method, path, params, opts) - Util.convert_to_stripe_object(resp.data, params, opts) + Util.convert_to_stripe_object_with_params(resp.data, params, opts) end private def execute_resource_request_internal(client_request_method_sym, @@ -129,7 +129,7 @@ def self.included(base) private def request_stripe_object(method:, path:, params:, opts: {}) resp, opts = execute_resource_request(method, path, params, opts) - Util.convert_to_stripe_object(resp.data, params, opts) + Util.convert_to_stripe_object_with_params(resp.data, params, opts) end # See notes on `alias` above. diff --git a/lib/stripe/api_resource.rb b/lib/stripe/api_resource.rb index 69416515d..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, params, opts) + Util.convert_to_stripe_object_with_params(resp.data, params, opts) end end diff --git a/lib/stripe/list_object.rb b/lib/stripe/list_object.rb index d5d74f055..3813a3603 100644 --- a/lib/stripe/list_object.rb +++ b/lib/stripe/list_object.rb @@ -94,7 +94,7 @@ def retrieve(id, opts = {}) id, retrieve_params = Util.normalize_id(id) url = "#{resource_url}/#{CGI.escape(id)}" resp, opts = execute_resource_request(:get, url, retrieve_params, opts) - Util.convert_to_stripe_object(resp.data, {}, opts) + Util.convert_to_stripe_object(resp.data, opts) end # Fetches the next page in the resource list (if there is one). diff --git a/lib/stripe/oauth.rb b/lib/stripe/oauth.rb index b89117755..580b424c8 100644 --- a/lib/stripe/oauth.rb +++ b/lib/stripe/oauth.rb @@ -49,7 +49,7 @@ def self.token(params = {}, opts = {}) :post, "/oauth/token", params, opts ) # This is just going to return a generic StripeObject, but that's okay - Util.convert_to_stripe_object(resp.data, {}, opts) + Util.convert_to_stripe_object(resp.data, opts) end def self.deauthorize(params = {}, opts = {}) @@ -59,7 +59,7 @@ def self.deauthorize(params = {}, opts = {}) :post, "/oauth/deauthorize", params, opts ) # This is just going to return a generic StripeObject, but that's okay - Util.convert_to_stripe_object(resp.data, {}, opts) + Util.convert_to_stripe_object(resp.data, opts) end end end diff --git a/lib/stripe/stripe_object.rb b/lib/stripe/stripe_object.rb index 555143a3d..b9c39c69b 100644 --- a/lib/stripe/stripe_object.rb +++ b/lib/stripe/stripe_object.rb @@ -148,7 +148,7 @@ def inspect def update_attributes(values, opts = {}, dirty: true) values.each do |k, v| add_accessors([k], values) unless metaclass.method_defined?(k.to_sym) - @values[k] = Util.convert_to_stripe_object(v, {}, opts) + @values[k] = Util.convert_to_stripe_object(v, opts) dirty_value!(@values[k]) if dirty @unsaved_values.add(k) end @@ -354,7 +354,7 @@ class << self; self; end "We interpret empty strings as nil in requests. " \ "You may set (object).#{k} = nil to delete the property." end - @values[k] = Util.convert_to_stripe_object(v, {}, @opts) + @values[k] = Util.convert_to_stripe_object(v, @opts) dirty_value!(@values[k]) @unsaved_values.add(k) end @@ -515,7 +515,7 @@ class << self; self; end # example by appending a new hash onto `additional_owners` for an # account. elsif value.is_a?(Hash) - Util.convert_to_stripe_object(value, {}, @opts).serialize_params + Util.convert_to_stripe_object(value, @opts).serialize_params elsif value.is_a?(StripeObject) update = value.serialize_params(force: force) diff --git a/lib/stripe/util.rb b/lib/stripe/util.rb index 96668c580..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 @@ -108,12 +108,28 @@ def self.custom_method(resource, target, name, http_verb, http_path) # 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, params = {}, opts = {}) + 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 when Array - data.map { |i| convert_to_stripe_object(i, {}, opts) } + data.map { |i| convert_to_stripe_object(i, opts) } when Hash # Try converting to a known object class. If none available, fall back # to generic StripeObject @@ -123,7 +139,7 @@ def self.convert_to_stripe_object(data, params = {}, 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 + obj.filters = params.dup end obj diff --git a/test/stripe/account_test.rb b/test/stripe/account_test.rb index b758c2c22..e4a135e31 100644 --- a/test/stripe/account_test.rb +++ b/test/stripe/account_test.rb @@ -118,7 +118,7 @@ class AccountTest < Test::Unit::TestCase object: "account", legal_entity: Stripe::StripeObject.construct_from({ }), - }, {}, {}) + }, {}) obj.legal_entity.additional_owners = [ { first_name: "Joe" }, { first_name: "Jane" }, @@ -144,7 +144,7 @@ class AccountTest < Test::Unit::TestCase Stripe::StripeObject.construct_from(first_name: "Jane"), ], }, - }, {}, {}) + }, {}) obj.legal_entity.additional_owners[1].first_name = "Stripe" expected = { @@ -166,7 +166,7 @@ class AccountTest < Test::Unit::TestCase Stripe::StripeObject.construct_from(first_name: "Jane"), ], }, - }, {}, {}) + }, {}) expected = { legal_entity: { @@ -187,7 +187,7 @@ class AccountTest < Test::Unit::TestCase Stripe::StripeObject.construct_from(first_name: "Jane"), ], }, - }, {}, {}) + }, {}) obj.legal_entity.additional_owners = nil expected = { @@ -201,7 +201,7 @@ class AccountTest < Test::Unit::TestCase should "serialize on a new individual" do obj = Stripe::Util.convert_to_stripe_object({ object: "account", - }, {}, {}) + }, {}) obj.individual = { first_name: "Jane" } expected = { individual: { first_name: "Jane" } } @@ -214,8 +214,8 @@ class AccountTest < Test::Unit::TestCase individual: Stripe::Util.convert_to_stripe_object({ object: "person", first_name: "Jenny", - }, {}, {}), - }, {}, {}) + }, {}), + }, {}) obj.individual = { first_name: "Jane" } expected = { individual: { first_name: "Jane" } } @@ -228,8 +228,8 @@ class AccountTest < Test::Unit::TestCase individual: Stripe::Util.convert_to_stripe_object({ object: "person", first_name: "Jenny", - }, {}, {}), - }, {}, {}) + }, {}), + }, {}) expected = { individual: {} } assert_equal(expected, obj.serialize_params) @@ -241,8 +241,8 @@ class AccountTest < Test::Unit::TestCase individual: Stripe::Util.convert_to_stripe_object({ object: "person", first_name: "Jenny", - }, {}, {}), - }, {}, {}) + }, {}), + }, {}) obj.individual = nil expected = { individual: "" } diff --git a/test/stripe/file_test.rb b/test/stripe/file_test.rb index 8bf93c2e7..1b16f5bbe 100644 --- a/test/stripe/file_test.rb +++ b/test/stripe/file_test.rb @@ -75,12 +75,12 @@ class FileTest < Test::Unit::TestCase end should "be deserializable when `object=file`" do - file = Stripe::Util.convert_to_stripe_object({ object: "file" }, {}, {}) + file = Stripe::Util.convert_to_stripe_object({ object: "file" }, {}) assert file.is_a?(Stripe::File) end should "be deserializable when `object=file_upload`" do - file = Stripe::Util.convert_to_stripe_object({ object: "file_upload" }, {}, {}) + file = Stripe::Util.convert_to_stripe_object({ object: "file_upload" }, {}) assert file.is_a?(Stripe::File) end end diff --git a/test/stripe/list_object_test.rb b/test/stripe/list_object_test.rb index 2450e5595..f65ee5ee1 100644 --- a/test/stripe/list_object_test.rb +++ b/test/stripe/list_object_test.rb @@ -20,7 +20,7 @@ class ListObjectTest < Test::Unit::TestCase { id: 2 }, { id: 3 }, ] - expected = Util.convert_to_stripe_object(arr, {}, {}) + expected = Util.convert_to_stripe_object(arr, {}) list = Stripe::ListObject.construct_from(data: arr) assert_equal expected, list.each.to_a end @@ -31,7 +31,7 @@ class ListObjectTest < Test::Unit::TestCase { id: 2 }, { id: 3 }, ] - expected = Util.convert_to_stripe_object(arr.reverse, {}, {}) + expected = Util.convert_to_stripe_object(arr.reverse, {}) list = Stripe::ListObject.construct_from(data: arr) assert_equal expected, list.reverse_each.to_a end @@ -45,7 +45,7 @@ class ListObjectTest < Test::Unit::TestCase { id: 5 }, { id: 6 }, ] - expected = Util.convert_to_stripe_object(arr, {}, {}) + expected = Util.convert_to_stripe_object(arr, {}) # Initial list object to page on. Notably, its last data element will be # used as a cursor to fetch the next page. @@ -80,7 +80,7 @@ class ListObjectTest < Test::Unit::TestCase { id: 2 }, { id: 1 }, ] - expected = Util.convert_to_stripe_object(arr, {}, {}) + expected = Util.convert_to_stripe_object(arr, {}) # Initial list object to page on. Notably, its first data element will be # used as a cursor to fetch the next page. @@ -115,7 +115,7 @@ class ListObjectTest < Test::Unit::TestCase { id: 2 }, { id: 3 }, ] - expected = Util.convert_to_stripe_object(arr, {}, {}) + expected = Util.convert_to_stripe_object(arr, {}) list = TestListObject.construct_from(data: [{ id: 1 }], has_more: true, diff --git a/test/stripe/search_result_object_test.rb b/test/stripe/search_result_object_test.rb index 6fb1b372a..fda3a78e6 100644 --- a/test/stripe/search_result_object_test.rb +++ b/test/stripe/search_result_object_test.rb @@ -20,7 +20,7 @@ class SearchResultObjectTest < Test::Unit::TestCase { id: 2 }, { id: 3 }, ] - expected = Util.convert_to_stripe_object(arr, {}, {}) + expected = Util.convert_to_stripe_object(arr, {}) list = Stripe::SearchResultObject.construct_from(data: arr) assert_equal expected, list.each.to_a end @@ -62,7 +62,7 @@ class SearchResultObjectTest < Test::Unit::TestCase { id: 2 }, { id: 3 }, ] - expected = Util.convert_to_stripe_object(arr, {}, {}) + expected = Util.convert_to_stripe_object(arr, {}) list = TestSearchResultObject.construct_from(data: [{ id: 1 }], has_more: true, diff --git a/test/stripe/util_test.rb b/test/stripe/util_test.rb index 772d3d469..71b6435e1 100644 --- a/test/stripe/util_test.rb +++ b/test/stripe/util_test.rb @@ -107,28 +107,28 @@ class UtilTest < Test::Unit::TestCase end should "#convert_to_stripe_object should pass through unknown types" do - obj = Util.convert_to_stripe_object(7, {}, {}) + obj = Util.convert_to_stripe_object(7, {}) assert_equal 7, obj end should "#convert_to_stripe_object should turn hashes into StripeObjects" do - obj = Util.convert_to_stripe_object({ foo: "bar" }, {}, {}) + obj = Util.convert_to_stripe_object({ foo: "bar" }, {}) assert obj.is_a?(StripeObject) assert_equal "bar", obj.foo end should "#convert_to_stripe_object should turn lists into ListObjects" do - obj = Util.convert_to_stripe_object({ object: "list" }, {}, {}) + obj = Util.convert_to_stripe_object({ object: "list" }, {}) assert obj.is_a?(ListObject) end should "#convert_to_stripe_object should marshal other classes" do - obj = Util.convert_to_stripe_object({ object: "account" }, {}, {}) + obj = Util.convert_to_stripe_object({ object: "account" }, {}) assert obj.is_a?(Account) end should "#convert_to_stripe_object should marshal arrays" do - obj = Util.convert_to_stripe_object([1, 2, 3], {}, {}) + obj = Util.convert_to_stripe_object([1, 2, 3], {}) assert_equal [1, 2, 3], obj end From b2ad7f0ca8bb2ca78e5be252e5c4c4f2482b9cb3 Mon Sep 17 00:00:00 2001 From: Pavel Krymets Date: Fri, 10 Jun 2022 09:48:50 -0700 Subject: [PATCH 4/7] Squish --- lib/stripe/api_operations/nested_resource.rb | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/lib/stripe/api_operations/nested_resource.rb b/lib/stripe/api_operations/nested_resource.rb index 791d04523..098c0e4bd 100644 --- a/lib/stripe/api_operations/nested_resource.rb +++ b/lib/stripe/api_operations/nested_resource.rb @@ -30,10 +30,9 @@ def nested_resource_class_methods(resource, path: nil, operations: nil, when :create define_singleton_method(:"create_#{resource}") \ do |id, params = {}, opts = {}| - url = send(resource_url_method, id) request_stripe_object( method: :post, - path: url, + path: send(resource_url_method, id), params: params, opts: opts ) @@ -41,10 +40,9 @@ def nested_resource_class_methods(resource, path: nil, operations: nil, when :retrieve define_singleton_method(:"retrieve_#{resource}") \ do |id, nested_id, opts = {}| - url = send(resource_url_method, id, nested_id) request_stripe_object( method: :get, - path: url, + path: send(resource_url_method, id, nested_id), params: {}, opts: opts ) @@ -52,10 +50,9 @@ def nested_resource_class_methods(resource, path: nil, operations: nil, when :update define_singleton_method(:"update_#{resource}") \ do |id, nested_id, params = {}, opts = {}| - url = send(resource_url_method, id, nested_id) request_stripe_object( method: :post, - path: url, + path: send(resource_url_method, id, nested_id), params: params, opts: opts ) @@ -63,10 +60,9 @@ def nested_resource_class_methods(resource, path: nil, operations: nil, when :delete define_singleton_method(:"delete_#{resource}") \ do |id, nested_id, params = {}, opts = {}| - url = send(resource_url_method, id, nested_id) request_stripe_object( method: :delete, - path: url, + path: send(resource_url_method, id, nested_id), params: params, opts: opts ) @@ -74,10 +70,9 @@ def nested_resource_class_methods(resource, path: nil, operations: nil, when :list define_singleton_method(:"list_#{resource_plural}") \ do |id, params = {}, opts = {}| - url = send(resource_url_method, id) request_stripe_object( method: :get, - path: url, + path: send(resource_url_method, id), params: params, opts: opts ) From cb49737a5f87a8b0a3c8abbe2372c90f3d57646f Mon Sep 17 00:00:00 2001 From: Pavel Krymets Date: Fri, 10 Jun 2022 12:23:41 -0700 Subject: [PATCH 5/7] rubocopter --- lib/stripe/api_operations/nested_resource.rb | 104 ++++++++++--------- 1 file changed, 56 insertions(+), 48 deletions(-) diff --git a/lib/stripe/api_operations/nested_resource.rb b/lib/stripe/api_operations/nested_resource.rb index 098c0e4bd..9c71df299 100644 --- a/lib/stripe/api_operations/nested_resource.rb +++ b/lib/stripe/api_operations/nested_resource.rb @@ -26,60 +26,68 @@ 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(operation, resource_url_method, resource_plural) + end + end + + private def define_operation( + operation, + resource_url_method, + resource_plural + ) + case operation + when :create + define_singleton_method(:"create_#{resource}") \ do |id, params = {}, opts = {}| - request_stripe_object( - method: :post, - path: send(resource_url_method, id), - params: params, - opts: 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 = {}| - request_stripe_object( - method: :get, - path: send(resource_url_method, id, nested_id), - params: {}, - opts: 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 = {}| - 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}") \ + 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 = {}| - 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}") \ + 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 = {}| - request_stripe_object( - method: :get, - path: send(resource_url_method, id), - params: params, - opts: 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 From 0e286c34470058c6801ca74a10c591e530a3aa87 Mon Sep 17 00:00:00 2001 From: Pavel Krymets Date: Fri, 10 Jun 2022 12:26:57 -0700 Subject: [PATCH 6/7] build --- lib/stripe/api_operations/nested_resource.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/stripe/api_operations/nested_resource.rb b/lib/stripe/api_operations/nested_resource.rb index 9c71df299..50811546a 100644 --- a/lib/stripe/api_operations/nested_resource.rb +++ b/lib/stripe/api_operations/nested_resource.rb @@ -26,11 +26,12 @@ def nested_resource_class_methods(resource, path: nil, operations: nil, end operations.each do |operation| - define_operation(operation, resource_url_method, resource_plural) + define_operation(resource, operation, resource_url_method, resource_plural) end end private def define_operation( + resource, operation, resource_url_method, resource_plural From 1a753293ac8af5e4fb853cb74a520246175bb8aa Mon Sep 17 00:00:00 2001 From: Pavel Krymets Date: Fri, 10 Jun 2022 13:14:37 -0700 Subject: [PATCH 7/7] ugh lint --- lib/stripe/api_operations/nested_resource.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/stripe/api_operations/nested_resource.rb b/lib/stripe/api_operations/nested_resource.rb index 50811546a..09b570944 100644 --- a/lib/stripe/api_operations/nested_resource.rb +++ b/lib/stripe/api_operations/nested_resource.rb @@ -26,7 +26,12 @@ def nested_resource_class_methods(resource, path: nil, operations: nil, end operations.each do |operation| - define_operation(resource, operation, resource_url_method, resource_plural) + define_operation( + resource, + operation, + resource_url_method, + resource_plural + ) end end