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

Better error message when passing non-string to custom method #810

Merged
merged 1 commit into from
Jul 16, 2019
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
5 changes: 5 additions & 0 deletions lib/stripe/api_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ def self.custom_method(name, http_verb:, http_path: nil)
end
http_path ||= name.to_s
define_singleton_method(name) do |id, params = {}, opts = {}|
unless id.is_a?(String)
raise ArgumentError,
"id should be a string representing the ID of an API resource"
end

url = "#{resource_url}/#{CGI.escape(id)}/#{CGI.escape(http_path)}"
resp, opts = request(http_verb, url, params, opts)
Util.convert_to_stripe_object(resp.data, opts)
Expand Down
21 changes: 21 additions & 0 deletions test/stripe/api_resource_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,31 @@

module Stripe
class ApiResourceTest < Test::Unit::TestCase
class CustomMethodAPIResource < APIResource
OBJECT_NAME = "custom_method".freeze
custom_method :my_method, http_verb: :post
end

class NestedTestAPIResource < APIResource
save_nested_resource :external_account
end

context ".custom_method" do
should "call to an RPC-style method" do
stub_request(:post, "#{Stripe.api_base}/v1/custom_methods/ch_123/my_method")
.to_return(body: JSON.generate({}))
CustomMethodAPIResource.my_method("ch_123")
end

should "raise an error if a non-ID is passed" do
e = assert_raises ArgumentError do
CustomMethodAPIResource.my_method(id: "ch_123")
end
assert_equal "id should be a string representing the ID of an API resource",
e.message
end
end

context ".save_nested_resource" do
should "can have a scalar set" do
r = NestedTestAPIResource.new("test_resource")
Expand Down