Sha256: f28451289594e2ee0464da5a667c087baf8f547647d95acb5759fa706db7e492

Contents?: true

Size: 1.71 KB

Versions: 17

Compression:

Stored size: 1.71 KB

Contents

# frozen_string_literal: true

module Stripe
  # The base class for nested TestHelpers classes in resource objects.
  # The APIResourceTestHelpers handles URL generation and custom method
  # support for test-helper methods.
  #
  #  class MyAPIResource < APIResource
  #    class TestHelpers < APIResourceTestHelpers
  class APIResourceTestHelpers
    include Stripe::APIOperations::Request

    def initialize(resource)
      @resource = resource
    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:
    # - name: the name of the custom method to create (as a symbol)
    # - http_verb: the HTTP verb for the API request (:get, :post, or :delete)
    # - http_path: the path to append to the resource's URL. If not provided,
    #              the name is used as the path
    #
    # For example, this call:
    #     custom_method :capture, http_verb: post
    # 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
    end

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

    def resource_url
      unless (id = @resource["id"])
        raise InvalidRequestError.new(
          "Could not determine which URL to request: #{self.class} instance " \
          "has invalid ID: #{id.inspect}",
          "id"
        )
      end
      "#{self.class.resource_url}/#{CGI.escape(id)}"
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
stripe-10.9.0.pre.beta.1 lib/stripe/api_resource_test_helpers.rb
stripe-10.8.0 lib/stripe/api_resource_test_helpers.rb
stripe-10.7.1 lib/stripe/api_resource_test_helpers.rb
stripe-10.8.0.pre.beta.1 lib/stripe/api_resource_test_helpers.rb
stripe-10.7.0 lib/stripe/api_resource_test_helpers.rb
stripe-10.7.0.pre.beta.3 lib/stripe/api_resource_test_helpers.rb
stripe-10.7.0.pre.beta.2 lib/stripe/api_resource_test_helpers.rb
stripe-10.7.0.pre.beta.1 lib/stripe/api_resource_test_helpers.rb
stripe-10.6.0 lib/stripe/api_resource_test_helpers.rb
stripe-10.6.0.pre.beta.1 lib/stripe/api_resource_test_helpers.rb
stripe-10.5.0 lib/stripe/api_resource_test_helpers.rb
stripe-10.5.0.pre.beta.1 lib/stripe/api_resource_test_helpers.rb
stripe-10.4.0 lib/stripe/api_resource_test_helpers.rb
stripe-10.4.0.pre.beta.1 lib/stripe/api_resource_test_helpers.rb
stripe-10.3.0 lib/stripe/api_resource_test_helpers.rb
stripe-10.3.0.pre.beta.1 lib/stripe/api_resource_test_helpers.rb
stripe-10.2.0 lib/stripe/api_resource_test_helpers.rb