diff --git a/lib/stripe/api_operations/nested_resource.rb b/lib/stripe/api_operations/nested_resource.rb index 09b570944..0efde45c2 100644 --- a/lib/stripe/api_operations/nested_resource.rb +++ b/lib/stripe/api_operations/nested_resource.rb @@ -35,6 +35,7 @@ def nested_resource_class_methods(resource, path: nil, operations: nil, end end + # rubocop:disable Metrics/MethodLength private def define_operation( resource, operation, @@ -53,12 +54,30 @@ 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, opts = {}| + 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 request_stripe_object( method: :get, path: send(resource_url_method, id, nested_id), - params: {}, + params: params, opts: opts ) end @@ -96,6 +115,7 @@ 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 diff --git a/test/stripe/api_operations_test.rb b/test/stripe/api_operations_test.rb index 73fc0db13..562abfb97 100644 --- a/test/stripe/api_operations_test.rb +++ b/test/stripe/api_operations_test.rb @@ -89,6 +89,36 @@ 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" }) + .to_return(body: JSON.generate(id: "nested_id", object: "nested", foo: "bar")) + nested_resource = MainResource.retrieve_nested("id", "nested_id", { expand: ["reverse"] }, { stripe_account: "acct_123" }) + assert_equal "bar", nested_resource.foo + end + + should "define a retrieve method with params and explicitly empty opts" do + stub_request(:get, "#{Stripe.api_base}/v1/mainresources/id/nesteds/nested_id?expand[]=reverse") + .to_return(body: JSON.generate(id: "nested_id", object: "nested", foo: "bar")) + nested_resource = MainResource.retrieve_nested("id", "nested_id", { expand: ["reverse"] }, {}) + 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" }) diff --git a/test/stripe/transfer_test.rb b/test/stripe/transfer_test.rb index e3fb068fc..e8ec16590 100644 --- a/test/stripe/transfer_test.rb +++ b/test/stripe/transfer_test.rb @@ -84,5 +84,59 @@ class TransferTest < Test::Unit::TestCase assert reversals.data.is_a?(Array) end end + + should "retrieve a reversal with expand" do + reversal = Stripe::Transfer.retrieve_reversal( + "tr_123", + "trr_123", + { expand: %w[transfer] }, + {} + ) + 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: ["available"] }, + { stripe_account: "acct_123" }) + assert_requested :get, "#{Stripe.api_base}/v1/transfers/tr_123/reversals/trr_123?expand[]=available" do |req| + assert_equal("acct_123", req.headers["Stripe-Account"]) + true + end + assert transfer_reversal.is_a?(Stripe::Reversal) + end + should "be retrievable with params and an explicitly empty opts" do + transfer_reversal = Stripe::Transfer.retrieve_reversal( + "tr_123", + "trr_123", + { expand: ["available"] }, + {} + ) + assert_requested :get, "#{Stripe.api_base}/v1/transfers/tr_123/reversals/trr_123?expand[]=available" do |req| + assert_nil(req.headers["Stripe-Account"]) + true + 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