Skip to content

Commit

Permalink
Static methods for delete (#752)
Browse files Browse the repository at this point in the history
New `.delete` class method on deletable API resources
  • Loading branch information
ob-stripe authored Apr 2, 2019
1 parent 160028a commit 0790bb4
Show file tree
Hide file tree
Showing 19 changed files with 303 additions and 89 deletions.
24 changes: 24 additions & 0 deletions lib/stripe/api_operations/delete.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,35 @@
module Stripe
module APIOperations
module Delete
module ClassMethods
# Deletes an API resource
#
# Deletes the identified resource with the passed in parameters.
#
# ==== Attributes
#
# * +id+ - ID of the resource to delete.
# * +params+ - A hash of parameters to pass to the API
# * +opts+ - A Hash of additional options (separate from the params /
# object values) to be added to the request. E.g. to allow for an
# idempotency_key to be passed in the request headers, or for the
# api_key to be overwritten. See {APIOperations::Request.request}.
def delete(id, params = {}, opts = {})
opts = Util.normalize_opts(opts)
resp, opts = request(:delete, "#{resource_url}/#{id}", params, opts)
Util.convert_to_stripe_object(resp.data, opts)
end
end

def delete(params = {}, opts = {})
opts = Util.normalize_opts(opts)
resp, opts = request(:delete, resource_url, params, opts)
initialize_from(resp.data, opts)
end

def self.included(base)
base.extend(ClassMethods)
end
end
end
end
20 changes: 15 additions & 5 deletions test/stripe/account_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,21 @@ class AccountTest < Test::Unit::TestCase
assert account.is_a?(Stripe::Account)
end

should "be deletable" do
account = Stripe::Account.retrieve("acct_123")
account = account.delete
assert_requested :delete, "#{Stripe.api_base}/v1/accounts/#{account.id}"
assert account.is_a?(Stripe::Account)
context "#delete" do
should "be deletable" do
account = Stripe::Account.retrieve("acct_123")
account = account.delete
assert_requested :delete, "#{Stripe.api_base}/v1/accounts/#{account.id}"
assert account.is_a?(Stripe::Account)
end
end

context ".delete" do
should "be deletable" do
account = Stripe::Account.delete("acct_123")
assert_requested :delete, "#{Stripe.api_base}/v1/accounts/acct_123"
assert account.is_a?(Stripe::Account)
end
end

should "be able to list Persons" do
Expand Down
21 changes: 17 additions & 4 deletions test/stripe/apple_pay_domain_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,23 @@ class ApplePayDomainTest < Test::Unit::TestCase
end

should "be deletable" do
domain = Stripe::ApplePayDomain.retrieve("apwc_123")
domain = domain.delete
assert_requested :delete, "#{Stripe.api_base}/v1/apple_pay/domains/#{domain.id}"
assert domain.is_a?(Stripe::ApplePayDomain)
end

context "#delete" do
should "be deletable" do
domain = Stripe::ApplePayDomain.retrieve("apwc_123")
domain = domain.delete
assert_requested :delete, "#{Stripe.api_base}/v1/apple_pay/domains/#{domain.id}"
assert domain.is_a?(Stripe::ApplePayDomain)
end
end

context ".delete" do
should "be deletable" do
domain = Stripe::ApplePayDomain.delete("apwc_123")
assert_requested :delete, "#{Stripe.api_base}/v1/apple_pay/domains/apwc_123"
assert domain.is_a?(Stripe::ApplePayDomain)
end
end
end
end
20 changes: 15 additions & 5 deletions test/stripe/coupon_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,21 @@ class CouponTest < Test::Unit::TestCase
assert coupon.is_a?(Stripe::Coupon)
end

should "be deletable" do
coupon = Stripe::Coupon.retrieve("25OFF")
coupon = coupon.delete
assert_requested :delete, "#{Stripe.api_base}/v1/coupons/#{coupon.id}"
assert coupon.is_a?(Stripe::Coupon)
context "#delete" do
should "be deletable" do
coupon = Stripe::Coupon.delete("25OFF")
assert_requested :delete, "#{Stripe.api_base}/v1/coupons/#{coupon.id}"
assert coupon.is_a?(Stripe::Coupon)
end
end

context ".delete" do
should "be deletable" do
coupon = Stripe::Coupon.retrieve("25OFF")
coupon = coupon.delete
assert_requested :delete, "#{Stripe.api_base}/v1/coupons/25OFF"
assert coupon.is_a?(Stripe::Coupon)
end
end
end
end
20 changes: 15 additions & 5 deletions test/stripe/customer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,21 @@ class CustomerTest < Test::Unit::TestCase
assert customer.is_a?(Stripe::Customer)
end

should "be deletable" do
customer = Stripe::Customer.retrieve("cus_123")
customer = customer.delete
assert_requested :delete, "#{Stripe.api_base}/v1/customers/#{customer.id}"
assert customer.is_a?(Stripe::Customer)
context "#delete" do
should "be deletable" do
customer = Stripe::Customer.retrieve("cus_123")
customer = customer.delete
assert_requested :delete, "#{Stripe.api_base}/v1/customers/#{customer.id}"
assert customer.is_a?(Stripe::Customer)
end
end

context ".delete" do
should "be deletable" do
customer = Stripe::Customer.delete("cus_123")
assert_requested :delete, "#{Stripe.api_base}/v1/customers/cus_123"
assert customer.is_a?(Stripe::Customer)
end
end

context "#create_subscription" do
Expand Down
7 changes: 7 additions & 0 deletions test/stripe/ephemeral_key_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,12 @@ class EphemeralKeyTest < Test::Unit::TestCase
assert_requested :delete, "#{Stripe.api_base}/v1/ephemeral_keys/#{key.id}"
end
end

context ".delete" do
should "succeed" do
Stripe::EphemeralKey.delete("ephkey_123")
assert_requested :delete, "#{Stripe.api_base}/v1/ephemeral_keys/ephkey_123"
end
end
end
end
23 changes: 17 additions & 6 deletions test/stripe/invoice_item_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,23 @@ class InvoiceItemTest < Test::Unit::TestCase
assert item.is_a?(Stripe::InvoiceItem)
end

should "be deletable" do
item = Stripe::InvoiceItem.retrieve("ii_123")
item = item.delete
assert_requested :delete,
"#{Stripe.api_base}/v1/invoiceitems/#{item.id}"
assert item.is_a?(Stripe::InvoiceItem)
context "#delete" do
should "be deletable" do
item = Stripe::InvoiceItem.retrieve("ii_123")
item = item.delete
assert_requested :delete,
"#{Stripe.api_base}/v1/invoiceitems/#{item.id}"
assert item.is_a?(Stripe::InvoiceItem)
end
end

context ".delete" do
should "be deletable" do
item = Stripe::InvoiceItem.delete("ii_123")
assert_requested :delete,
"#{Stripe.api_base}/v1/invoiceitems/ii_123"
assert item.is_a?(Stripe::InvoiceItem)
end
end
end
end
20 changes: 16 additions & 4 deletions test/stripe/invoice_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,22 @@ class InvoiceTest < Test::Unit::TestCase
end

should "be deletable" do
invoice = Stripe::Invoice.retrieve("in_123")
invoice = invoice.delete
assert_requested :delete, "#{Stripe.api_base}/v1/invoices/#{invoice.id}"
assert invoice.is_a?(Stripe::Invoice)
end
context "#delete" do
should "be deletable" do
invoice = Stripe::Invoice.retrieve("in_123")
invoice = invoice.delete
assert_requested :delete, "#{Stripe.api_base}/v1/invoices/#{invoice.id}"
assert invoice.is_a?(Stripe::Invoice)
end
end

context ".delete" do
should "be deletable" do
invoice = Stripe::Invoice.delete("in_123")
assert_requested :delete, "#{Stripe.api_base}/v1/invoices/in_123"
assert invoice.is_a?(Stripe::Invoice)
end
end

context "#finalize" do
Expand Down
36 changes: 21 additions & 15 deletions test/stripe/plan_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ class PlanTest < Test::Unit::TestCase
end

should "be retrievable" do
plan = Stripe::Plan.retrieve("sapphire-elite")
assert_requested :get, "#{Stripe.api_base}/v1/plans/sapphire-elite"
plan = Stripe::Plan.retrieve("pl_123")
assert_requested :get, "#{Stripe.api_base}/v1/plans/pl_123"
assert plan.is_a?(Stripe::Plan)
end

Expand All @@ -22,8 +22,7 @@ class PlanTest < Test::Unit::TestCase
amount: 5000,
interval: "month",
name: "Sapphire elite",
currency: "usd",
id: "sapphire-elite"
currency: "usd"
)
assert_requested :post, "#{Stripe.api_base}/v1/plans"
assert plan.is_a?(Stripe::Plan)
Expand All @@ -35,7 +34,6 @@ class PlanTest < Test::Unit::TestCase
interval: "month",
name: "Sapphire elite",
currency: "usd",
id: "sapphire-elite",
usage_type: "metered"
)
assert_requested :post, "#{Stripe.api_base}/v1/plans"
Expand All @@ -47,7 +45,6 @@ class PlanTest < Test::Unit::TestCase
interval: "month",
name: "Sapphire elite",
currency: "usd",
id: "sapphire-elite",
billing_scheme: "tiered",
tiers_mode: "volume",
tiers: [{ up_to: 100, amount: 1000 }, { up_to: "inf", amount: 2000 }]
Expand All @@ -61,7 +58,6 @@ class PlanTest < Test::Unit::TestCase
interval: "month",
name: "Sapphire elite",
currency: "usd",
id: "sapphire-elite",
amount: 5000,
transform_usage: { round: "up", divide_by: 50 }
)
Expand All @@ -70,23 +66,33 @@ class PlanTest < Test::Unit::TestCase
end

should "be saveable" do
plan = Stripe::Plan.retrieve("sapphire-elite")
plan = Stripe::Plan.retrieve("pl_123")
plan.metadata["key"] = "value"
plan.save
assert_requested :post, "#{Stripe.api_base}/v1/plans/#{plan.id}"
end

should "be updateable" do
plan = Stripe::Plan.update("sapphire-elite", metadata: { foo: "bar" })
assert_requested :post, "#{Stripe.api_base}/v1/plans/sapphire-elite"
plan = Stripe::Plan.update("pl_123", metadata: { foo: "bar" })
assert_requested :post, "#{Stripe.api_base}/v1/plans/pl_123"
assert plan.is_a?(Stripe::Plan)
end

should "be deletable" do
plan = Stripe::Plan.retrieve("sapphire-elite")
plan = plan.delete
assert_requested :delete, "#{Stripe.api_base}/v1/plans/#{plan.id}"
assert plan.is_a?(Stripe::Plan)
context "#delete" do
should "be deletable" do
plan = Stripe::Plan.retrieve("pl_123")
plan = plan.delete
assert_requested :delete, "#{Stripe.api_base}/v1/plans/#{plan.id}"
assert plan.is_a?(Stripe::Plan)
end
end

context ".delete" do
should "be deletable" do
plan = Stripe::Plan.delete("pl_123")
assert_requested :delete, "#{Stripe.api_base}/v1/plans/pl_123"
assert plan.is_a?(Stripe::Plan)
end
end
end
end
20 changes: 15 additions & 5 deletions test/stripe/product_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,21 @@ class ProductTest < Test::Unit::TestCase
assert product.is_a?(Stripe::Product)
end

should "be deletable" do
product = Stripe::Product.retrieve("prod_123")
product = product.delete
assert_requested :delete, "#{Stripe.api_base}/v1/products/#{product.id}"
assert product.is_a?(Stripe::Product)
context "#delete" do
should "be deletable" do
product = Stripe::Product.retrieve("prod_123")
product = product.delete
assert_requested :delete, "#{Stripe.api_base}/v1/products/#{product.id}"
assert product.is_a?(Stripe::Product)
end
end

context ".delete" do
should "be deletable" do
product = Stripe::Product.delete("prod_123")
assert_requested :delete, "#{Stripe.api_base}/v1/products/prod_123"
assert product.is_a?(Stripe::Product)
end
end
end
end
20 changes: 15 additions & 5 deletions test/stripe/radar/value_list_item_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,21 @@ class ValueListItemTest < Test::Unit::TestCase
assert item.is_a?(Stripe::Radar::ValueListItem)
end

should "be deletable" do
list = Stripe::Radar::ValueListItem.retrieve("rsli_123")
list = list.delete
assert_requested :delete, "#{Stripe.api_base}/v1/radar/value_list_items/rsli_123"
assert list.is_a?(Stripe::Radar::ValueListItem)
context "#delete" do
should "be deletable" do
list = Stripe::Radar::ValueListItem.retrieve("rsli_123")
list = list.delete
assert_requested :delete, "#{Stripe.api_base}/v1/radar/value_list_items/rsli_123"
assert list.is_a?(Stripe::Radar::ValueListItem)
end
end

context ".delete" do
should "be deletable" do
list = Stripe::Radar::ValueListItem.delete("rsli_123")
assert_requested :delete, "#{Stripe.api_base}/v1/radar/value_list_items/rsli_123"
assert list.is_a?(Stripe::Radar::ValueListItem)
end
end
end
end
Expand Down
20 changes: 15 additions & 5 deletions test/stripe/radar/value_list_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,21 @@ class ValueListTest < Test::Unit::TestCase
assert list.is_a?(Stripe::Radar::ValueList)
end

should "be deletable" do
list = Stripe::Radar::ValueList.retrieve("rsl_123")
list = list.delete
assert_requested :delete, "#{Stripe.api_base}/v1/radar/value_lists/rsl_123"
assert list.is_a?(Stripe::Radar::ValueList)
context "#delete" do
should "be deletable" do
list = Stripe::Radar::ValueList.retrieve("rsl_123")
list = list.delete
assert_requested :delete, "#{Stripe.api_base}/v1/radar/value_lists/rsl_123"
assert list.is_a?(Stripe::Radar::ValueList)
end
end

context ".delete" do
should "be deletable" do
list = Stripe::Radar::ValueList.delete("rsl_123")
assert_requested :delete, "#{Stripe.api_base}/v1/radar/value_lists/rsl_123"
assert list.is_a?(Stripe::Radar::ValueList)
end
end
end
end
Expand Down
Loading

0 comments on commit 0790bb4

Please sign in to comment.