Skip to content

Commit

Permalink
More coupon goodness
Browse files Browse the repository at this point in the history
  • Loading branch information
caffo committed Jul 28, 2011
1 parent d8e499f commit a6e251d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
31 changes: 17 additions & 14 deletions lib/chargify_api_ares.rb
Expand Up @@ -65,7 +65,7 @@ def configure
Subscription::Component.site = site + "/subscriptions/:subscription_id"
Subscription::Statement.site = site + "/subscriptions/:subscription_id"
Subscription::Transaction.site = site + "/subscriptions/:subscription_id"
Subscription::Coupon.site = site + "/product_families/:product_family_id"
Coupon.site = site + "/product_families/:product_family_id"
end
end

Expand Down Expand Up @@ -186,8 +186,8 @@ def add_coupon(code)
post :add_coupon, :code => code
end

def remove_coupon
delete :remove_coupon
def remove_coupon(code)
delete :remove_coupon, :code => code
end

class Component < Base
Expand All @@ -197,17 +197,6 @@ def id
end
end

class Coupon < Base
# Coupons are found in the scope of a product family
def self.find_by_product_family_id_and_code(product_family_id, code)
Coupon.new get(:lookup, :product_family_id => product_family_id, :code => code)
end

def usage
get :usage
end
end

class Statement < Base
end

Expand Down Expand Up @@ -292,4 +281,18 @@ def refund(attrs = {})
class PaymentProfile < Base
end

class Coupon < Base
def self.find_by_product_family_id_and_code(product_family_id, code)
Coupon.new get(:lookup, :product_family_id => product_family_id, :code => code)
end

def usage
get :usage
end

def archive
self.destroy
end
end

end
12 changes: 6 additions & 6 deletions samples/coupons.rb
Expand Up @@ -12,7 +12,7 @@


# Create a new coupon
coupon = Chargify::Subscription::Coupon.create(
coupon = Chargify::Coupon.create(
:name => "66% off",
:code => "66OFF",
:description => "66% OFF for life",
Expand All @@ -25,16 +25,16 @@
coupon = Chargify::Subscription::Coupon.find_by_product_family_id_and_code(2,"10OFF")

# Update coupon information
coupon = Chargify::Subscription::Coupon.find_by_product_family_id_and_code(2,"10OFF")
coupon = Chargify::Coupon.find_by_product_family_id_and_code(2,"10OFF")
coupon.code = "FOO"
coupon.save

# Archive coupon
coupon = Chargify::Subscription::Coupon.find_by_product_family_id_and_code(2,"10OFF")
coupon.destroy
coupon = Chargify::Coupon.find_by_product_family_id_and_code(2,"10OFF")
coupon.archive

# Inspect coupon usage
coupon = Chargify::Subscription::Coupon.find_by_product_family_id_and_code(2,"10OFF")
coupon = Chargify::Coupon.find_by_product_family_id_and_code(2,"10OFF")
coupon.usage
#>> [{"name"=>"Product 1", "savings"=>0.0, "id"=>8, "signups"=>5, "revenue"=>0.0}, {"name"=>"Product 2", "savings"=>0.0, "id"=>9, "signups"=>0, "revenue"=>0.0}]

Expand All @@ -44,4 +44,4 @@

#Remove coupon from subscription
subscription = Subscription.find_by_customer_reference('moklett')
subscription.remove_coupon
subscription.remove_coupon('50OFF')

0 comments on commit a6e251d

Please sign in to comment.