chargify_api_ares 1.0.4 → 1.0.5

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 068fdf18d68304e12298e546b2c2d8d6a74edd2f
4
- data.tar.gz: 93999e1561575a8a3ee53bfdcc4975bf1d84bf1f
3
+ metadata.gz: 2caae8888bb7f0c0d3b233532c05ce0d38276965
4
+ data.tar.gz: d7924e0dfccd7f0077f06a78e6779a87b7f65c40
5
5
  SHA512:
6
- metadata.gz: 07cddfce56890486b484d5a3f12d4acad608cdabcef31788f713ce74a75e4a3db3fa55cb9d2b4036317d5fb515162a365120856871fb6b3f64c8ee51e6866fc3
7
- data.tar.gz: f41b1837ce9b83b4f38f1dc913f0d45f2370638a564d70c162824d5bac47de23d36b6f4d9fa5ab400c015c2701f3743dcf8198b4a04a2e08ee257cd37fcf2f0b
6
+ metadata.gz: 13c421925c09b7062a12ac605d97bf2e02dcef6b8dbd45640bb8134253f7ed217cc2489667680266f5bd53ce0b708170dfe8a5fbd0cef2016f9ae9abf40088a1
7
+ data.tar.gz: 9c9ad7f26b9f0e1900ad026b464b95a8507949f264e0289d0d8d1a66f28da7f7352c7f20d3ab80f89394e680783a89d23f7a293d32b31271e1e3512a892329c1
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- chargify_api_ares (1.0.3)
4
+ chargify_api_ares (1.0.5)
5
5
  activeresource (>= 3.0.0)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -100,6 +100,27 @@ subscription.save
100
100
  subscription.cancel
101
101
  ```
102
102
 
103
+ ##### Note
104
+
105
+ Updating nested resources is _not_ supported nor recommended. If you wish to update a subscriptions customer please do so by updating the customer object itself.
106
+
107
+ Bad:
108
+
109
+ ```ruby
110
+ subscription = Chargify::Subscription.find(123)
111
+ subscription.customer.first_name = 'fred'
112
+ subscription.customer.save
113
+ ```
114
+
115
+ Good:
116
+
117
+ ```ruby
118
+ subscription = Chargify::Subscription.find(123)
119
+ customer = Chargify::Customer.find(subscription.customer.id)
120
+ customer.first_name = 'fred'
121
+ customer.save
122
+ ```
123
+
103
124
  Check out the examples in the `examples` directory. If you're not familiar with how ActiveResource works, you may be interested in some [ActiveResource Documentation](http://apidock.com/rails/ActiveResource/Base)
104
125
 
105
126
  ### Compatibility
@@ -4,7 +4,7 @@ Gem::Specification.new do |s|
4
4
  s.rubygems_version = '1.3.7'
5
5
 
6
6
  s.name = 'chargify_api_ares'
7
- s.version = '1.0.4'
7
+ s.version = '1.0.5'
8
8
  s.date = '2014-01-21'
9
9
  s.summary = 'A Chargify API wrapper for Ruby using ActiveResource'
10
10
  s.description = ''
@@ -1,5 +1,6 @@
1
1
  require 'active_resource'
2
2
  require 'chargify_api_ares/config'
3
+ require 'chargify_api_ares/response_helper'
3
4
  require 'chargify_api_ares/resources/base'
4
5
  require 'chargify_api_ares/resources/charge'
5
6
  require 'chargify_api_ares/resources/component'
@@ -1,9 +1,11 @@
1
1
  module Chargify
2
2
  class Coupon < Base
3
+ include ResponseHelper
4
+
3
5
  def self.find_all_by_product_family_id(product_family_id)
4
6
  Coupon.find(:all, :params => { :product_family_id => product_family_id })
5
7
  end
6
-
8
+
7
9
  def self.find_by_product_family_id_and_code(product_family_id, code)
8
10
  find(:one, :from => :lookup, :params => {:product_family_id => product_family_id, :code => code})
9
11
  end
@@ -21,7 +23,9 @@ module Chargify
21
23
  end
22
24
 
23
25
  def usage
24
- get :usage
26
+ process_capturing_errors do
27
+ get :usage
28
+ end
25
29
  end
26
30
 
27
31
  def archive
@@ -1,6 +1,6 @@
1
1
  module Chargify
2
2
  class Invoice < Base
3
-
3
+
4
4
  def self.find_by_invoice_id(id)
5
5
  find(:first, params: {id: id})
6
6
  end
@@ -8,7 +8,7 @@ module Chargify
8
8
  def self.find_by_subscription_id(id)
9
9
  find(:all, params: {subscription_id: id})
10
10
  end
11
-
11
+
12
12
  def self.unpaid_from_subscription(subscription_id)
13
13
  find(:all, params: {subscription_id: subscription_id, state: "unpaid"})
14
14
  end
@@ -17,4 +17,4 @@ module Chargify
17
17
  find(:all, params: {state: "unpaid"})
18
18
  end
19
19
  end
20
- end
20
+ end
@@ -3,7 +3,7 @@ module Chargify
3
3
  def self.find_by_handle(handle)
4
4
  find(:one, :from => :lookup, :params => {:handle => handle})
5
5
  end
6
-
6
+
7
7
  protected
8
8
 
9
9
  # Products are created in the scope of a ProductFamily, i.e. /product_families/nnn/products
@@ -3,11 +3,11 @@ module Chargify
3
3
  def self.find_by_handle(handle, attributes = {})
4
4
  ProductFamily.find(:one, :from => :lookup, :params => { :handle => handle })
5
5
  end
6
-
6
+
7
7
  class Product < Base
8
8
  self.prefix = "/product_families/:product_family_id/"
9
9
  end
10
-
10
+
11
11
  class Component < Base
12
12
  self.prefix = "/product_families/:product_family_id/"
13
13
 
@@ -39,16 +39,16 @@ module Chargify
39
39
  "#{self.component_kind}s"
40
40
  end
41
41
  end
42
-
42
+
43
43
  class Coupon < Base
44
44
  self.prefix = "/product_families/:product_family_id/"
45
45
  end
46
-
46
+
47
47
  def products(params = {})
48
48
  params.merge!(:product_family_id => self.id)
49
49
  ::Chargify::ProductFamily::Product.find(:all, :params => params)
50
50
  end
51
-
51
+
52
52
  def components(params = {})
53
53
  params.merge!({:product_family_id => self.id})
54
54
  ::Chargify::ProductFamily::Component.find(:all, :params => params)
@@ -1,5 +1,7 @@
1
1
  module Chargify
2
2
  class Subscription < Base
3
+ include ResponseHelper
4
+
3
5
  def self.find_by_customer_reference(reference)
4
6
  customer = Customer.find_by_reference(reference)
5
7
  find(:first, :params => {:customer_id => customer.id})
@@ -25,7 +27,7 @@ module Chargify
25
27
  params.merge!({:subscription_id => self.id})
26
28
  Component.find(:all, :params => params)
27
29
  end
28
-
30
+
29
31
  def events(params = {})
30
32
  params.merge!(:subscription_id => self.id)
31
33
  Event.all(:params => params)
@@ -43,19 +45,27 @@ module Chargify
43
45
  end
44
46
 
45
47
  def credit(attrs = {})
46
- post :credits, {}, attrs.to_xml(:root => :credit)
48
+ process_capturing_errors do
49
+ post :credits, {}, attrs.to_xml(:root => :credit)
50
+ end
47
51
  end
48
52
 
49
53
  def refund(attrs = {})
50
- post :refunds, {}, attrs.to_xml(:root => :refund)
54
+ process_capturing_errors do
55
+ post :refunds, {}, attrs.to_xml(:root => :refund)
56
+ end
51
57
  end
52
58
 
53
59
  def reactivate(params = {})
54
- put :reactivate, params
60
+ process_capturing_errors do
61
+ put :reactivate, params
62
+ end
55
63
  end
56
64
 
57
65
  def reset_balance
58
- put :reset_balance
66
+ process_capturing_errors do
67
+ put :reset_balance
68
+ end
59
69
  end
60
70
 
61
71
  def migrate(attrs = {})
@@ -79,21 +89,29 @@ module Chargify
79
89
  end
80
90
 
81
91
  def adjustment(attrs = {})
82
- post :adjustments, {}, attrs.to_xml(:root => :adjustment)
92
+ process_capturing_errors do
93
+ post :adjustments, {}, attrs.to_xml(:root => :adjustment)
94
+ end
83
95
  end
84
96
 
85
97
  def add_coupon(code)
86
- post :add_coupon, :code => code
98
+ process_capturing_errors do
99
+ post :add_coupon, :code => code
100
+ end
87
101
  end
88
102
 
89
103
  def remove_coupon(code=nil)
90
- if code.nil?
91
- delete :remove_coupon
92
- else
93
- delete :remove_coupon, :code => code
104
+ process_capturing_errors do
105
+ if code.nil?
106
+ delete :remove_coupon
107
+ else
108
+ delete :remove_coupon, :code => code
109
+ end
94
110
  end
95
111
  end
96
112
 
113
+ private
114
+
97
115
  class Component < Base
98
116
  self.prefix = "/subscriptions/:subscription_id/"
99
117
 
@@ -106,7 +124,7 @@ module Chargify
106
124
  class Event < Base
107
125
  self.prefix = '/subscriptions/:subscription_id/'
108
126
  end
109
-
127
+
110
128
  class Statement < Base
111
129
  self.prefix = "/subscriptions/:subscription_id/"
112
130
  end
@@ -3,7 +3,7 @@ module Chargify
3
3
  def subscription_id=(i)
4
4
  self.prefix_options[:subscription_id] = i
5
5
  end
6
-
6
+
7
7
  def component_id=(i)
8
8
  self.prefix_options[:component_id] = i
9
9
  end
@@ -1,5 +1,6 @@
1
1
  module Chargify
2
2
  class Webhook < Base
3
+
3
4
  def self.replay(webhook_ids_array)
4
5
  post :replay, {}, webhook_ids_array.to_xml(:root => :ids)
5
6
  end
@@ -8,4 +9,4 @@ module Chargify
8
9
  self.class.replay([self.id])
9
10
  end
10
11
  end
11
- end
12
+ end
@@ -0,0 +1,17 @@
1
+ module Chargify
2
+ module ResponseHelper
3
+ private
4
+ def process_capturing_errors(&block)
5
+ begin
6
+ yield if block_given?
7
+ rescue ActiveResource::ResourceInvalid => error
8
+ if :xml == Chargify.format.to_sym
9
+ self.errors.from_xml(error.response.body)
10
+ else
11
+ self.errors.from_json(error.response.body)
12
+ end
13
+ end
14
+ self
15
+ end
16
+ end
17
+ end
@@ -235,6 +235,25 @@ describe "Remote" do
235
235
  end
236
236
  end
237
237
 
238
+ describe 'failing to reactivate a subscription' do
239
+ before(:all) do
240
+ @reactivated_subscription = Chargify::Subscription.create(
241
+ :product_handle => pro_plan.handle,
242
+ :customer_reference => johnadoe.reference,
243
+ :payment_profile_attributes => good_payment_profile_attributes)
244
+
245
+ @result = @reactivated_subscription.reactivate
246
+ end
247
+
248
+ it "is not valid after a reactivation" do
249
+ expect(@result.errors.any?).to be_true
250
+ end
251
+
252
+ it 'has errors when the reactivation fails' do
253
+ expect(@result.errors.full_messages.first).to eql 'Cannot reactivate a subscription that is not marked "Canceled", "Unpaid", or "Trial Ended".'
254
+ end
255
+ end
256
+
238
257
  describe "adding a one time charge" do
239
258
  before(:all) do
240
259
  @subscription = Chargify::Subscription.create(
@@ -257,7 +276,7 @@ describe "Remote" do
257
276
  :product_handle => basic_plan.handle,
258
277
  :customer_reference => johnadoe.reference,
259
278
  :payment_profile_attributes => declined_payment_profile_attributes)
260
-
279
+
261
280
  @charge = @subscription.charge(:amount => 7, :memo => 'One Time Charge')
262
281
  end
263
282
 
@@ -269,15 +288,15 @@ describe "Remote" do
269
288
  expect(@charge.errors.full_messages.first).to eql "Bogus Gateway: Forced failure"
270
289
  end
271
290
  end
272
-
291
+
273
292
  describe "migrating a subscription to a valid product" do
274
293
  before(:all) do
275
294
  @subscription = Chargify::Subscription.create(
276
295
  :product_handle => basic_plan.handle,
277
296
  :customer_reference => johnadoe.reference,
278
297
  :payment_profile_attributes => good_payment_profile_attributes)
279
-
280
- @migration = @subscription.migrate(:product_handle => pro_plan.handle)
298
+
299
+ @migration = @subscription.migrate(:product_handle => pro_plan.handle)
281
300
  end
282
301
 
283
302
  it "is valid when the migration is successful" do
@@ -287,7 +306,7 @@ describe "Remote" do
287
306
  it "migrates the product" do
288
307
  expect(@migration.subscription.product.handle).to eql "pro"
289
308
  end
290
-
309
+
291
310
  it "has a subscription" do
292
311
  expect(@migration.subscription).to_not be_nil
293
312
  end
@@ -299,18 +318,18 @@ describe "Remote" do
299
318
  :product_handle => basic_plan.handle,
300
319
  :customer_reference => johnadoe.reference,
301
320
  :payment_profile_attributes => good_payment_profile_attributes)
302
-
303
- @migration = @subscription.migrate(:product_handle => "a-bad-handle")
321
+
322
+ @migration = @subscription.migrate(:product_handle => "a-bad-handle")
304
323
  end
305
324
 
306
325
  it "is invalid when the migration is not successful" do
307
326
  expect(@migration).to_not be_valid
308
327
  end
309
-
328
+
310
329
  it "is has errors when the migration is not successful" do
311
330
  expect(@migration.errors.full_messages.first).to eql "Invalid Product"
312
331
  end
313
-
332
+
314
333
  it "will not have a subscription" do
315
334
  expect(@migration.subscription).to be_nil
316
335
  end
@@ -322,7 +341,7 @@ describe "Remote" do
322
341
  :product_handle => basic_plan.handle,
323
342
  :customer_reference => johnadoe.reference,
324
343
  :payment_profile_attributes => good_payment_profile_attributes)
325
- @preview = Chargify::Migration.preview(:subscription_id => @subscription.id, :product_handle => pro_plan.handle)
344
+ @preview = Chargify::Migration.preview(:subscription_id => @subscription.id, :product_handle => pro_plan.handle)
326
345
  end
327
346
 
328
347
  it "is valid when the migration preview is successful" do
@@ -333,14 +352,14 @@ describe "Remote" do
333
352
  expect(@preview.charge_in_cents).to eql "5000"
334
353
  end
335
354
  end
336
-
355
+
337
356
  describe "previewing a valid migration via Chargify::Migration::Preview" do
338
357
  before(:all) do
339
358
  @subscription = Chargify::Subscription.create(
340
359
  :product_handle => basic_plan.handle,
341
360
  :customer_reference => johnadoe.reference,
342
361
  :payment_profile_attributes => good_payment_profile_attributes)
343
- @preview = Chargify::Migration::Preview.create(:subscription_id => @subscription.id, :product_handle => pro_plan.handle)
362
+ @preview = Chargify::Migration::Preview.create(:subscription_id => @subscription.id, :product_handle => pro_plan.handle)
344
363
  end
345
364
 
346
365
  it "is valid when the migration preview is successful" do
@@ -351,7 +370,7 @@ describe "Remote" do
351
370
  expect(@preview.charge_in_cents).to eql "5000"
352
371
  end
353
372
  end
354
-
373
+
355
374
  describe "previewing an invalid migration via Chargify::Migration" do
356
375
  before(:all) do
357
376
  @subscription = Chargify::Subscription.create(
@@ -359,7 +378,7 @@ describe "Remote" do
359
378
  :customer_reference => johnadoe.reference,
360
379
  :payment_profile_attributes => good_payment_profile_attributes)
361
380
 
362
- @preview = Chargify::Migration.preview(:subscription_id => @subscription.id, :product_id => 9999999)
381
+ @preview = Chargify::Migration.preview(:subscription_id => @subscription.id, :product_id => 9999999)
363
382
  end
364
383
 
365
384
  it "is invalid when the migration preview is invalid" do
@@ -370,7 +389,7 @@ describe "Remote" do
370
389
  expect(@preview.errors.full_messages.first).to eql "Product must be specified"
371
390
  end
372
391
  end
373
-
392
+
374
393
  describe "previewing an invalid migration via Chargify::Migration::Preview" do
375
394
  before(:all) do
376
395
  @subscription = Chargify::Subscription.create(
@@ -378,7 +397,7 @@ describe "Remote" do
378
397
  :customer_reference => johnadoe.reference,
379
398
  :payment_profile_attributes => good_payment_profile_attributes)
380
399
 
381
- @preview = Chargify::Migration::Preview.create(:subscription_id => @subscription.id, :product_id => 9999999)
400
+ @preview = Chargify::Migration::Preview.create(:subscription_id => @subscription.id, :product_id => 9999999)
382
401
  end
383
402
 
384
403
  it "is invalid when the migration preview is invalid" do
@@ -389,7 +408,7 @@ describe "Remote" do
389
408
  expect(@preview.errors.full_messages.first).to eql "Product must be specified"
390
409
  end
391
410
  end
392
-
411
+
393
412
  describe "adding a credit" do
394
413
  before(:all) do
395
414
  @subscription = Chargify::Subscription.create(
@@ -404,6 +423,11 @@ describe "Remote" do
404
423
  }.should change{@subscription.reload.transactions.size}.by(1)
405
424
  most_recent_transaction(@subscription).amount_in_cents.should == -700
406
425
  end
426
+
427
+ it 'responds with errors when request is invalid' do
428
+ response = @subscription.credit(:amount => nil)
429
+ expect(response.errors.full_messages.first).to eql "Amount in cents: is not a number."
430
+ end
407
431
  end
408
432
 
409
433
  describe "adding a refund" do
@@ -422,6 +446,12 @@ describe "Remote" do
422
446
  end
423
447
 
424
448
  context "via Chargify::Subscription#refund" do
449
+
450
+ it 'responds with an error if params are not present' do
451
+ response = @subscription.refund(:payment_id => @payment.id)
452
+ expect(response.errors.full_messages.first).to eql("Memo: cannot be blank.")
453
+ end
454
+
425
455
  it "creates a refund" do
426
456
  lambda{
427
457
  @subscription.refund :payment_id => @payment.id, :amount => 7,
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chargify_api_ares
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.4
4
+ version: 1.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Klett
@@ -198,6 +198,7 @@ files:
198
198
  - lib/chargify_api_ares/resources/transaction.rb
199
199
  - lib/chargify_api_ares/resources/usage.rb
200
200
  - lib/chargify_api_ares/resources/webhook.rb
201
+ - lib/chargify_api_ares/response_helper.rb
201
202
  - lib/patches/activemodel_3_0_patch.rb
202
203
  - spec/factories.rb
203
204
  - spec/remote/remote.example.yml
@@ -238,7 +239,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
238
239
  version: '0'
239
240
  requirements: []
240
241
  rubyforge_project:
241
- rubygems_version: 2.2.1
242
+ rubygems_version: 2.2.2
242
243
  signing_key:
243
244
  specification_version: 3
244
245
  summary: A Chargify API wrapper for Ruby using ActiveResource
@@ -263,4 +264,3 @@ test_files:
263
264
  - spec/spec.opts
264
265
  - spec/spec_helper.rb
265
266
  - spec/support/fake_resource.rb
266
- has_rdoc: