spreedly 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,3 +1,8 @@
1
+ === 1.2.0 / 2009-04-30
2
+
3
+ * Added mock support for Subscriber#activate_free_trial [scottmotte].
4
+ * Added tests for Subscriber#activate_free_trial.
5
+
1
6
  === 1.1.0 / 2009-04-24
2
7
 
3
8
  * Compatibility with the latest hoe and HTTParty [seancribbs].
data/README.txt CHANGED
@@ -19,14 +19,14 @@ the http://spreedly.com API. Created by Terralien[http://terralien.com].
19
19
 
20
20
  # For real
21
21
  require 'spreedly'
22
- Spreedly.configure('site name', 'crazy hash token')
22
+ Spreedly.configure('site short name', 'crazy hash token')
23
23
  url = Spreedly.subscribe_url(:id => 'customer id', :plan => 'plan id')
24
24
  subscriber = Spreedly::Subscriber.find('customer id')
25
25
  subscriber.active?
26
26
 
27
- # For mock
27
+ # For fast tests
28
28
  require 'spreedly/mock'
29
- Spreedly.configure('site name', 'crazy hash token')
29
+ Spreedly.configure('site short name', 'crazy hash token')
30
30
  url = Spreedly.subscribe_url(:id => 'customer id', :plan => 'plan id')
31
31
  subscriber = Spreedly::Subscriber.find('customer id')
32
32
  subscriber.active?
data/lib/spreedly.rb CHANGED
@@ -185,5 +185,9 @@ module Spreedly
185
185
  def self.find(id)
186
186
  all.detect{|e| e.id.to_s == id.to_s}
187
187
  end
188
+
189
+ def trial?
190
+ (plan_type == 'free_trial')
191
+ end
188
192
  end
189
193
  end
data/lib/spreedly/mock.rb CHANGED
@@ -50,6 +50,7 @@ module Spreedly
50
50
  :store_credit => proc{BigDecimal("0.0")},
51
51
  :active_until => proc{nil},
52
52
  :feature_level => proc{""},
53
+ :on_trial => proc{false}
53
54
  }
54
55
 
55
56
  def self.wipe! # :nodoc: all
@@ -103,6 +104,14 @@ module Spreedly
103
104
  @attributes[:feature_level] = feature_level if feature_level
104
105
  @attributes[:active] = true
105
106
  end
107
+
108
+ def activate_free_trial(plan_id)
109
+ raise "Could not activate free trial for subscriber: validation failed. missing subscription plan id" unless plan_id
110
+ raise "Could not active free trial for subscriber: subscriber or subscription plan no longer exists." unless self.class.find(id) && SubscriptionPlan.find(plan_id)
111
+ raise "Could not activate free trial for subscriber: subscription plan either 1) isn't a free trial, 2) the subscriber is not eligible for a free trial, or 3) the subscription plan is not enabled." if on_trial?
112
+ @attributes[:active] = true
113
+ @attributes[:on_trial] = true
114
+ end
106
115
  end
107
116
 
108
117
  class SubscriptionPlan < Resource
@@ -115,7 +124,11 @@ module Spreedly
115
124
  end
116
125
 
117
126
  def self.plans
118
- @plans ||= {1 => new(:id => 1, :name => 'Default mock plan')}
127
+ @plans ||= {1 => new(:id => 1, :name => 'Default mock plan'), 2 => new(:id => 2, :name => 'Test Free Trial Plan', :plan_type => 'free_trial')}
128
+ end
129
+
130
+ def trial?
131
+ (plan_type == "free_trial")
119
132
  end
120
133
  end
121
134
  end
@@ -1,3 +1,3 @@
1
1
  module Spreedly
2
- VERSION = "1.1.0"
2
+ VERSION = "1.2.0"
3
3
  end
@@ -155,6 +155,41 @@ class SpreedlyGemTest < Test::Unit::TestCase
155
155
  assert_equal plan.name, Spreedly::SubscriptionPlan.find(plan.id).name
156
156
  end
157
157
 
158
+ context "with a Free Trial plan" do
159
+ setup do
160
+ @trial = Spreedly::SubscriptionPlan.all.detect{|e| e.name == "Test Free Trial Plan" && e.trial?}
161
+ assert @trial, "For this test to pass in REAL mode you must have a trial plan in your Spreedly test site with the name \"Test Free Trial Plan\"."
162
+ end
163
+
164
+ should "be able to activate free trial" do
165
+ sub = create_subscriber
166
+ assert !sub.active?
167
+ assert !sub.on_trial?
168
+
169
+ sub.activate_free_trial(@trial.id)
170
+ sub = Spreedly::Subscriber.find(sub.id)
171
+ assert sub.active?
172
+ assert sub.on_trial?
173
+ end
174
+
175
+ should "throw an error if a second trial is activated" do
176
+ sub = create_subscriber
177
+ sub.activate_free_trial(@trial.id)
178
+ ex = assert_raise(RuntimeError){sub.activate_free_trial(@trial.id)}
179
+ assert_match %r{not eligible}, ex.message
180
+ end
181
+
182
+ should "throw errors on invalid free trial activation" do
183
+ sub = create_subscriber
184
+
185
+ ex = assert_raise(RuntimeError){sub.activate_free_trial(0)}
186
+ assert_match %r{no longer exists}, ex.message
187
+
188
+ ex = assert_raise(RuntimeError){sub.activate_free_trial(nil)}
189
+ assert_match %r{missing}, ex.message
190
+ end
191
+ end
192
+
158
193
  only_real do
159
194
  should "throw an error if comp is wrong type" do
160
195
  sub = create_subscriber
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spreedly
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nathaniel Talbott
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-04-24 00:00:00 -04:00
12
+ date: 2009-04-30 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency