Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use request_stripe_object for all requests #1071

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/stripe/api_operations/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 12 additions & 7 deletions lib/stripe/api_operations/delete.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 6 additions & 9 deletions lib/stripe/api_operations/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
96 changes: 62 additions & 34 deletions lib/stripe/api_operations/nested_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,46 +26,74 @@ def nested_resource_class_methods(resource, path: nil, operations: nil,
end

operations.each do |operation|
case operation
when :create
define_singleton_method(:"create_#{resource}") \
define_operation(
resource,
operation,
resource_url_method,
resource_plural
)
end
end

private def define_operation(
resource,
operation,
resource_url_method,
resource_plural
)
case operation
when :create
define_singleton_method(:"create_#{resource}") \
do |id, params = {}, opts = {}|
url = send(resource_url_method, id)
resp, opts = execute_resource_request(:post, url, params, opts)
Util.convert_to_stripe_object(resp.data, opts)
end
when :retrieve
define_singleton_method(:"retrieve_#{resource}") \
request_stripe_object(
method: :post,
path: send(resource_url_method, id),
params: params,
opts: opts
)
end
when :retrieve
define_singleton_method(:"retrieve_#{resource}") \
do |id, nested_id, opts = {}|
url = send(resource_url_method, id, nested_id)
resp, opts = execute_resource_request(:get, url, {}, opts)
Util.convert_to_stripe_object(resp.data, opts)
end
when :update
define_singleton_method(:"update_#{resource}") \
request_stripe_object(
method: :get,
path: send(resource_url_method, id, nested_id),
params: {},
opts: opts
)
end
when :update
define_singleton_method(:"update_#{resource}") \
do |id, nested_id, params = {}, opts = {}|
url = send(resource_url_method, id, nested_id)
resp, opts = execute_resource_request(:post, url, params, opts)
Util.convert_to_stripe_object(resp.data, opts)
end
when :delete
define_singleton_method(:"delete_#{resource}") \
request_stripe_object(
method: :post,
path: send(resource_url_method, id, nested_id),
params: params,
opts: opts
)
end
when :delete
define_singleton_method(:"delete_#{resource}") \
do |id, nested_id, params = {}, opts = {}|
url = send(resource_url_method, id, nested_id)
resp, opts = execute_resource_request(:delete, url, params,
opts)
Util.convert_to_stripe_object(resp.data, opts)
end
when :list
define_singleton_method(:"list_#{resource_plural}") \
request_stripe_object(
method: :delete,
path: send(resource_url_method, id, nested_id),
params: params,
opts: opts
)
end
when :list
define_singleton_method(:"list_#{resource_plural}") \
do |id, params = {}, opts = {}|
url = send(resource_url_method, id)
resp, opts = execute_resource_request(:get, url, params, opts)
Util.convert_to_stripe_object(resp.data, opts)
end
else
raise ArgumentError, "Unknown operation: #{operation.inspect}"
request_stripe_object(
method: :get,
path: send(resource_url_method, id),
params: params,
opts: opts
)
end
else
raise ArgumentError, "Unknown operation: #{operation.inspect}"
end
end
end
Expand Down
10 changes: 10 additions & 0 deletions lib/stripe/api_operations/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ def execute_resource_request_stream(method, url,
)
end

private def request_stripe_object(method:, path:, params:, opts: {})
resp, opts = execute_resource_request(method, path, params, opts)
Util.convert_to_stripe_object_with_params(resp.data, params, opts)
end

private def execute_resource_request_internal(client_request_method_sym,
method, url,
params, opts,
Expand Down Expand Up @@ -122,6 +127,11 @@ def self.included(base)
)
end

private def request_stripe_object(method:, path:, params:, opts: {})
pakrym-stripe marked this conversation as resolved.
Show resolved Hide resolved
resp, opts = execute_resource_request(method, path, params, opts)
Util.convert_to_stripe_object_with_params(resp.data, params, opts)
end

# See notes on `alias` above.
alias request execute_resource_request
end
Expand Down
9 changes: 6 additions & 3 deletions lib/stripe/api_operations/save.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
15 changes: 6 additions & 9 deletions lib/stripe/api_operations/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/stripe/api_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def request_stripe_object(method:, path:, params:, opts: {})
if Util.object_name_matches_class?(resp.data[:object], self.class)
initialize_from(resp.data, opts)
else
Util.convert_to_stripe_object(resp.data, opts)
Util.convert_to_stripe_object_with_params(resp.data, params, opts)
end
end

Expand Down
22 changes: 10 additions & 12 deletions lib/stripe/resources/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,21 @@ def reject(params = {}, opts = {})
end

def self.persons(account, params = {}, opts = {})
resp, opts = execute_resource_request(
:get,
format("/v1/accounts/%<account>s/persons", { account: CGI.escape(account) }),
params,
opts
request_stripe_object(
method: :get,
path: format("/v1/accounts/%<account>s/persons", { account: CGI.escape(account) }),
params: params,
opts: opts
)
Util.convert_to_stripe_object(resp.data, opts)
end

def self.reject(account, params = {}, opts = {})
resp, opts = execute_resource_request(
:post,
format("/v1/accounts/%<account>s/reject", { account: CGI.escape(account) }),
params,
opts
request_stripe_object(
method: :post,
path: format("/v1/accounts/%<account>s/reject", { account: CGI.escape(account) }),
params: params,
opts: opts
)
Util.convert_to_stripe_object(resp.data, opts)
end

save_nested_resource :external_account
Expand Down
22 changes: 10 additions & 12 deletions lib/stripe/resources/apps/secret.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,21 @@ class Secret < APIResource
OBJECT_NAME = "apps.secret"

def self.delete_where(params = {}, opts = {})
resp, opts = execute_resource_request(
:post,
"/v1/apps/secrets/delete",
params,
opts
request_stripe_object(
method: :post,
path: "/v1/apps/secrets/delete",
params: params,
opts: opts
)
Util.convert_to_stripe_object(resp.data, opts)
end

def self.find(params = {}, opts = {})
resp, opts = execute_resource_request(
:get,
"/v1/apps/secrets/find",
params,
opts
request_stripe_object(
method: :get,
path: "/v1/apps/secrets/find",
params: params,
opts: opts
)
Util.convert_to_stripe_object(resp.data, opts)
end
end
end
Expand Down
11 changes: 5 additions & 6 deletions lib/stripe/resources/charge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@ def capture(params = {}, opts = {})
end

def self.capture(charge, params = {}, opts = {})
resp, opts = execute_resource_request(
:post,
format("/v1/charges/%<charge>s/capture", { charge: CGI.escape(charge) }),
params,
opts
request_stripe_object(
method: :post,
path: format("/v1/charges/%<charge>s/capture", { charge: CGI.escape(charge) }),
params: params,
opts: opts
)
Util.convert_to_stripe_object(resp.data, opts)
end

def self.search(params = {}, opts = {})
Expand Down
11 changes: 5 additions & 6 deletions lib/stripe/resources/checkout/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,12 @@ def expire(params = {}, opts = {})
end

def self.expire(session, params = {}, opts = {})
resp, opts = execute_resource_request(
:post,
format("/v1/checkout/sessions/%<session>s/expire", { session: CGI.escape(session) }),
params,
opts
request_stripe_object(
method: :post,
path: format("/v1/checkout/sessions/%<session>s/expire", { session: CGI.escape(session) }),
params: params,
opts: opts
)
Util.convert_to_stripe_object(resp.data, opts)
end
end
end
Expand Down
27 changes: 17 additions & 10 deletions lib/stripe/resources/credit_note.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,30 @@ def void_credit_note(params = {}, opts = {})
end

def self.void_credit_note(id, params = {}, opts = {})
resp, opts = execute_resource_request(
:post,
format("/v1/credit_notes/%<id>s/void", { id: CGI.escape(id) }),
params,
opts
request_stripe_object(
method: :post,
path: format("/v1/credit_notes/%<id>s/void", { id: CGI.escape(id) }),
params: params,
opts: opts
)
Util.convert_to_stripe_object(resp.data, opts)
end

def self.preview(params, opts = {})
resp, opts = execute_resource_request(:get, resource_url + "/preview", params, opts)
Util.convert_to_stripe_object(resp.data, opts)
request_stripe_object(
method: :get,
path: resource_url + "/preview",
params: params,
opts: opts
)
end

def self.list_preview_line_items(params, opts = {})
resp, opts = execute_resource_request(:get, resource_url + "/preview/lines", params, opts)
Util.convert_to_stripe_object(resp.data, opts)
request_stripe_object(
method: :get,
path: resource_url + "/preview/lines",
params: params,
opts: opts
)
end
end
end
Loading