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

detach method for detaching sources from customers #589

Merged
merged 1 commit into from
Oct 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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