Skip to content

Commit

Permalink
support new lifetime subscription API
Browse files Browse the repository at this point in the history
  • Loading branch information
rlivsey committed Jan 20, 2010
1 parent 577dccc commit f147644
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/rspreedly.rb
Expand Up @@ -11,7 +11,7 @@
require 'rspreedly/payment_method'
require 'rspreedly/complimentary_subscription'
require 'rspreedly/complimentary_time_extension'

require 'rspreedly/lifetime_complimentary_subscription'

module RSpreedly

Expand Down
5 changes: 5 additions & 0 deletions lib/rspreedly/lifetime_complimentary_subscription.rb
@@ -0,0 +1,5 @@
module RSpreedly
class LifetimeComplimentarySubscription < Base
attr_accessor :feature_level
end
end
7 changes: 7 additions & 0 deletions lib/rspreedly/subscriber.rb
Expand Up @@ -155,6 +155,13 @@ def allow_free_trial
true
end

def grant_lifetime_subscription(feature_level)
subscription = LifetimeComplimentarySubscription.new(:feature_level => feature_level)
result = api_request(:post, "/subscribers/#{self.customer_id}/lifetime_complimentary_subscriptions.xml", :body => subscription.to_xml)
self.attributes = result["subscriber"]
true
end

def to_xml(opts={})

# the api doesn't let us send these things
Expand Down
4 changes: 4 additions & 0 deletions spec/fixtures/feature_level_blank.xml
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<errors>
<error>Feature level can't be blank</error>
</errors>
25 changes: 25 additions & 0 deletions spec/fixtures/lifetime_subscription_success.xml
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<subscriber>
<active-until type="datetime" nil="true">2010-02-21T19:04:28Z</active-until>
<billing-first-name nil="true"></billing-first-name>
<billing-last-name nil="true"></billing-last-name>
<created-at type="datetime">2009-10-28T22:27:38Z</created-at>
<customer-id>42</customer-id>
<eligible-for-free-trial type="boolean">true</eligible-for-free-trial>
<email>new@email.com</email>
<grace-until type="datetime">2013-05-02T00:07:37Z</grace-until>
<lifetime-subscription type="boolean">true</lifetime-subscription>
<ready-to-renew type="boolean">false</ready-to-renew>
<screen-name>bob</screen-name>
<store-credit type="decimal">0.0</store-credit>
<store-credit-currency-code>USD</store-credit-currency-code>
<token>81ba778a5849f461ebc0d77e78b9221af2210c6b</token>
<updated-at type="datetime">2009-10-28T22:55:37Z</updated-at>
<recurring type="boolean">false</recurring>
<card-expires-before-next-auto-renew type="boolean">false</card-expires-before-next-auto-renew>
<subscription-plan-name></subscription-plan-name>
<active type="boolean">false</active>
<in_grace_period type="boolean"></in_grace_period>
<on-trial type="boolean">false</on-trial>
<feature-level type="string">Something</feature-level>
</subscriber>
40 changes: 40 additions & 0 deletions spec/subscriber_spec.rb
Expand Up @@ -495,4 +495,44 @@
}.should raise_error(RSpreedly::Error::NotFound)
end
end

describe "#grant_lifetime_subscription" do

before(:each) do
@subscriber = RSpreedly::Subscriber.new(:customer_id => 42)
end

it "should return true if successful" do
stub_http_with_fixture("lifetime_subscription_success.xml", 200)
@subscriber.grant_lifetime_subscription("Something").should be_true
end

it "should update the subscriber's lifetime_subscription if successful" do
stub_http_with_fixture("lifetime_subscription_success.xml", 200)
lambda{
@subscriber.grant_lifetime_subscription("Something")
}.should change(@subscriber, :lifetime_subscription).to(true)
end

it "should update the subscriber's feature_level if successful" do
stub_http_with_fixture("lifetime_subscription_success.xml", 200)
lambda{
@subscriber.grant_lifetime_subscription("Something")
}.should change(@subscriber, :feature_level).to("Something")
end

it "should raise NotFound if the subscriber doesn't exist" do
stub_http_with_fixture("subscriber_not_found.xml", 404)
lambda{
@subscriber.grant_lifetime_subscription("Something")
}.should raise_error(RSpreedly::Error::NotFound)
end

it "should raise BadRequest if the feature level is blank" do
stub_http_with_fixture("feature_level_blank.xml", 422)
lambda{
@subscriber.grant_lifetime_subscription("Something")
}.should raise_error(RSpreedly::Error::BadRequest)
end
end
end

0 comments on commit f147644

Please sign in to comment.