stefl-chargify 0.3.3

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.
Files changed (45) hide show
  1. data/.gitignore +23 -0
  2. data/.rspec +1 -0
  3. data/Gemfile +20 -0
  4. data/Gemfile.lock +51 -0
  5. data/LICENSE +20 -0
  6. data/README.markdown +46 -0
  7. data/Rakefile +33 -0
  8. data/VERSION +1 -0
  9. data/changelog.md +30 -0
  10. data/lib/chargify/base.rb +89 -0
  11. data/lib/chargify/config.rb +86 -0
  12. data/lib/chargify/customer.rb +101 -0
  13. data/lib/chargify/error.rb +24 -0
  14. data/lib/chargify/parser.rb +13 -0
  15. data/lib/chargify/product.rb +38 -0
  16. data/lib/chargify/product_family.rb +47 -0
  17. data/lib/chargify/subscription.rb +154 -0
  18. data/lib/chargify/transaction.rb +14 -0
  19. data/lib/chargify.rb +18 -0
  20. data/spec/fixtures/charge_subscription.json +5 -0
  21. data/spec/fixtures/charge_subscription_missing_parameters.json +4 -0
  22. data/spec/fixtures/component.json +11 -0
  23. data/spec/fixtures/components.json +24 -0
  24. data/spec/fixtures/customer.json +12 -0
  25. data/spec/fixtures/customers.json +12 -0
  26. data/spec/fixtures/deleted_subscription.json +1 -0
  27. data/spec/fixtures/invalid_subscription.json +48 -0
  28. data/spec/fixtures/list_metered_subscriptions.json +3 -0
  29. data/spec/fixtures/migrate_subscription.json +51 -0
  30. data/spec/fixtures/new_customer.json +12 -0
  31. data/spec/fixtures/product.json +17 -0
  32. data/spec/fixtures/products.json +17 -0
  33. data/spec/fixtures/subscription.json +49 -0
  34. data/spec/fixtures/subscription_not_found.json +1 -0
  35. data/spec/fixtures/subscriptions.json +49 -0
  36. data/spec/spec_helper.rb +27 -0
  37. data/spec/support/fakeweb_stubs.rb +33 -0
  38. data/spec/unit/chargify/config_spec.rb +147 -0
  39. data/spec/unit/chargify/customer_spec.rb +186 -0
  40. data/spec/unit/chargify/parser_spec.rb +7 -0
  41. data/spec/unit/chargify/product_spec.rb +40 -0
  42. data/spec/unit/chargify/subscription_spec.rb +432 -0
  43. data/spec/unit/chargify/transaction_spec.rb +11 -0
  44. data/stefl-chargify.gemspec +102 -0
  45. metadata +180 -0
@@ -0,0 +1,432 @@
1
+ require 'spec_helper'
2
+
3
+ describe Chargify::Subscription do
4
+
5
+ before(:all) do
6
+ Chargify::Config.setup do |config|
7
+ config[:api_key] = 'OU812'
8
+ config[:subdomain] = 'pengwynn'
9
+ end
10
+ end
11
+
12
+ describe '.all' do
13
+
14
+ it "should return a list of products" do
15
+ stub_get "https://OU812:x@pengwynn.chargify.com/subscriptions.json", "subscriptions.json"
16
+ subscription = Chargify::Subscription.all
17
+ subscription.first.customer.reference.should == 'bradleyjoyce'
18
+ end
19
+
20
+ end
21
+
22
+ describe '.find!' do
23
+
24
+ it "should return info for a subscription" do
25
+ stub_get "https://OU812:x@pengwynn.chargify.com/subscriptions/13.json", "subscription.json"
26
+ subscription = Chargify::Subscription.find!(13)
27
+ subscription.customer.reference.should == 'bradleyjoyce'
28
+ end
29
+
30
+ it "should throw a not found error if a subscription is not found" do
31
+ stub_get "https://OU812:x@pengwynn.chargify.com/subscriptions/18.json", "subscription_not_found.json", 404
32
+ lambda {
33
+ Chargify::Subscription.find!(18)
34
+ }.should raise_error(Chargify::Error::NotFound)
35
+ end
36
+
37
+ end
38
+
39
+ describe '.find' do
40
+
41
+ it "should return nil if a subscription is not found" do
42
+ stub_get "https://OU812:x@pengwynn.chargify.com/subscriptions/18.json", "subscription_not_found.json", 404
43
+ Chargify::Subscription.find(18).should == nil
44
+ end
45
+
46
+ end
47
+
48
+ describe '.create!' do
49
+
50
+ it "should create a customer subscription" do
51
+ stub_post "https://OU812:x@pengwynn.chargify.com/subscriptions.json", "subscription.json"
52
+ options = {
53
+ :product_handle => 'monthly',
54
+ :customer_reference => 'bradleyjoyce',
55
+ :customer_attributes => {
56
+ :first_name => "Wynn",
57
+ :last_name => "Netherland",
58
+ :email => "wynn@example.com"
59
+ }
60
+ }
61
+ subscription = Chargify::Subscription.create!(options)
62
+ subscription.customer.organization.should == 'Squeejee'
63
+ end
64
+
65
+ it "should create a customer subscription with a coupon code" do
66
+ stub_post "https://OU812:x@pengwynn.chargify.com/subscriptions.json", "subscription.json"
67
+ options = {
68
+ :product_handle => 'monthly',
69
+ :customer_reference => 'bradleyjoyce',
70
+ :customer_attributes => {
71
+ :first_name => "Wynn",
72
+ :last_name => "Netherland",
73
+ :email => "wynn@example.com"
74
+ },
75
+ :coupon_code => "EARLYBIRD"
76
+ }
77
+ subscription = Chargify::Subscription.create!(options)
78
+ subscription.should be_a(Hashie::Mash)
79
+ #subscription.coupon.should == 'Squeejee'
80
+ end
81
+
82
+ it "should raise error to true when subscription is created successfully" do
83
+ stub_post "https://OU812:x@pengwynn.chargify.com/subscriptions.json", "subscription.json", 201
84
+ options = {
85
+ :product_handle => 'monthly',
86
+ :customer_reference => 'bradleyjoyce',
87
+ :customer_attributes => {
88
+ :first_name => "Wynn",
89
+ :last_name => "Netherland",
90
+ :email => "wynn@example.com"
91
+ }
92
+ }
93
+ subscription = Chargify::Subscription.create!(options)
94
+ subscription.should be_a(Hashie::Mash)
95
+ end
96
+
97
+ it "should raise error to nil when subscription is not created successfully" do
98
+ stub_post "https://OU812:x@pengwynn.chargify.com/subscriptions.json", "subscription.json", 422
99
+ options = {
100
+ :product_handle => 'monthly',
101
+ :customer_reference => 'bradleyjoyce',
102
+ :customer_attributes => {
103
+ :first_name => "Wynn",
104
+ :last_name => "Netherland",
105
+ :email => "wynn@example.com"
106
+ }
107
+ }
108
+ lambda {
109
+ Chargify::Subscription.create!(options)
110
+ }.should raise_error(Chargify::Error::BadRequest)
111
+
112
+ end
113
+
114
+ it "should raise UnexpectedResponseError when reponse is invalid JSON" do
115
+ stub_post "https://OU812:x@pengwynn.chargify.com/subscriptions.json", "invalid_subscription.json"
116
+ options = {
117
+ :product_handle => 'monthly',
118
+ :customer_reference => 'bradleyjoyce',
119
+ :customer_attributes => {
120
+ :first_name => "Wynn",
121
+ :last_name => "Netherland",
122
+ :email => "wynn@example.com"
123
+ }
124
+ }
125
+ lambda {
126
+ Chargify::Subscription.create!(options)
127
+ }.should raise_error(Chargify::Error::UnexpectedResponse)
128
+ end
129
+
130
+ end
131
+
132
+ describe '.create' do
133
+
134
+ it "should return false if creation errors out" do
135
+ stub_post "https://OU812:x@pengwynn.chargify.com/subscriptions.json", "subscription.json", 422
136
+ options = {
137
+ :product_handle => 'monthly',
138
+ :customer_reference => 'bradleyjoyce',
139
+ :customer_attributes => {
140
+ :first_name => "Wynn",
141
+ :last_name => "Netherland",
142
+ :email => "wynn@example.com"
143
+ }
144
+ }
145
+ Chargify::Subscription.create(options).should == false
146
+ end
147
+
148
+ end
149
+
150
+ describe '.update!' do
151
+
152
+ it "should update a customer subscription" do
153
+ stub_put "https://OU812:x@pengwynn.chargify.com/subscriptions/123.json", "subscription.json"
154
+ options = {
155
+ :product_handle => 'monthly',
156
+ :customer_reference => 'bradleyjoyce',
157
+ :customer_attributes => {
158
+ :first_name => "Wynn",
159
+ :last_name => "Netherland",
160
+ :email => "wynn@example.com"
161
+ }
162
+ }
163
+ subscription = Chargify::Subscription.update!(123, options)
164
+ subscription.customer.organization.should == 'Squeejee'
165
+ end
166
+
167
+ it "should raise error to true when subscription is updated successfully" do
168
+ stub_put "https://OU812:x@pengwynn.chargify.com/subscriptions/123.json", "subscription.json", 200
169
+ options = {
170
+ :product_handle => 'monthly',
171
+ :customer_reference => 'bradleyjoyce',
172
+ :customer_attributes => {
173
+ :first_name => "Wynn",
174
+ :last_name => "Netherland",
175
+ :email => "wynn@example.com"
176
+ }
177
+ }
178
+ subscription = Chargify::Subscription.update!(123, options)
179
+ subscription.should be_a(Hashie::Mash)
180
+ end
181
+
182
+ it "should raise error to false when subscription is not updated successfully" do
183
+ stub_put "https://OU812:x@pengwynn.chargify.com/subscriptions/123.json", "subscription.json", 500
184
+ options = {
185
+ :product_handle => 'monthly',
186
+ :customer_reference => 'bradleyjoyce',
187
+ :customer_attributes => {
188
+ :first_name => "Wynn",
189
+ :last_name => "Netherland",
190
+ :email => "wynn@example.com"
191
+ }
192
+ }
193
+
194
+ lambda {
195
+ Chargify::Subscription.update!(123, options)
196
+ }.should raise_error(Chargify::Error::ServerError)
197
+ end
198
+
199
+ end
200
+
201
+ describe '.update' do
202
+
203
+ it "should return false if subscription update fails" do
204
+ stub_put "https://OU812:x@pengwynn.chargify.com/subscriptions/123.json", "subscription.json", 500
205
+ options = {
206
+ :product_handle => 'monthly',
207
+ :customer_reference => 'bradleyjoyce',
208
+ :customer_attributes => {
209
+ :first_name => "Wynn",
210
+ :last_name => "Netherland",
211
+ :email => "wynn@example.com"
212
+ }
213
+ }
214
+ Chargify::Subscription.update(123, options)
215
+ end
216
+
217
+ end
218
+
219
+ describe '.reactivate!' do
220
+
221
+ it "should reactivate a subscription" do
222
+ stub_put "https://OU812:x@pengwynn.chargify.com/subscriptions/123/reactivate.json", "subscription.json", 200
223
+ subscription = Chargify::Subscription.reactivate!(123)
224
+ subscription.state.should == "active"
225
+ end
226
+
227
+ it "should raise error to nil when subscription is not reactivated successfully" do
228
+ stub_put "https://OU812:x@pengwynn.chargify.com/subscriptions/123/reactivate.json", "subscription_not_found.json", 422
229
+ lambda {
230
+ Chargify::Subscription.reactivate!(123)
231
+ }.should raise_error(Chargify::Error::BadRequest)
232
+ end
233
+
234
+ it "should raise error to false when subscription is reactivated successfully" do
235
+ stub_put "https://OU812:x@pengwynn.chargify.com/subscriptions/123/reactivate.json", "subscription.json", 200
236
+ subscription = Chargify::Subscription.reactivate!(123)
237
+ subscription.should be_a(Hashie::Mash)
238
+ end
239
+
240
+ end
241
+
242
+ describe '.reactivate' do
243
+
244
+ it "should return false if an error occurs" do
245
+ stub_put "https://OU812:x@pengwynn.chargify.com/subscriptions/123/reactivate.json", "subscription_not_found.json", 422
246
+ Chargify::Subscription.reactivate(123).should == false
247
+ end
248
+
249
+ end
250
+
251
+ describe '.cancel!' do
252
+
253
+ it "should raise error to nil when subscription is not cancelled successfully" do
254
+ stub_delete "https://OU812:x@pengwynn.chargify.com/subscriptions/123.json", "deleted_subscription.json", 500
255
+
256
+ lambda {
257
+ Chargify::Subscription.cancel!(123)
258
+ }.should raise_error(Chargify::Error::ServerError)
259
+ end
260
+
261
+ it "should raise error to true when subscription is cancelled successfully" do
262
+ stub_delete "https://OU812:x@pengwynn.chargify.com/subscriptions/123.json", "deleted_subscription.json", 200
263
+ Chargify::Subscription.cancel!(123).should == true
264
+ end
265
+
266
+ end
267
+
268
+ describe '.cancel' do
269
+
270
+ it "should return false if an error occurs" do
271
+ stub_delete "https://OU812:x@pengwynn.chargify.com/subscriptions/123.json", "deleted_subscription.json", 500
272
+ Chargify::Subscription.cancel(123).should == false
273
+ end
274
+
275
+ end
276
+
277
+ describe '.charge!' do
278
+
279
+ before do
280
+ stub_post "https://OU812:x@pengwynn.chargify.com/subscriptions/123/charges.json", "charge_subscription.json", 201
281
+ @options = {
282
+ :memo => "This is the description of the one time charge.",
283
+ :amount => 1.00,
284
+ :amount_in_cents => 100
285
+ }
286
+ end
287
+
288
+ it "should accept :amount as a parameter" do
289
+ subscription = Chargify::Subscription.charge!(123, @options)
290
+
291
+ subscription.should be_a(Hashie::Mash)
292
+ subscription.amount_in_cents.should == @options[:amount]*100
293
+ end
294
+
295
+ it "should accept :amount_in_cents as a parameter" do
296
+ subscription = Chargify::Subscription.charge!(123, @options)
297
+
298
+ subscription.should be_a(Hashie::Mash)
299
+ subscription.amount_in_cents.should == @options[:amount_in_cents]
300
+ end
301
+
302
+ it "should have success? as false if parameters are missing" do
303
+ stub_post "https://OU812:x@pengwynn.chargify.com/subscriptions/123/charges.json", "charge_subscription_missing_parameters.json", 422
304
+
305
+ lambda {
306
+ Chargify::Subscription.charge!(123, {})
307
+ }.should raise_error(Chargify::Error::BadRequest)
308
+ end
309
+
310
+ it "should have success? as false if the subscription is not found" do
311
+ stub_post "https://OU812:x@pengwynn.chargify.com/subscriptions/9999/charges.json", "", 404
312
+
313
+ lambda {
314
+ Chargify::Subscription.charge!(9999, @options)
315
+ }.should raise_error(Chargify::Error::NotFound)
316
+ end
317
+
318
+ end
319
+
320
+ describe '.charge' do
321
+
322
+ it "should return false if an error occurs" do
323
+ stub_post "https://OU812:x@pengwynn.chargify.com/subscriptions/123/charges.json", "charge_subscription_missing_parameters.json", 422
324
+ Chargify::Subscription.charge(123, {}).should == false
325
+ end
326
+
327
+ end
328
+
329
+ describe '.migrate!' do
330
+
331
+ it "should migrate a subscription from one product to another" do
332
+ stub_post "https://OU812:x@pengwynn.chargify.com/subscriptions/123/migrations.json", "migrate_subscription.json"
333
+
334
+ subscription = Chargify::Subscription.migrate!(123, 354)
335
+ subscription.should be_a(Hashie::Mash)
336
+ subscription.product.id.should == 354
337
+ end
338
+
339
+ it "should throw an error if one occurs" do
340
+ stub_post "https://OU812:x@pengwynn.chargify.com/subscriptions/123/migrations.json", "", 422
341
+ lambda {
342
+ Chargify::Subscription.migrate!(123, 354)
343
+ }.should raise_error(Chargify::Error::BadRequest)
344
+ end
345
+
346
+ end
347
+
348
+ describe '.migrate' do
349
+
350
+ it "should return false if an error occurs" do
351
+ stub_post "https://OU812:x@pengwynn.chargify.com/subscriptions/123/migrations.json", "", 422
352
+ Chargify::Subscription.migrate(123, 354).should == false
353
+ end
354
+
355
+ end
356
+
357
+ describe '.components!' do
358
+
359
+ it "should list components" do
360
+ stub_get "https://OU812:x@pengwynn.chargify.com/subscriptions/123/components.json", "components.json"
361
+ components = Chargify::Subscription.components(123)
362
+ components.first.allocated_quantity.should == 42
363
+ components.last.allocated_quantity.should == 2
364
+ end
365
+
366
+ it "should throw an error if one occurs"
367
+
368
+ end
369
+
370
+ describe '.components' do
371
+
372
+ it 'should return false if an error occurs'
373
+
374
+ end
375
+
376
+ describe '.find_component!' do
377
+
378
+ it "should show a specific component" do
379
+ stub_get "https://OU812:x@pengwynn.chargify.com/subscriptions/123/components/16.json", "component.json"
380
+ component = Chargify::Subscription.find_component!(123, 16)
381
+ component.name.should == "Extra Rubies"
382
+ component.allocated_quantity.should == 42
383
+ end
384
+
385
+ it "should throw an error if one occurs"
386
+
387
+ end
388
+
389
+ describe '.find_component!' do
390
+
391
+ it 'should return false if an error occurs'
392
+
393
+ end
394
+
395
+ describe '.update_component!' do
396
+
397
+ it "should update the allocated_quantity for a component" do
398
+ stub_put "https://OU812:x@pengwynn.chargify.com/subscriptions/123/components/16.json", "component.json"
399
+ response = Chargify::Subscription.update_component!(123, 16, 20_000_000)
400
+ response.should be_a(Hashie::Mash)
401
+ end
402
+
403
+ it "should throw an error if one occurs"
404
+
405
+ end
406
+
407
+ describe '.update_component' do
408
+
409
+ it 'should return false if an error occurs'
410
+
411
+ end
412
+
413
+ describe '.component_usage!' do
414
+
415
+ it "should list usage for a subscription" do
416
+ stub_get "https://OU812:x@pengwynn.chargify.com/subscriptions/123/components/456/usages.json", "list_metered_subscriptions.json", 200
417
+
418
+ subscription = Chargify::Subscription.component_usage(123, 456)
419
+ subscription.should be_a(Hashie::Mash)
420
+ end
421
+
422
+ it "should throw an error if one occurs"
423
+
424
+ end
425
+
426
+ describe '.component_usage' do
427
+
428
+ it 'should return false if an error occurs'
429
+
430
+ end
431
+
432
+ end
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ describe Chargify::Transaction do
4
+
5
+ describe '.all' do
6
+
7
+ it "should return all transactions"
8
+
9
+ end
10
+
11
+ end
@@ -0,0 +1,102 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{stefl-chargify}
8
+ s.version = "0.3.3"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Wynn Netherland", "Justin Smestad"]
12
+ s.date = %q{2010-10-04}
13
+ s.email = %q{justin.smestad@gmail.com}
14
+ s.extra_rdoc_files = [
15
+ "LICENSE",
16
+ "README.markdown"
17
+ ]
18
+ s.files = [
19
+ ".gitignore",
20
+ ".rspec",
21
+ "Gemfile",
22
+ "Gemfile.lock",
23
+ "LICENSE",
24
+ "README.markdown",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "changelog.md",
28
+ "lib/chargify.rb",
29
+ "lib/chargify/base.rb",
30
+ "lib/chargify/config.rb",
31
+ "lib/chargify/customer.rb",
32
+ "lib/chargify/error.rb",
33
+ "lib/chargify/parser.rb",
34
+ "lib/chargify/product.rb",
35
+ "lib/chargify/product_family.rb",
36
+ "lib/chargify/subscription.rb",
37
+ "lib/chargify/transaction.rb",
38
+ "spec/fixtures/charge_subscription.json",
39
+ "spec/fixtures/charge_subscription_missing_parameters.json",
40
+ "spec/fixtures/component.json",
41
+ "spec/fixtures/components.json",
42
+ "spec/fixtures/customer.json",
43
+ "spec/fixtures/customers.json",
44
+ "spec/fixtures/deleted_subscription.json",
45
+ "spec/fixtures/invalid_subscription.json",
46
+ "spec/fixtures/list_metered_subscriptions.json",
47
+ "spec/fixtures/migrate_subscription.json",
48
+ "spec/fixtures/new_customer.json",
49
+ "spec/fixtures/product.json",
50
+ "spec/fixtures/products.json",
51
+ "spec/fixtures/subscription.json",
52
+ "spec/fixtures/subscription_not_found.json",
53
+ "spec/fixtures/subscriptions.json",
54
+ "spec/spec_helper.rb",
55
+ "spec/support/fakeweb_stubs.rb",
56
+ "spec/unit/chargify/config_spec.rb",
57
+ "spec/unit/chargify/customer_spec.rb",
58
+ "spec/unit/chargify/parser_spec.rb",
59
+ "spec/unit/chargify/product_spec.rb",
60
+ "spec/unit/chargify/subscription_spec.rb",
61
+ "spec/unit/chargify/transaction_spec.rb",
62
+ "stefl-chargify.gemspec"
63
+ ]
64
+ s.homepage = %q{http://github.com/stefl/chargify}
65
+ s.rdoc_options = ["--charset=UTF-8"]
66
+ s.require_paths = ["lib"]
67
+ s.rubygems_version = %q{1.3.7}
68
+ s.summary = %q{Ruby wrapper for the Chargify API}
69
+ s.test_files = [
70
+ "spec/spec_helper.rb",
71
+ "spec/support/fakeweb_stubs.rb",
72
+ "spec/unit/chargify/config_spec.rb",
73
+ "spec/unit/chargify/customer_spec.rb",
74
+ "spec/unit/chargify/parser_spec.rb",
75
+ "spec/unit/chargify/product_spec.rb",
76
+ "spec/unit/chargify/subscription_spec.rb",
77
+ "spec/unit/chargify/transaction_spec.rb"
78
+ ]
79
+
80
+ if s.respond_to? :specification_version then
81
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
82
+ s.specification_version = 3
83
+
84
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
85
+ s.add_runtime_dependency(%q<httparty>, ["~> 0.6.1"])
86
+ s.add_runtime_dependency(%q<hashie>, ["~> 0.4.0"])
87
+ s.add_runtime_dependency(%q<json>, [">= 0"])
88
+ s.add_runtime_dependency(%q<activesupport>, [">= 3.0.0"])
89
+ else
90
+ s.add_dependency(%q<httparty>, ["~> 0.6.1"])
91
+ s.add_dependency(%q<hashie>, ["~> 0.4.0"])
92
+ s.add_dependency(%q<json>, [">= 0"])
93
+ s.add_dependency(%q<activesupport>, [">= 3.0.0"])
94
+ end
95
+ else
96
+ s.add_dependency(%q<httparty>, ["~> 0.6.1"])
97
+ s.add_dependency(%q<hashie>, ["~> 0.4.0"])
98
+ s.add_dependency(%q<json>, [">= 0"])
99
+ s.add_dependency(%q<activesupport>, [">= 3.0.0"])
100
+ end
101
+ end
102
+