spreedly 1.2.2 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/History.txt CHANGED
@@ -1,3 +1,8 @@
1
+ === 1.3.0 / 2009-06-04
2
+
3
+ * Properly handle invalid Subscriber lookup [karnowski].
4
+ * Added support for stopping auto renew [scottmotte, dsimard].
5
+
1
6
  === 1.2.2 / 2009-05-11
2
7
 
3
8
  * Fixed an error in the README [karnowski].
data/Rakefile CHANGED
@@ -9,6 +9,7 @@ Hoe.new('spreedly', Spreedly::VERSION) do |project|
9
9
  project.rubyforge_name = 'terralien'
10
10
  project.developer('Nathaniel Talbott', 'nathaniel@terralien.com')
11
11
  project.test_globs = ["test/**/*_test.rb"]
12
+ project.extra_deps = ["mechanize"]
12
13
  project.extra_dev_deps = ["thoughtbot-shoulda"]
13
14
  end
14
15
 
data/lib/spreedly/mock.rb CHANGED
@@ -23,7 +23,7 @@ module Spreedly
23
23
  end
24
24
 
25
25
  def initialize(params={})
26
- @attributes = params
26
+ @attributes = params.inject({}){|a,(k,v)| a[k.to_sym] = v; a}
27
27
  self.class.attributes.each{|k,v| @attributes[k] = v.call}
28
28
  end
29
29
 
@@ -50,7 +50,8 @@ 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
+ :on_trial => proc{false},
54
+ :recurring => proc{false}
54
55
  }
55
56
 
56
57
  def self.wipe! # :nodoc: all
@@ -58,7 +59,7 @@ module Spreedly
58
59
  end
59
60
 
60
61
  def self.create!(id, email=nil, screen_name=nil) # :nodoc: all
61
- sub = new({:id => id, :email => email, :screen_name => screen_name})
62
+ sub = new({:customer_id => id, :email => email, :screen_name => screen_name})
62
63
 
63
64
  if subscribers[sub.id]
64
65
  raise "Could not create subscriber: already exists."
@@ -91,6 +92,10 @@ module Spreedly
91
92
  end
92
93
  end
93
94
 
95
+ def id
96
+ @attributes[:customer_id]
97
+ end
98
+
94
99
  def comp(quantity, units, feature_level=nil)
95
100
  raise "Could not comp subscriber: no longer exists." unless self.class.find(id)
96
101
  raise "Could not comp subscriber: validation failed." unless units && quantity
@@ -112,6 +117,16 @@ module Spreedly
112
117
  @attributes[:active] = true
113
118
  @attributes[:on_trial] = true
114
119
  end
120
+
121
+ def stop_auto_renew
122
+ raise "Could not stop auto renew for subscriber: subscriber does not exist." unless self.class.find(id)
123
+ @attributes[:recurring] = false
124
+ end
125
+
126
+ def subscribe(plan_id)
127
+ @attributes[:active] = true
128
+ @attributes[:recurring] = true
129
+ end
115
130
  end
116
131
 
117
132
  class SubscriptionPlan < Resource
@@ -124,7 +139,11 @@ module Spreedly
124
139
  end
125
140
 
126
141
  def self.plans
127
- @plans ||= {1 => new(:id => 1, :name => 'Default mock plan'), 2 => new(:id => 2, :name => 'Test Free Trial Plan', :plan_type => 'free_trial')}
142
+ @plans ||= {
143
+ 1 => new(:id => 1, :name => 'Default mock plan'),
144
+ 2 => new(:id => 2, :name => 'Test Free Trial Plan', :plan_type => 'free_trial'),
145
+ 3 => new(:id => 3, :name => 'Test Regular Plan'),
146
+ }
128
147
  end
129
148
 
130
149
  def trial?
@@ -1,3 +1,3 @@
1
1
  module Spreedly
2
- VERSION = "1.2.2"
2
+ VERSION = "1.3.0"
3
3
  end
data/lib/spreedly.rb CHANGED
@@ -116,7 +116,8 @@ module Spreedly
116
116
 
117
117
  # Looks a subscriber up by id.
118
118
  def self.find(id)
119
- new(Spreedly.get("/subscribers/#{id}.xml")['subscriber'])
119
+ xml = Spreedly.get("/subscribers/#{id}.xml")
120
+ (xml.nil? ? nil : new(xml['subscriber']))
120
121
  end
121
122
 
122
123
  # Returns all the subscribers in your site.
@@ -156,10 +157,10 @@ module Spreedly
156
157
  end
157
158
 
158
159
  # Activates a free trial on the subscriber.
159
- # Requires subscription_id of the free trial plan
160
- def activate_free_trial(subscription_id)
160
+ # Requires plan_id of the free trial plan
161
+ def activate_free_trial(plan_id)
161
162
  result = Spreedly.post("/subscribers/#{id}/subscribe_to_free_trial.xml", :body =>
162
- Spreedly.to_xml_params(:subscription_plan => {:id => subscription_id}))
163
+ Spreedly.to_xml_params(:subscription_plan => {:id => plan_id}))
163
164
  case result.code
164
165
  when /2../
165
166
  when '404'
@@ -173,6 +174,19 @@ module Spreedly
173
174
  end
174
175
  end
175
176
 
177
+ # Stop the auto renew of the subscriber such that their recurring subscription will no longer be renewed.
178
+ # usage: @subscriber.stop_auto_renew
179
+ def stop_auto_renew
180
+ result = Spreedly.post("/subscribers/#{id}/stop_auto_renew.xml")
181
+ case result.code
182
+ when /2../
183
+ when '404'
184
+ raise "Could not stop auto renew for subscriber: subscriber does not exist."
185
+ else
186
+ raise "Could not stop auto renew for subscriber: result code #{result.code}."
187
+ end
188
+ end
189
+
176
190
  end
177
191
 
178
192
  class SubscriptionPlan < Resource
@@ -1,3 +1,4 @@
1
+ require 'rubygems'
1
2
  require 'test/unit'
2
3
  require 'shoulda'
3
4
  require 'yaml'
@@ -5,6 +6,7 @@ require 'pp'
5
6
 
6
7
  if ENV["SPREEDLY_TEST"] == "REAL"
7
8
  require 'spreedly'
9
+ require 'spreedly/test_hacks'
8
10
  else
9
11
  require 'spreedly/mock'
10
12
  end
@@ -45,10 +47,15 @@ class SpreedlyGemTest < Test::Unit::TestCase
45
47
  assert_nil subscriber.active_until
46
48
  end
47
49
 
50
+ should "return nil when getting a subscriber that does NOT exist" do
51
+ assert_nil Spreedly::Subscriber.find("junk")
52
+ end
53
+
48
54
  should "expose and parse attributes" do
49
55
  subscriber = create_subscriber
50
56
  assert_kind_of Time, subscriber.created_at
51
57
  assert_equal false, subscriber.active
58
+ assert_equal false, subscriber.recurring
52
59
  assert_equal BigDecimal('0.0'), subscriber.store_credit
53
60
  end
54
61
 
@@ -190,6 +197,33 @@ class SpreedlyGemTest < Test::Unit::TestCase
190
197
  end
191
198
  end
192
199
 
200
+ context "with a Regular plan" do
201
+ setup do
202
+ @regular_plan = Spreedly::SubscriptionPlan.all.detect{|e| e.name == "Test Regular Plan"}
203
+ assert @regular_plan, "For this test to pass in REAL mode you must have a regular plan in your Spreedly test site with the name \"Test Regular Plan\"."
204
+ end
205
+
206
+ should "stop auto renew for subscriber" do
207
+ subscriber = create_subscriber
208
+ subscriber.subscribe(@regular_plan.id)
209
+
210
+ subscriber = Spreedly::Subscriber.find(subscriber.id)
211
+ assert subscriber.active?
212
+ assert subscriber.recurring
213
+
214
+ subscriber.stop_auto_renew
215
+ subscriber = Spreedly::Subscriber.find(subscriber.id)
216
+ assert subscriber.active?
217
+ assert !subscriber.recurring
218
+ end
219
+ end
220
+
221
+ should "throw an error if stopping auto renew on a non-existent subscriber" do
222
+ sub = Spreedly::Subscriber.new('customer_id' => 'bogus')
223
+ ex = assert_raise(RuntimeError){sub.stop_auto_renew}
224
+ assert_match %r{does not exist}, ex.message
225
+ end
226
+
193
227
  only_real do
194
228
  should "throw an error if comp is wrong type" do
195
229
  sub = create_subscriber
data/test/test_site.yml CHANGED
@@ -1,2 +1,2 @@
1
1
  name: terralien-test
2
- token: 3a8233463b5f8150d8bab0d2e7bac9272fc31763
2
+ token: 781651cf01c65b7f0c8f6565723ea60436e87a56
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.2.2
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nathaniel Talbott
@@ -9,9 +9,19 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-05-11 00:00:00 -04:00
12
+ date: 2009-06-04 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: mechanize
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
15
25
  - !ruby/object:Gem::Dependency
16
26
  name: thoughtbot-shoulda
17
27
  type: :development