Module: Stripe::APIOperations::NestedResource

Included in:
Stripe::Account, Stripe::ApplicationFee, Billing::Meter, Charge, Customer, Invoice, Product, Source, SubscriptionItem, Transfer
Defined in:
lib/stripe/api_operations/nested_resource.rb

Overview

Adds methods to help manipulate a subresource from its parent resource so that it’s possible to do so from a static context (i.e. without a pre-existing collection of subresources on the parent).

For example, a transfer gains the static methods for reversals so that the methods ‘.create_reversal`, `.retrieve_reversal`, `.update_reversal`, etc. all become available.

Instance Method Summary collapse

Instance Method Details

#nested_resource_class_methods(resource, path: nil, operations: nil, resource_plural: nil) ⇒ Object

Raises:

  • (ArgumentError)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/stripe/api_operations/nested_resource.rb', line 13

def nested_resource_class_methods(resource, path: nil, operations: nil,
                                  resource_plural: nil)
  resource_plural ||= "#{resource}s"
  path ||= resource_plural

  raise ArgumentError, "operations array required" if operations.nil?

  resource_url_method = :"#{resource}s_url"

  define_singleton_method(resource_url_method) do |id, nested_id = nil|
    url = "#{resource_url}/#{CGI.escape(id)}/#{CGI.escape(path)}"
    url += "/#{CGI.escape(nested_id)}" unless nested_id.nil?
    url
  end

  operations.each do |operation|
    define_operation(
      resource,
      operation,
      resource_url_method,
      resource_plural
    )
  end
end