Skip to content

Commit

Permalink
Merge pull request #589 from stripe/ob-detach-sources
Browse files Browse the repository at this point in the history
detach method for detaching sources from customers
  • Loading branch information
brandur-stripe authored Oct 11, 2017
2 parents 6bd1d6b + 92e2166 commit a0a9c7c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
13 changes: 9 additions & 4 deletions lib/stripe/source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,24 @@ class Source < APIResource

OBJECT_NAME = "source".freeze

def delete(params = {}, opts = {})
def detach(params = {}, opts = {})
if !respond_to?(:customer) || customer.nil? || customer.empty?
raise NotImplementedError,
"Source objects cannot be deleted, they can only be detached " \
"from customer objects. This source object does not appear to " \
"be currently attached to a customer object."
"This source object does not appear to be currently attached " \
"to a customer object."
end

url = "#{Customer.resource_url}/#{CGI.escape(customer)}/sources/#{CGI.escape(id)}"
resp, opts = request(:delete, url, params, Util.normalize_opts(opts))
initialize_from(resp.data, opts)
end

def delete(params = {}, opts = {})
detach(params, opts)
end
extend Gem::Deprecate
deprecate :delete, "#detach", 2017, 10

def verify(params = {}, opts = {})
resp, opts = request(:post, resource_url + "/verify", params, Util.normalize_opts(opts))
initialize_from(resp.data, opts)
Expand Down
24 changes: 21 additions & 3 deletions test/stripe/source_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,43 @@ class SourceTest < Test::Unit::TestCase
assert source.is_a?(Stripe::Source)
end

context "#delete" do
context "#detach" do
should "not be deletable when unattached" do
source = Stripe::Source.retrieve("src_123")

assert_raises NotImplementedError do
source.delete
source.detach
end
end

should "be deletable when attached to a customer" do
source = Stripe::Source.construct_from(customer: "cus_123",
id: "src_123",
object: "source")
source = source.delete
source = source.detach
assert_requested :delete, "#{Stripe.api_base}/v1/customers/cus_123/sources/src_123"
assert source.is_a?(Stripe::Source)
end
end

context "#delete" do
should "warn that #delete is deprecated" do
old_stderr = $stderr
$stderr = StringIO.new
begin
source = Stripe::Source.construct_from(customer: "cus_123",
id: "src_123",
object: "source")
source.delete
message = "NOTE: Stripe::Source#delete is " \
"deprecated; use #detach instead"
assert_match Regexp.new(message), $stderr.string
ensure
$stderr = old_stderr
end
end
end

should "not be listable" do
assert_raises NoMethodError do
Stripe::Source.list
Expand Down

0 comments on commit a0a9c7c

Please sign in to comment.