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

Implement object_name_to_module so it will work for new objects in future #792

Merged
merged 1 commit into from
Nov 28, 2023
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
48 changes: 10 additions & 38 deletions lib/stripe/util.ex
Original file line number Diff line number Diff line change
Expand Up @@ -51,47 +51,19 @@ defmodule Stripe.Util do
def atomize_key(k), do: k

@spec object_name_to_module(String.t()) :: module
def object_name_to_module("billing_portal.configuration"),
do: Stripe.BillingPortal.Configuration

def object_name_to_module("billing_portal.session"), do: Stripe.BillingPortal.Session
def object_name_to_module("checkout.session"), do: Stripe.Checkout.Session
def object_name_to_module("radar.early_fraud_warning"), do: Stripe.Radar.EarlyFraudWarning
def object_name_to_module("file"), do: Stripe.File

def object_name_to_module("identity.verification_session"),
do: Stripe.Identity.VerificationSession

def object_name_to_module("identity.verification_report"),
do: Stripe.Identity.VerificationReport

def object_name_to_module("reporting.report_type"),
do: Stripe.Reporting.ReportType

def object_name_to_module("reporting.report_run"),
do: Stripe.Reporting.ReportRun

def object_name_to_module("issuing.authorization"), do: Stripe.Issuing.Authorization
def object_name_to_module("issuing.card"), do: Stripe.Issuing.Card
def object_name_to_module("issuing.cardholder"), do: Stripe.Issuing.Cardholder
def object_name_to_module("issuing.transaction"), do: Stripe.Issuing.Transaction
def object_name_to_module("tax_id"), do: Stripe.TaxId
def object_name_to_module("usage_record"), do: Stripe.UsageRecord
def object_name_to_module("terminal.connection_token"), do: Stripe.Terminal.ConnectionToken
def object_name_to_module("terminal.location"), do: Stripe.Terminal.Location
def object_name_to_module("terminal.reader"), do: Stripe.Terminal.Reader
def object_name_to_module("test_helpers.test_clock"), do: Stripe.TestHelpers.TestClock

def object_name_to_module("usage_record_summary"),
do: Stripe.UsageRecordSummary
def object_name_to_module("test_helpers.test_clock"), do: Stripe.TestClock
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi! I believe there is a mistake made here and this should be Stripe.TestHelpers.TestClock. It is currently breaking api requests to retrieve test clocks. I've filed a bug report #825


def object_name_to_module(object_name) do
module_name =
module_parts =
object_name
|> String.split("_")
|> Enum.map_join("", &String.capitalize/1)

Module.concat(Stripe, module_name)
|> String.split(".")
|> Enum.map(fn part ->
part
|> String.split("_")
|> Enum.map_join("", &String.capitalize/1)
end)

Module.concat([Stripe | module_parts])
end

@spec module_to_string(module) :: String.t()
Expand Down
18 changes: 18 additions & 0 deletions test/stripe/util_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,24 @@ defmodule Stripe.UtilTest do
assert object_name_to_module("transfer") == Stripe.Transfer
assert object_name_to_module("transfer_reversal") == Stripe.TransferReversal
assert object_name_to_module("token") == Stripe.Token

assert object_name_to_module("billing_portal.session") == Stripe.BillingPortal.Session
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These become a bit extra, but I figure that I'd add manual tests for any of the handwritten functions I removed

assert object_name_to_module("checkout.session") == Stripe.Checkout.Session
assert object_name_to_module("identity.verification_report") == Stripe.Identity.VerificationReport
assert object_name_to_module("identity.verification_session") == Stripe.Identity.VerificationSession
assert object_name_to_module("issuing.authorization") == Stripe.Issuing.Authorization
assert object_name_to_module("issuing.card") == Stripe.Issuing.Card
assert object_name_to_module("issuing.cardholder") == Stripe.Issuing.Cardholder
assert object_name_to_module("issuing.transaction") == Stripe.Issuing.Transaction
assert object_name_to_module("radar.early_fraud_warning") == Stripe.Radar.EarlyFraudWarning
assert object_name_to_module("reporting.report_type") == Stripe.Reporting.ReportType
assert object_name_to_module("reporting.report_run") == Stripe.Reporting.ReportRun
assert object_name_to_module("tax_id") == Stripe.TaxId
assert object_name_to_module("terminal.reader") == Stripe.Terminal.Reader
assert object_name_to_module("terminal.location") == Stripe.Terminal.Location
assert object_name_to_module("terminal.connection_token") == Stripe.Terminal.ConnectionToken
assert object_name_to_module("usage_record") == Stripe.UsageRecord
assert object_name_to_module("usage_record_summary") == Stripe.UsageRecordSummary
end
end

Expand Down
Loading