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

Singleton retrieve method now requires params to be passed as the first argument #1417

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
22 changes: 1 addition & 21 deletions lib/stripe/api_operations/nested_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ def nested_resource_class_methods(resource, path: nil, operations: nil,
end
end

# rubocop:disable Metrics/MethodLength
private def define_operation(
resource,
operation,
Expand All @@ -54,26 +53,8 @@ def nested_resource_class_methods(resource, path: nil, operations: nil,
)
end
when :retrieve
# TODO: (Major) Split params_or_opts to params and opts and get rid of the complicated way to add params
define_singleton_method(:"retrieve_#{resource}") \
do |id, nested_id, params_or_opts = {}, definitely_opts = nil|
opts = nil
params = nil
if definitely_opts.nil?
unrecognized_key = params_or_opts.keys.find { |k| !Util::OPTS_USER_SPECIFIED.include?(k) }
if unrecognized_key
raise ArgumentError,
"Unrecognized request option: #{unrecognized_key}. Did you mean to specify this as " \
"retrieve params? " \
"If so, you must explicitly pass an opts hash as a fourth argument. " \
"For example: .retrieve(#{id}, #{nested_id}, {#{unrecognized_key}: 'foo'}, {})"
end

opts = params_or_opts
else
opts = definitely_opts
params = params_or_opts
end
do |id, nested_id, params = {}, opts = {}|
request_stripe_object(
method: :get,
path: send(resource_url_method, id, nested_id),
Expand Down Expand Up @@ -115,7 +96,6 @@ def nested_resource_class_methods(resource, path: nil, operations: nil,
raise ArgumentError, "Unknown operation: #{operation.inspect}"
end
end
# rubocop:enable Metrics/MethodLength
end
end
end
19 changes: 1 addition & 18 deletions lib/stripe/singleton_api_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,7 @@ def resource_url
self.class.resource_url
end

def self.retrieve(params_or_opts = {}, definitely_opts = nil)
opts = nil
params = nil
if definitely_opts.nil?
unrecognized_key = params_or_opts.keys.find { |k| !Util::OPTS_USER_SPECIFIED.include?(k) }
if unrecognized_key
raise ArgumentError,
"Unrecognized request option: #{unrecognized_key}. Did you mean to specify this as retrieve params? " \
"If so, you must explicitly pass an opts hash as a second argument. " \
"For example: .retrieve({#{unrecognized_key}: 'foo'}, {})"
end

opts = params_or_opts
else
opts = definitely_opts
params = params_or_opts
end

def self.retrieve(params = {}, opts = {})
instance = new(params, Util.normalize_opts(opts))
instance.refresh
instance
Expand Down
15 changes: 0 additions & 15 deletions test/stripe/api_operations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,6 @@ def self.object_name
assert_equal "bar", nested_resource.foo
end

should "define a retrieve method with only opts" do
stub_request(:get, "#{Stripe.api_base}/v1/mainresources/id/nesteds/nested_id")
.with(headers: { "Stripe-Account" => "acct_123" })
.to_return(body: JSON.generate(id: "nested_id", object: "nested", foo: "bar"))
nested_resource = MainResource.retrieve_nested("id", "nested_id", { stripe_account: "acct_123" })
assert_equal "bar", nested_resource.foo
end

should "define a retrieve method with both opts and params" do
stub_request(:get, "#{Stripe.api_base}/v1/mainresources/id/nesteds/nested_id?expand[]=reverse")
.with(headers: { "Stripe-Account" => "acct_123" })
Expand All @@ -112,13 +104,6 @@ def self.object_name
assert_equal "bar", nested_resource.foo
end

should "warns when attempting to retrieve and pass only params" do
exception = assert_raises(ArgumentError) do
MainResource.retrieve_nested("id", "nested_id", { expand: ["reverse"] })
end
assert_match(/Unrecognized request option/, exception.message)
end

should "define an update method" do
stub_request(:post, "#{Stripe.api_base}/v1/mainresources/id/nesteds/nested_id")
.with(body: { foo: "baz" })
Expand Down
14 changes: 0 additions & 14 deletions test/stripe/balance_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,6 @@ class BalanceTest < Test::Unit::TestCase
assert_requested :get, "#{Stripe.api_base}/v1/balance"
assert balance.is_a?(Stripe::Balance)
end
should "be retrievable with opts only" do
balance = Stripe::Balance.retrieve({ stripe_account: "acct_123" })
assert_requested :get, "#{Stripe.api_base}/v1/balance" do |req|
assert_equal("acct_123", req.headers["Stripe-Account"])
true
end
assert balance.is_a?(Stripe::Balance)
end
should "be retrievable with opts and params" do
balance = Stripe::Balance.retrieve({ expand: ["available"] }, { stripe_account: "acct_123" })
assert_requested :get, "#{Stripe.api_base}/v1/balance?expand[]=available" do |req|
Expand All @@ -33,11 +25,5 @@ class BalanceTest < Test::Unit::TestCase
end
assert balance.is_a?(Stripe::Balance)
end
should "warn you if you are attempting to pass only params" do
exception = assert_raises(ArgumentError) do
Stripe::Balance.retrieve({ expand: ["available"] })
end
assert_match(/Unrecognized request option/, exception.message)
end
end
end
19 changes: 0 additions & 19 deletions test/stripe/transfer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,19 +95,6 @@ class TransferTest < Test::Unit::TestCase
assert_requested :get, "#{Stripe.api_base}/v1/transfers/tr_123/reversals/trr_123?expand%5B%5D=transfer"
assert reversal.is_a?(Stripe::Reversal)
end

should "be retrievable with opts only" do
transfer_reversal = Stripe::Transfer.retrieve_reversal(
"tr_123",
"trr_123",
{ stripe_account: "acct_123" }
)
assert_requested :get, "#{Stripe.api_base}/v1/transfers/tr_123/reversals/trr_123" do |req|
assert_equal("acct_123", req.headers["Stripe-Account"])
true
end
assert transfer_reversal.is_a?(Stripe::Reversal)
end
should "be retrievable with opts and params" do
transfer_reversal = Stripe::Transfer.retrieve_reversal("tr_123",
"trr_123",
Expand All @@ -132,11 +119,5 @@ class TransferTest < Test::Unit::TestCase
end
assert transfer_reversal.is_a?(Stripe::Reversal)
end
should "warn you if you are attempting to pass only params" do
exception = assert_raises(ArgumentError) do
Stripe::Transfer.retrieve_reversal("tr_123", "trr_123", { expand: ["available"] })
end
assert_match(/Unrecognized request option/, exception.message)
end
end
end
Loading