Skip to content

Return consistent result using '[]' and accessor methods of Stripe::StripeObject class #1338

Open

Description

@doom-weaver

Is your feature request related to a problem? Please describe.

Presently, using [] or an accessor on a StripeObject return different results:

 irb(main):005> require('stripe')
 => true
 irb(main):006> x = Stripe::StripeObject.new()
 => 
#<Stripe::StripeObject:0x13088> JSON: {
...
 irb(main):007> x.a
 /Users/cleve/.asdf/installs/ruby/3.1.4/lib/ruby/gems/3.1.0/gems/stripe-5.55.0/lib/stripe/stripe_object.rb:399:in `method_missing': undefined method `a' for #<Stripe::StripeObject:0x13088> JSON: { (NoMethodError)
}
        from (irb):7:in `<main>'
        from /Users/cleve/.asdf/installs/ruby/3.1.4/lib/ruby/gems/3.1.0/gems/irb-1.9.0/exe/irb:9:in `<top (required)>'
        from /Users/cleve/.asdf/installs/ruby/3.1.4/bin/irb:25:in `load'
        from /Users/cleve/.asdf/installs/ruby/3.1.4/bin/irb:25:in `<main>'
 irb(main):008> x["a"]
 => nil
 irb(main):009> 

Using the accessor raises a NoMethodError while the use of the [] method returns nil.

Describe the solution you'd like

Have both accessor patterns generate the same output for non-existent fields. This would be more consistent semantically.

Describe alternatives you've considered

It is easy to work around this using [] or the accessor depending on the context.

Additional context

In my case I am using the metadata field of stripe payment intent to control some operational metrics and backend processes. Hence - we need to check metadata fields to determine what action, if any, to take and what metrics to report. That is when this difference was encountered.

I could not find documentation for the fact that the accessor raises errors while the hash does not - short of updating the library documenting the difference in the Stripe Ruby docs would be a nice win.

Activity

  1. pakrym-stripe commented on Feb 21, 2024

    @pakrym-stripe
    Contributor

    I've documented the behavior in #1339. I'll keep the issue open to see if we get other reports of inconsistency being confusing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions