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

Support sending parameters inside singleton retrieve #1294

Merged
merged 3 commits into from
Dec 11, 2023
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
21 changes: 19 additions & 2 deletions lib/stripe/singleton_api_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,25 @@ def resource_url
self.class.resource_url
end

def self.retrieve(opts = {})
instance = new(nil, Util.normalize_opts(opts))
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

instance = new(params, Util.normalize_opts(opts))
instance.refresh
instance
end
Expand Down
32 changes: 31 additions & 1 deletion test/stripe/balance_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,40 @@

module Stripe
class BalanceTest < Test::Unit::TestCase
should "be retrievable" do
should "be retrievable with no arguments" do
balance = Stripe::Balance.retrieve
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|
assert_equal("acct_123", req.headers["Stripe-Account"])
true
end
assert balance.is_a?(Stripe::Balance)
end
should "be retrievable with params and an explicitly empty opts" do
balance = Stripe::Balance.retrieve({ expand: ["available"] }, {})
assert_requested :get, "#{Stripe.api_base}/v1/balance?expand[]=available" do |req|
assert_nil(req.headers["Stripe-Account"])
true
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