Fail resource-based methods for next resources (#1453)

* Fail resource based methods on v2 resources

* remove extra line

* rubocop
This commit is contained in:
helenye-stripe 2024-09-25 13:26:51 -07:00 committed by GitHub
parent 248f214f86
commit dc84ce7d41
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 45 additions and 2 deletions

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require "stripe"
require "date"

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require "stripe"
require "date"
@ -17,7 +19,6 @@ end
# Send meter events
api_key = "{{API_KEY}}"
customer_id = "{{CUSTOMER_ID}}"
example = NewExample.new(api_key)
example.do_something_great

View File

@ -25,6 +25,12 @@ module Stripe
end
def self.resource_url
if name.include?("Stripe::V2")
raise NotImplementedError,
"V2 resources do not have a defined URL. Please use the StripeClient " \
"to make V2 requests"
end
if self == APIResource
raise NotImplementedError,
"APIResource is an abstract class. You should perform actions " \
@ -86,12 +92,21 @@ module Stripe
"#{self.class.resource_url}/#{CGI.escape(id)}"
end
# TODO: v2 objects can be refreshed -- the api mode should depend on the object here.
def refresh
if self.class.name.include?("Stripe::V2")
raise NotImplementedError,
"It is not possible to refresh v2 objects. Please retrieve the object using the StripeClient instead."
end
@requestor.execute_request_initialize_from(:get, resource_url, :api, self, params: @retrieve_params)
end
def self.retrieve(id, opts = {})
if name.include?("Stripe::V2")
raise NotImplementedError,
"It is not possible to retrieve v2 objects on the resource. Please use the StripeClient instead."
end
opts = Util.normalize_opts(opts)
instance = new(id, opts)
instance.refresh

View File

@ -894,6 +894,31 @@ module Stripe
end
end
context "v2 resources" do
should "raise an NotImplementedError on resource_url" do
assert_raises NotImplementedError do
Stripe::V2::Event.resource_url
end
end
should "raise an NotImplementedError on retrieve" do
assert_raises NotImplementedError do
Stripe::V2::Event.retrieve("acct_123")
end
end
should "raise an NotImplementedError on refresh" do
stub_request(:post, "#{Stripe::DEFAULT_API_BASE}/v2/billing/meter_event_session")
.to_return(body: JSON.generate(object: "billing.meter_event_session"))
client = Stripe::StripeClient.new("sk_test_123")
session = client.v2.billing.meter_event_session.create
assert_raises NotImplementedError do
session.refresh
end
end
end
@@fixtures = {} # rubocop:disable Style/ClassVars
setup do
if @@fixtures.empty?