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

Add API resource instance methods to StripeClient #954

Closed
wants to merge 3 commits into from

Commits on Apr 6, 2021

  1. Add API resource instance methods to StripeClient

    This change introduces convenience methods to access API resources
    through `StripeClient` for per-client configuration. The instance client
    can be configured with the same global Stripe configurations. As a
    result, an instance of `StripeClient` is able to override or fallback
    to the global configuration that was present at the time of
    initialization.
    
    Here's an example:
    
    ```ruby
    Stripe::Customer.list() == StripeClient.new.customer.list()
    ```
    
    The primary workhorse for this feature is a new module called
    `Stripe::ClientAPIOperations` that defines instance methods on
    `StripeClient` when it is included. A `ClientProxy` is used to send any
    method calls to an API resource with the instantiated client injected.
    There are a few noteworthy aspects of this approach:
    
    - Many resources are namespaced, which introduces a unique challenge
      when it comes to method chaining calls (e.g.
      client.issuing.authorizations).  In order to handle those cases, we
      create a `ClientProxy` object for the root namespace (e.g., "issuing")
      and define all resource methods (e.g. "authorizations") at once to
      avoid re-defining the proxy object when there are multiple resources
      per namespace.
    
    - Sigma deviates from other namespaced API resources and does not have
      an `OBJECT_NAME` separated by a period. We account for that nuance
      directly.
    
    - `method_missing` is substantially slower than direct calls. Therefore,
      methods are defined where possible but `method_missing` is still used
      at the last step when delegating resource methods to the actual
      resource.
    
    - Each API resource spec was converted to use instance based
      methods and was done to ensure adequate test coverage. Since this
      entire feature is built on proxying methods, testing via the client
      implicitly tests the original implementation for "free".
    joeltaylor committed Apr 6, 2021
    Configuration menu
    Copy the full SHA
    e3d8db4 View commit details
    Browse the repository at this point in the history
  2. Configuration menu
    Copy the full SHA
    e5ed86c View commit details
    Browse the repository at this point in the history
  3. Add nil check for Ruby 2.3

    It's possible for `filters` to be `nil`, which causes Ruby 2.3 to raise
    an error: `TypeError: can't dup NilClass`
    joeltaylor committed Apr 6, 2021
    Configuration menu
    Copy the full SHA
    de9c3de View commit details
    Browse the repository at this point in the history