Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add methods for dynamically referenced constants #1327

Merged
merged 6 commits into from
Feb 10, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
7 changes: 6 additions & 1 deletion lib/stripe/api_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ class APIResource < StripeObject
# for example, where this is allowed.
attr_accessor :save_with_parent

OBJECT_NAME = ""
def self.object_name
""
end

def self.class_name
name.split("::")[-1]
end
Expand All @@ -23,7 +28,7 @@ def self.resource_url
end
# Namespaces are separated in object names with periods (.) and in URLs
# with forward slashes (/), so replace the former with the latter.
"/v1/#{self::OBJECT_NAME.downcase.tr('.', '/')}s"
"/v1/#{object_name.downcase.tr('.', '/')}s"
end

# A metaprogramming call that specifies that a field of a resource can be
Expand Down
8 changes: 6 additions & 2 deletions lib/stripe/api_resource_test_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ def initialize(resource)
@resource = resource
end

def self.resource_class
nil
end

# Adds a custom method to a test helper. This is used to add support for
# non-CRUDL API requests, e.g. capturing charges. custom_method takes the
# following parameters:
Expand All @@ -27,12 +31,12 @@ def initialize(resource)
# adds a `capture` class method to the resource class that, when called,
# will send a POST request to `/v1/<object_name>/capture`.
def self.custom_method(name, http_verb:, http_path: nil)
Util.custom_method self::RESOURCE_CLASS, self, name, http_verb, http_path
Util.custom_method resource_class, self, name, http_verb, http_path
end

def self.resource_url
"/v1/test_helpers/" \
"#{self::RESOURCE_CLASS::OBJECT_NAME.downcase.tr('.', '/')}s"
"#{resource_class.object_name.downcase.tr('.', '/')}s"
end

def resource_url
Expand Down
3 changes: 3 additions & 0 deletions lib/stripe/list_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ class ListObject < StripeObject
include Stripe::APIOperations::Create

OBJECT_NAME = "list"
def self.object_name
"list"
end

# This accessor allows a `ListObject` to inherit various filters that were
# given to a predecessor. This allows for things like consistent limits,
Expand Down
238 changes: 120 additions & 118 deletions lib/stripe/object_types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,136 +2,138 @@
# frozen_string_literal: true

# rubocop:disable Metrics/MethodLength
# rubocop:disable Metrics/AbcSize

module Stripe
module ObjectTypes
def self.object_names_to_classes
{
# data structures
ListObject::OBJECT_NAME => ListObject,
SearchResultObject::OBJECT_NAME => SearchResultObject,
ListObject.object_name => ListObject,
SearchResultObject.object_name => SearchResultObject,

# business objects
File::OBJECT_NAME_ALT => File,
Account::OBJECT_NAME => Account,
AccountLink::OBJECT_NAME => AccountLink,
AccountSession::OBJECT_NAME => AccountSession,
ApplePayDomain::OBJECT_NAME => ApplePayDomain,
ApplicationFee::OBJECT_NAME => ApplicationFee,
ApplicationFeeRefund::OBJECT_NAME => ApplicationFeeRefund,
Apps::Secret::OBJECT_NAME => Apps::Secret,
Balance::OBJECT_NAME => Balance,
BalanceTransaction::OBJECT_NAME => BalanceTransaction,
BankAccount::OBJECT_NAME => BankAccount,
BillingPortal::Configuration::OBJECT_NAME => BillingPortal::Configuration,
BillingPortal::Session::OBJECT_NAME => BillingPortal::Session,
Capability::OBJECT_NAME => Capability,
Card::OBJECT_NAME => Card,
CashBalance::OBJECT_NAME => CashBalance,
Charge::OBJECT_NAME => Charge,
Checkout::Session::OBJECT_NAME => Checkout::Session,
Climate::Order::OBJECT_NAME => Climate::Order,
Climate::Product::OBJECT_NAME => Climate::Product,
Climate::Supplier::OBJECT_NAME => Climate::Supplier,
CountrySpec::OBJECT_NAME => CountrySpec,
Coupon::OBJECT_NAME => Coupon,
CreditNote::OBJECT_NAME => CreditNote,
CreditNoteLineItem::OBJECT_NAME => CreditNoteLineItem,
Customer::OBJECT_NAME => Customer,
CustomerBalanceTransaction::OBJECT_NAME => CustomerBalanceTransaction,
CustomerCashBalanceTransaction::OBJECT_NAME => CustomerCashBalanceTransaction,
CustomerSession::OBJECT_NAME => CustomerSession,
Discount::OBJECT_NAME => Discount,
Dispute::OBJECT_NAME => Dispute,
EphemeralKey::OBJECT_NAME => EphemeralKey,
Event::OBJECT_NAME => Event,
ExchangeRate::OBJECT_NAME => ExchangeRate,
File::OBJECT_NAME => File,
FileLink::OBJECT_NAME => FileLink,
FinancialConnections::Account::OBJECT_NAME => FinancialConnections::Account,
FinancialConnections::AccountOwner::OBJECT_NAME => FinancialConnections::AccountOwner,
FinancialConnections::AccountOwnership::OBJECT_NAME =>
File.object_name_alt => File,
Account.object_name => Account,
AccountLink.object_name => AccountLink,
AccountSession.object_name => AccountSession,
ApplePayDomain.object_name => ApplePayDomain,
ApplicationFee.object_name => ApplicationFee,
ApplicationFeeRefund.object_name => ApplicationFeeRefund,
Apps::Secret.object_name => Apps::Secret,
Balance.object_name => Balance,
BalanceTransaction.object_name => BalanceTransaction,
BankAccount.object_name => BankAccount,
BillingPortal::Configuration.object_name => BillingPortal::Configuration,
BillingPortal::Session.object_name => BillingPortal::Session,
Capability.object_name => Capability,
Card.object_name => Card,
CashBalance.object_name => CashBalance,
Charge.object_name => Charge,
Checkout::Session.object_name => Checkout::Session,
Climate::Order.object_name => Climate::Order,
Climate::Product.object_name => Climate::Product,
Climate::Supplier.object_name => Climate::Supplier,
CountrySpec.object_name => CountrySpec,
Coupon.object_name => Coupon,
CreditNote.object_name => CreditNote,
CreditNoteLineItem.object_name => CreditNoteLineItem,
Customer.object_name => Customer,
CustomerBalanceTransaction.object_name => CustomerBalanceTransaction,
CustomerCashBalanceTransaction.object_name => CustomerCashBalanceTransaction,
CustomerSession.object_name => CustomerSession,
Discount.object_name => Discount,
Dispute.object_name => Dispute,
EphemeralKey.object_name => EphemeralKey,
Event.object_name => Event,
ExchangeRate.object_name => ExchangeRate,
File.object_name => File,
FileLink.object_name => FileLink,
FinancialConnections::Account.object_name => FinancialConnections::Account,
FinancialConnections::AccountOwner.object_name => FinancialConnections::AccountOwner,
FinancialConnections::AccountOwnership.object_name =>
FinancialConnections::AccountOwnership,
FinancialConnections::Session::OBJECT_NAME => FinancialConnections::Session,
FinancialConnections::Transaction::OBJECT_NAME => FinancialConnections::Transaction,
FundingInstructions::OBJECT_NAME => FundingInstructions,
Identity::VerificationReport::OBJECT_NAME => Identity::VerificationReport,
Identity::VerificationSession::OBJECT_NAME => Identity::VerificationSession,
Invoice::OBJECT_NAME => Invoice,
InvoiceItem::OBJECT_NAME => InvoiceItem,
InvoiceLineItem::OBJECT_NAME => InvoiceLineItem,
Issuing::Authorization::OBJECT_NAME => Issuing::Authorization,
Issuing::Card::OBJECT_NAME => Issuing::Card,
Issuing::Cardholder::OBJECT_NAME => Issuing::Cardholder,
Issuing::Dispute::OBJECT_NAME => Issuing::Dispute,
Issuing::Token::OBJECT_NAME => Issuing::Token,
Issuing::Transaction::OBJECT_NAME => Issuing::Transaction,
LineItem::OBJECT_NAME => LineItem,
LoginLink::OBJECT_NAME => LoginLink,
Mandate::OBJECT_NAME => Mandate,
PaymentIntent::OBJECT_NAME => PaymentIntent,
PaymentLink::OBJECT_NAME => PaymentLink,
PaymentMethod::OBJECT_NAME => PaymentMethod,
PaymentMethodConfiguration::OBJECT_NAME => PaymentMethodConfiguration,
PaymentMethodDomain::OBJECT_NAME => PaymentMethodDomain,
Payout::OBJECT_NAME => Payout,
Person::OBJECT_NAME => Person,
Plan::OBJECT_NAME => Plan,
Price::OBJECT_NAME => Price,
Product::OBJECT_NAME => Product,
PromotionCode::OBJECT_NAME => PromotionCode,
Quote::OBJECT_NAME => Quote,
Radar::EarlyFraudWarning::OBJECT_NAME => Radar::EarlyFraudWarning,
Radar::ValueList::OBJECT_NAME => Radar::ValueList,
Radar::ValueListItem::OBJECT_NAME => Radar::ValueListItem,
Refund::OBJECT_NAME => Refund,
Reporting::ReportRun::OBJECT_NAME => Reporting::ReportRun,
Reporting::ReportType::OBJECT_NAME => Reporting::ReportType,
Reversal::OBJECT_NAME => Reversal,
Review::OBJECT_NAME => Review,
SetupAttempt::OBJECT_NAME => SetupAttempt,
SetupIntent::OBJECT_NAME => SetupIntent,
ShippingRate::OBJECT_NAME => ShippingRate,
Sigma::ScheduledQueryRun::OBJECT_NAME => Sigma::ScheduledQueryRun,
Source::OBJECT_NAME => Source,
SourceTransaction::OBJECT_NAME => SourceTransaction,
Subscription::OBJECT_NAME => Subscription,
SubscriptionItem::OBJECT_NAME => SubscriptionItem,
SubscriptionSchedule::OBJECT_NAME => SubscriptionSchedule,
Tax::Calculation::OBJECT_NAME => Tax::Calculation,
Tax::CalculationLineItem::OBJECT_NAME => Tax::CalculationLineItem,
Tax::Registration::OBJECT_NAME => Tax::Registration,
Tax::Settings::OBJECT_NAME => Tax::Settings,
Tax::Transaction::OBJECT_NAME => Tax::Transaction,
Tax::TransactionLineItem::OBJECT_NAME => Tax::TransactionLineItem,
TaxCode::OBJECT_NAME => TaxCode,
TaxId::OBJECT_NAME => TaxId,
TaxRate::OBJECT_NAME => TaxRate,
Terminal::Configuration::OBJECT_NAME => Terminal::Configuration,
Terminal::ConnectionToken::OBJECT_NAME => Terminal::ConnectionToken,
Terminal::Location::OBJECT_NAME => Terminal::Location,
Terminal::Reader::OBJECT_NAME => Terminal::Reader,
TestHelpers::TestClock::OBJECT_NAME => TestHelpers::TestClock,
Token::OBJECT_NAME => Token,
Topup::OBJECT_NAME => Topup,
Transfer::OBJECT_NAME => Transfer,
Treasury::CreditReversal::OBJECT_NAME => Treasury::CreditReversal,
Treasury::DebitReversal::OBJECT_NAME => Treasury::DebitReversal,
Treasury::FinancialAccount::OBJECT_NAME => Treasury::FinancialAccount,
Treasury::FinancialAccountFeatures::OBJECT_NAME => Treasury::FinancialAccountFeatures,
Treasury::InboundTransfer::OBJECT_NAME => Treasury::InboundTransfer,
Treasury::OutboundPayment::OBJECT_NAME => Treasury::OutboundPayment,
Treasury::OutboundTransfer::OBJECT_NAME => Treasury::OutboundTransfer,
Treasury::ReceivedCredit::OBJECT_NAME => Treasury::ReceivedCredit,
Treasury::ReceivedDebit::OBJECT_NAME => Treasury::ReceivedDebit,
Treasury::Transaction::OBJECT_NAME => Treasury::Transaction,
Treasury::TransactionEntry::OBJECT_NAME => Treasury::TransactionEntry,
UsageRecord::OBJECT_NAME => UsageRecord,
UsageRecordSummary::OBJECT_NAME => UsageRecordSummary,
WebhookEndpoint::OBJECT_NAME => WebhookEndpoint,
FinancialConnections::Session.object_name => FinancialConnections::Session,
FinancialConnections::Transaction.object_name => FinancialConnections::Transaction,
FundingInstructions.object_name => FundingInstructions,
Identity::VerificationReport.object_name => Identity::VerificationReport,
Identity::VerificationSession.object_name => Identity::VerificationSession,
Invoice.object_name => Invoice,
InvoiceItem.object_name => InvoiceItem,
InvoiceLineItem.object_name => InvoiceLineItem,
Issuing::Authorization.object_name => Issuing::Authorization,
Issuing::Card.object_name => Issuing::Card,
Issuing::Cardholder.object_name => Issuing::Cardholder,
Issuing::Dispute.object_name => Issuing::Dispute,
Issuing::Token.object_name => Issuing::Token,
Issuing::Transaction.object_name => Issuing::Transaction,
LineItem.object_name => LineItem,
LoginLink.object_name => LoginLink,
Mandate.object_name => Mandate,
PaymentIntent.object_name => PaymentIntent,
PaymentLink.object_name => PaymentLink,
PaymentMethod.object_name => PaymentMethod,
PaymentMethodConfiguration.object_name => PaymentMethodConfiguration,
PaymentMethodDomain.object_name => PaymentMethodDomain,
Payout.object_name => Payout,
Person.object_name => Person,
Plan.object_name => Plan,
Price.object_name => Price,
Product.object_name => Product,
PromotionCode.object_name => PromotionCode,
Quote.object_name => Quote,
Radar::EarlyFraudWarning.object_name => Radar::EarlyFraudWarning,
Radar::ValueList.object_name => Radar::ValueList,
Radar::ValueListItem.object_name => Radar::ValueListItem,
Refund.object_name => Refund,
Reporting::ReportRun.object_name => Reporting::ReportRun,
Reporting::ReportType.object_name => Reporting::ReportType,
Reversal.object_name => Reversal,
Review.object_name => Review,
SetupAttempt.object_name => SetupAttempt,
SetupIntent.object_name => SetupIntent,
ShippingRate.object_name => ShippingRate,
Sigma::ScheduledQueryRun.object_name => Sigma::ScheduledQueryRun,
Source.object_name => Source,
SourceTransaction.object_name => SourceTransaction,
Subscription.object_name => Subscription,
SubscriptionItem.object_name => SubscriptionItem,
SubscriptionSchedule.object_name => SubscriptionSchedule,
Tax::Calculation.object_name => Tax::Calculation,
Tax::CalculationLineItem.object_name => Tax::CalculationLineItem,
Tax::Registration.object_name => Tax::Registration,
Tax::Settings.object_name => Tax::Settings,
Tax::Transaction.object_name => Tax::Transaction,
Tax::TransactionLineItem.object_name => Tax::TransactionLineItem,
TaxCode.object_name => TaxCode,
TaxId.object_name => TaxId,
TaxRate.object_name => TaxRate,
Terminal::Configuration.object_name => Terminal::Configuration,
Terminal::ConnectionToken.object_name => Terminal::ConnectionToken,
Terminal::Location.object_name => Terminal::Location,
Terminal::Reader.object_name => Terminal::Reader,
TestHelpers::TestClock.object_name => TestHelpers::TestClock,
Token.object_name => Token,
Topup.object_name => Topup,
Transfer.object_name => Transfer,
Treasury::CreditReversal.object_name => Treasury::CreditReversal,
Treasury::DebitReversal.object_name => Treasury::DebitReversal,
Treasury::FinancialAccount.object_name => Treasury::FinancialAccount,
Treasury::FinancialAccountFeatures.object_name => Treasury::FinancialAccountFeatures,
Treasury::InboundTransfer.object_name => Treasury::InboundTransfer,
Treasury::OutboundPayment.object_name => Treasury::OutboundPayment,
Treasury::OutboundTransfer.object_name => Treasury::OutboundTransfer,
Treasury::ReceivedCredit.object_name => Treasury::ReceivedCredit,
Treasury::ReceivedDebit.object_name => Treasury::ReceivedDebit,
Treasury::Transaction.object_name => Treasury::Transaction,
Treasury::TransactionEntry.object_name => Treasury::TransactionEntry,
UsageRecord.object_name => UsageRecord,
UsageRecordSummary.object_name => UsageRecordSummary,
WebhookEndpoint.object_name => WebhookEndpoint,
}
end
end
end

# rubocop:enable Metrics/AbcSize
# rubocop:enable Metrics/MethodLength
3 changes: 3 additions & 0 deletions lib/stripe/resources/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ class Account < APIResource
extend Stripe::APIOperations::NestedResource

OBJECT_NAME = "account"
def self.object_name
"account"
end

nested_resource_class_methods :capability,
operations: %i[retrieve update list],
Expand Down
3 changes: 3 additions & 0 deletions lib/stripe/resources/account_link.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ class AccountLink < APIResource
extend Stripe::APIOperations::Create

OBJECT_NAME = "account_link"
def self.object_name
"account_link"
end

# Creates an AccountLink object that includes a single-use Stripe URL that the platform can redirect their user to in order to take them through the Connect Onboarding flow.
def self.create(params = {}, opts = {})
Expand Down
3 changes: 3 additions & 0 deletions lib/stripe/resources/account_session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ class AccountSession < APIResource
extend Stripe::APIOperations::Create

OBJECT_NAME = "account_session"
def self.object_name
"account_session"
end

# Creates a AccountSession object that includes a single-use token that the platform can use on their front-end to grant client-side API access.
def self.create(params = {}, opts = {})
Expand Down
3 changes: 3 additions & 0 deletions lib/stripe/resources/apple_pay_domain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ class ApplePayDomain < APIResource
extend Stripe::APIOperations::List

OBJECT_NAME = "apple_pay_domain"
def self.object_name
"apple_pay_domain"
end

def self.resource_url
"/v1/apple_pay/domains"
Expand Down
3 changes: 3 additions & 0 deletions lib/stripe/resources/application_fee.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ class ApplicationFee < APIResource
extend Stripe::APIOperations::NestedResource

OBJECT_NAME = "application_fee"
def self.object_name
"application_fee"
end

nested_resource_class_methods :refund, operations: %i[create retrieve update list]

Expand Down
3 changes: 3 additions & 0 deletions lib/stripe/resources/application_fee_refund.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ class ApplicationFeeRefund < APIResource
include Stripe::APIOperations::Save

OBJECT_NAME = "fee_refund"
def self.object_name
"fee_refund"
end

def resource_url
"#{ApplicationFee.resource_url}/#{CGI.escape(fee)}/refunds" \
Expand Down
3 changes: 3 additions & 0 deletions lib/stripe/resources/apps/secret.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ class Secret < APIResource
extend Stripe::APIOperations::List

OBJECT_NAME = "apps.secret"
def self.object_name
"apps.secret"
end

# Deletes a secret from the secret store by name and scope.
def self.delete_where(params = {}, opts = {})
Expand Down
3 changes: 3 additions & 0 deletions lib/stripe/resources/balance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@ module Stripe
# Related guide: [Understanding Connect account balances](https://stripe.com/docs/connect/account-balances)
class Balance < SingletonAPIResource
OBJECT_NAME = "balance"
def self.object_name
"balance"
end
end
end
Loading
Loading