Skip to content

Commit

Permalink
Add support for the Capability resource and APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
remi-stripe authored and ob-stripe committed May 14, 2019
1 parent 793a61c commit ff34036
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ sudo: false
env:
global:
# If changing this number, please also change it in `test/test_helper.rb`.
- STRIPE_MOCK_VERSION=0.54.0
- STRIPE_MOCK_VERSION=0.56.0

cache:
directories:
Expand Down
1 change: 1 addition & 0 deletions lib/stripe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
require "stripe/bank_account"
require "stripe/bitcoin_receiver"
require "stripe/bitcoin_transaction"
require "stripe/capability"
require "stripe/card"
require "stripe/charge"
require "stripe/checkout/session"
Expand Down
6 changes: 6 additions & 0 deletions lib/stripe/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ class Account < APIResource
custom_method :reject, http_verb: :post

save_nested_resource :external_account
nested_resource_class_methods :capability,
operations: %i[retrieve update list],
resource_plural: "capabilities"
nested_resource_class_methods :external_account,
operations: %i[create retrieve update delete list]
nested_resource_class_methods :login_link, operations: %i[create]
Expand Down Expand Up @@ -52,6 +55,9 @@ def persons(params = {}, opts = {})
Util.convert_to_stripe_object(resp.data, opts)
end

# We are not adding a helper for capabilities here as the Account object already has a
# capabilities property which is a hash and not the sub-list of capabilities.

def reject(params = {}, opts = {})
resp, opts = request(:post, resource_url + "/reject", params, opts)
initialize_from(resp.data, opts)
Expand Down
7 changes: 4 additions & 3 deletions lib/stripe/api_operations/nested_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ module APIOperations
# methods `.create_reversal`, `.retrieve_reversal`, `.update_reversal`,
# etc. all become available.
module NestedResource
def nested_resource_class_methods(resource, path: nil, operations: nil)
path ||= "#{resource}s"
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"
Expand Down Expand Up @@ -48,7 +49,7 @@ def nested_resource_class_methods(resource, path: nil, operations: nil)
Util.convert_to_stripe_object(resp.data, opts)
end
when :list
define_singleton_method(:"list_#{resource}s") do |id, params = {}, opts = {}|
define_singleton_method(:"list_#{resource_plural}") do |id, params = {}, opts = {}|
url = send(resource_url_method, id)
resp, opts = request(:get, url, params, opts)
Util.convert_to_stripe_object(resp.data, opts)
Expand Down
26 changes: 26 additions & 0 deletions lib/stripe/capability.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

module Stripe
class Capability < APIResource
extend Stripe::APIOperations::List
include Stripe::APIOperations::Save

OBJECT_NAME = "capability".freeze

def resource_url
if !respond_to?(:account) || account.nil?
raise NotImplementedError,
"Capabilities cannot be accessed without an account ID."
end
"#{Account.resource_url}/#{CGI.escape(account)}/capabilities/#{CGI.escape(id)}"
end

def self.retrieve(_id, _opts = {})
raise NotImplementedError, "Capabilities cannot be retrieved without an account ID. Retrieve a capability using account.retrieve_capability('acct_123', 'acap_123')"
end

def self.update(_id, _params = nil, _opts = nil)
raise NotImplementedError, "Capabilities cannot be updated without an account ID. Update a capability using `p = account.update_capability('acct_123', 'acap_123', params)"
end
end
end
1 change: 1 addition & 0 deletions lib/stripe/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def self.object_classes # rubocop:disable Metrics/MethodLength
BankAccount::OBJECT_NAME => BankAccount,
BitcoinReceiver::OBJECT_NAME => BitcoinReceiver,
BitcoinTransaction::OBJECT_NAME => BitcoinTransaction,
Capability::OBJECT_NAME => Capability,
Card::OBJECT_NAME => Card,
Charge::OBJECT_NAME => Charge,
Checkout::Session::OBJECT_NAME => Checkout::Session,
Expand Down
34 changes: 34 additions & 0 deletions test/stripe/account_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,40 @@ class AccountTest < Test::Unit::TestCase
end
end

context "#retrieve_capability" do
should "retrieve a capability" do
capability = Stripe::Account.retrieve_capability(
"acct_123",
"acap_123"
)
assert_requested :get, "#{Stripe.api_base}/v1/accounts/acct_123/capabilities/acap_123"
assert capability.is_a?(Stripe::Capability)
end
end

context "#update_capability" do
should "update a capability" do
capability = Stripe::Account.update_capability(
"acct_123",
"acap_123",
requested: true
)
assert_requested :post, "#{Stripe.api_base}/v1/accounts/acct_123/capabilities/acap_123"
assert capability.is_a?(Stripe::Capability)
end
end

context "#list_capabilities" do
should "list the account's external accounts" do
capabilities = Stripe::Account.list_capabilities(
"acct_123"
)
assert_requested :get, "#{Stripe.api_base}/v1/accounts/acct_123/capabilities"
assert capabilities.is_a?(Stripe::ListObject)
assert capabilities.data.is_a?(Array)
end
end

context "#create_external_account" do
should "create an external account" do
external_account = Stripe::Account.create_external_account(
Expand Down
45 changes: 45 additions & 0 deletions test/stripe/capability_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# frozen_string_literal: true

require ::File.expand_path("../../test_helper", __FILE__)

module Stripe
class CapabilityTest < Test::Unit::TestCase
context "#resource_url" do
should "return a resource URL" do
capability = Stripe::Capability.construct_from(
id: "acap_123",
account: "acct_123"
)
assert_equal "/v1/accounts/acct_123/capabilities/acap_123",
capability.resource_url
end

should "raise without an account" do
capability = Stripe::Capability.construct_from(id: "acap_123")
assert_raises NotImplementedError do
capability.resource_url
end
end
end

should "raise on #retrieve" do
assert_raises NotImplementedError do
Stripe::Capability.retrieve("acap_123")
end
end

should "raise on #update" do
assert_raises NotImplementedError do
Stripe::Capability.update("acap_123", {})
end
end

should "be saveable" do
capability = Stripe::Account.retrieve_capability("acct_123", "acap_123")
capability.requested = true
capability.save
assert_requested :post,
"#{Stripe.api_base}/v1/accounts/#{capability.account}/capabilities/#{capability.id}"
end
end
end
2 changes: 1 addition & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
require ::File.expand_path("../stripe_mock", __FILE__)

# If changing this number, please also change it in `.travis.yml`.
MOCK_MINIMUM_VERSION = "0.54.0".freeze
MOCK_MINIMUM_VERSION = "0.56.0".freeze
MOCK_PORT = Stripe::StripeMock.start

# Disable all real network connections except those that are outgoing to
Expand Down

0 comments on commit ff34036

Please sign in to comment.