From 3ecb678e093389d9a8c0062dd346f12c342d5126 Mon Sep 17 00:00:00 2001 From: Richard Marmorstein Date: Mon, 27 Nov 2023 16:23:01 -0800 Subject: [PATCH 1/2] Support sending parameters inside singleton retrieve --- lib/stripe/singleton_api_resource.rb | 21 ++++++++++++++++-- test/stripe/balance_test.rb | 32 +++++++++++++++++++++++++++- 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/lib/stripe/singleton_api_resource.rb b/lib/stripe/singleton_api_resource.rb index 9b05a3a05..44746eec9 100644 --- a/lib/stripe/singleton_api_resource.rb +++ b/lib/stripe/singleton_api_resource.rb @@ -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(paramsOrOpts = {}, definitelyOpts = nil) + opts = nil + params = nil + if !definitelyOpts.nil? + opts = definitelyOpts + params = paramsOrOpts + else + unrecognized_key = paramsOrOpts.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 = paramsOrOpts + end + + + instance = new(params, Util.normalize_opts(opts)) instance.refresh instance end diff --git a/test/stripe/balance_test.rb b/test/stripe/balance_test.rb index 56e1b7b4f..de714b406 100644 --- a/test/stripe/balance_test.rb +++ b/test/stripe/balance_test.rb @@ -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 From 9c7a649a9384dc73dbf637ba6d69a25a611d765d Mon Sep 17 00:00:00 2001 From: Annie Li Date: Mon, 11 Dec 2023 11:04:16 -0800 Subject: [PATCH 2/2] lint --- lib/stripe/singleton_api_resource.rb | 22 +++++++++++----------- test/stripe/balance_test.rb | 8 ++++---- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/lib/stripe/singleton_api_resource.rb b/lib/stripe/singleton_api_resource.rb index 44746eec9..205edbbc4 100644 --- a/lib/stripe/singleton_api_resource.rb +++ b/lib/stripe/singleton_api_resource.rb @@ -17,24 +17,24 @@ def resource_url self.class.resource_url end - def self.retrieve(paramsOrOpts = {}, definitelyOpts = nil) + def self.retrieve(params_or_opts = {}, definitely_opts = nil) opts = nil params = nil - if !definitelyOpts.nil? - opts = definitelyOpts - params = paramsOrOpts - else - unrecognized_key = paramsOrOpts.keys.find { |k| !Util::OPTS_USER_SPECIFIED.include?(k) } - if unrecognized_key + 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'}, {})" + "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 = paramsOrOpts + opts = params_or_opts + else + opts = definitely_opts + params = params_or_opts end - instance = new(params, Util.normalize_opts(opts)) instance.refresh instance diff --git a/test/stripe/balance_test.rb b/test/stripe/balance_test.rb index 4c99dc23d..3879ed7b6 100644 --- a/test/stripe/balance_test.rb +++ b/test/stripe/balance_test.rb @@ -10,7 +10,7 @@ class BalanceTest < Test::Unit::TestCase assert balance.is_a?(Stripe::Balance) end should "be retrievable with opts only" do - balance = Stripe::Balance.retrieve({stripe_account: "acct_123"}) + 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 @@ -18,7 +18,7 @@ class BalanceTest < Test::Unit::TestCase 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"}) + 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 @@ -26,7 +26,7 @@ class BalanceTest < Test::Unit::TestCase assert balance.is_a?(Stripe::Balance) end should "be retrievable with params and an explicitly empty opts" do - balance = Stripe::Balance.retrieve({expand: ["available"]}, {}) + 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 @@ -35,7 +35,7 @@ class BalanceTest < Test::Unit::TestCase end should "warn you if you are attempting to pass only params" do exception = assert_raises(ArgumentError) do - Stripe::Balance.retrieve({expand: ["available"]}) + Stripe::Balance.retrieve({ expand: ["available"] }) end assert_match(/Unrecognized request option/, exception.message) end