chargify_api_ares 0.3.9 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -29,17 +29,9 @@ See the `samples` directory for more usage examples.
29
29
 
30
30
  ### Installation
31
31
 
32
- This library can be installed as a gem. It is hosted on [Gemcutter](http://gemcutter.org).
32
+ This library can be installed as a gem. It is hosted on [Rubygems](http://rubygems.org).
33
33
 
34
- If you don't have your system set up to use gemcutter, follow the instructions on their site
35
- <http://gemcutter.org>, i.e.:
36
-
37
- $ gem install gemcutter
38
- $ gem tumble
39
-
40
- This will install Gemcutter and set your gem sources to search the gemcutter repos.
41
-
42
- Then you can install this library as a gem:
34
+ You can install this library as a gem using the following command:
43
35
 
44
36
  $ gem install chargify_api_ares
45
37
 
@@ -70,11 +62,4 @@ Now you'll have access to classes the interact with the Chargify API, such as:
70
62
  `Chargifiy::Subscription`
71
63
 
72
64
  Check out the examples in the `samples` directory. If you're not familiar with how ActiveResource works,
73
- you may be interested in some [ActiveResource Documentation](http://apidock.com/rails/ActiveResource/Base)
74
-
75
-
76
-
77
- ### Contributors
78
-
79
- * Michael Klett (Grasshopper Labs and Chargify)
80
- * The Lab Rats @ Phase Two Labs
65
+ you may be interested in some [ActiveResource Documentation](http://apidock.com/rails/ActiveResource/Base)
data/Rakefile CHANGED
@@ -1,14 +1,9 @@
1
- begin
2
- require 'jeweler'
3
- Jeweler::Tasks.new do |gemspec|
4
- gemspec.name = "chargify_api_ares"
5
- gemspec.summary = "A Chargify API wrapper for Ruby using ActiveResource"
6
- gemspec.description = ""
7
- gemspec.email = "mklett@grasshopper.com"
8
- gemspec.homepage = "http://github.com/grasshopperlabs/chargify_api_ares"
9
- gemspec.authors = ["Michael Klett", "The Lab Rats @ Phase Two Labs", "Brian Rose","Nathan Verni"]
10
- Jeweler::GemcutterTasks.new
11
- end
12
- rescue LoadError
13
- puts "Jeweler not available. Install it with: sudo gem install jeweler"
14
- end
1
+ require 'rake'
2
+ require 'rspec/core/rake_task'
3
+
4
+ desc 'Run the spec suite'
5
+ RSpec::Core::RakeTask.new('spec') {|t|
6
+ t.rspec_opts = ['--colour', '--format Fuubar']
7
+ }
8
+
9
+ task :default => :spec
@@ -1,17 +1,12 @@
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
1
  Gem::Specification.new do |s|
7
2
  s.name = %q{chargify_api_ares}
8
- s.version = "0.3.9"
3
+ s.version = "0.4.0"
9
4
 
10
5
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = ["Michael Klett", "Nathan Verni", "The Lab Rats @ Phase Two Labs", "Brian Rose"]
12
- s.date = %q{2011-3-2}
6
+ s.authors = ["Michael Klett", "Nathan Verni", "Rodrigo Franco"]
7
+ s.date = %q{2011-03-02}
13
8
  s.description = %q{}
14
- s.email = %q{mklett@grasshopper.com}
9
+ s.email = %q{support@chargify.com}
15
10
  s.extra_rdoc_files = [
16
11
  "LICENSE.txt",
17
12
  "README.md"
@@ -72,4 +67,3 @@ Gem::Specification.new do |s|
72
67
  else
73
68
  end
74
69
  end
75
-
@@ -1,5 +1,7 @@
1
1
  # Chargify API Wrapper using ActiveResource.
2
2
  #
3
+ require 'thread'
4
+
3
5
  begin
4
6
  require 'active_resource'
5
7
  rescue LoadError
@@ -8,7 +10,7 @@ rescue LoadError
8
10
  require 'active_resource'
9
11
  rescue LoadError
10
12
  abort <<-ERROR
11
- The 'activeresource' library could not be loaded. If you have RubyGems
13
+ The 'activeresource' library could not be loaded. If you have RubyGems
12
14
  installed you can install ActiveResource by doing "gem install activeresource".
13
15
  ERROR
14
16
  end
@@ -48,48 +50,51 @@ end
48
50
 
49
51
 
50
52
  module Chargify
51
-
53
+
52
54
  class << self
53
55
  attr_accessor :subdomain, :api_key, :site, :format, :timeout
54
-
56
+
55
57
  def configure
56
58
  yield self
57
-
59
+
58
60
  Base.user = api_key
59
61
  Base.password = 'X'
60
62
  Base.timeout = timeout unless (timeout.blank?)
61
-
63
+
62
64
  self.site ||= "https://#{subdomain}.chargify.com"
63
65
 
64
- Base.site = site
65
- Subscription::Component.site = site + "/subscriptions/:subscription_id"
66
- Subscription::Statement.site = site + "/subscriptions/:subscription_id"
66
+ Base.site = site
67
+ Subscription::Component.site = site + "/subscriptions/:subscription_id"
68
+ Subscription::Statement.site = site + "/subscriptions/:subscription_id"
67
69
  Subscription::Transaction.site = site + "/subscriptions/:subscription_id"
70
+ Coupon.site = site + "/product_families/:product_family_id"
68
71
  end
69
72
  end
70
-
73
+
71
74
  class Base < ActiveResource::Base
75
+ self.format = :xml
76
+
72
77
  def self.element_name
73
78
  name.split(/::/).last.underscore
74
79
  end
75
-
80
+
76
81
  def to_xml(options = {})
77
82
  options.merge!(:dasherize => false)
78
83
  super
79
84
  end
80
85
  end
81
-
86
+
82
87
  class Site < Base
83
88
  def self.clear_data!(params = {})
84
89
  post(:clear_data, params)
85
90
  end
86
91
  end
87
-
92
+
88
93
  class Customer < Base
89
94
  def self.find_by_reference(reference)
90
95
  Customer.new get(:lookup, :reference => reference)
91
96
  end
92
-
97
+
93
98
  def subscriptions(params = {})
94
99
  params.merge!({:customer_id => self.id})
95
100
  Subscription.find(:all, :params => params)
@@ -100,13 +105,13 @@ module Chargify
100
105
  PaymentProfile.find(:all, :params => params)
101
106
  end
102
107
  end
103
-
108
+
104
109
  class Subscription < Base
105
110
  def self.find_by_customer_reference(reference)
106
111
  customer = Customer.find_by_reference(reference)
107
- find(:first, :params => {:customer_id => customer.id})
112
+ find(:first, :params => {:customer_id => customer.id})
108
113
  end
109
-
114
+
110
115
  # Strip off nested attributes of associations before saving, or type-mismatch errors will occur
111
116
  def save
112
117
  self.attributes.delete('customer')
@@ -114,47 +119,47 @@ module Chargify
114
119
  self.attributes.delete('credit_card')
115
120
  super
116
121
  end
117
-
122
+
118
123
  def cancel
119
124
  destroy
120
125
  end
121
-
126
+
122
127
  def component(id)
123
128
  Component.find(id, :params => {:subscription_id => self.id})
124
129
  end
125
-
130
+
126
131
  def components(params = {})
127
132
  params.merge!({:subscription_id => self.id})
128
133
  Component.find(:all, :params => params)
129
134
  end
130
-
135
+
131
136
  def payment_profile
132
137
  credit_card
133
138
  end
134
-
139
+
135
140
  # Perform a one-time charge on an existing subscription.
136
- # For more information, please see the one-time charge API docs available
141
+ # For more information, please see the one-time charge API docs available
137
142
  # at: http://support.chargify.com/faqs/api/api-charges
138
143
  def charge(attrs = {})
139
144
  post :charges, {}, attrs.to_xml(:root => :charge)
140
145
  end
141
-
146
+
142
147
  def credit(attrs = {})
143
148
  post :credits, {}, attrs.to_xml(:root => :credit)
144
149
  end
145
-
150
+
146
151
  def refund(attrs = {})
147
152
  post :refunds, {}, attrs.to_xml(:root => :refund)
148
153
  end
149
-
154
+
150
155
  def reactivate(params = {})
151
156
  put :reactivate, params
152
157
  end
153
-
158
+
154
159
  def reset_balance
155
160
  put :reset_balance
156
161
  end
157
-
162
+
158
163
  def migrate(attrs = {})
159
164
  post :migrations, :migration => attrs
160
165
  end
@@ -164,24 +169,36 @@ module Chargify
164
169
  raise ActiveResource::ResourceNotFound.new(nil) if (statement.subscription_id != self.id)
165
170
  statement
166
171
  end
167
-
172
+
168
173
  def statements(params = {})
169
174
  params.merge!(:subscription_id => self.id)
170
175
  Statement.find(:all, :params => params)
171
176
  end
172
-
177
+
173
178
  def transactions(params = {})
174
179
  params.merge!(:subscription_id => self.id)
175
180
  Transaction.find(:all, :params => params)
176
181
  end
177
-
182
+
183
+ def adjustment(attrs = {})
184
+ post :adjustments, {}, attrs.to_xml(:root => :adjustment)
185
+ end
186
+
187
+ def add_coupon(code)
188
+ post :add_coupon, :code => code
189
+ end
190
+
191
+ def remove_coupon(code)
192
+ delete :remove_coupon, :code => code
193
+ end
194
+
178
195
  class Component < Base
179
196
  # All Subscription Components are considered already existing records, but the id isn't used
180
197
  def id
181
198
  self.component_id
182
199
  end
183
200
  end
184
-
201
+
185
202
  class Statement < Base
186
203
  end
187
204
 
@@ -206,12 +223,12 @@ module Chargify
206
223
  def self.find_by_handle(handle)
207
224
  Product.new get(:lookup, :handle => handle)
208
225
  end
209
-
226
+
210
227
  protected
211
-
228
+
212
229
  # Products are created in the scope of a ProductFamily, i.e. /product_families/nnn/products
213
230
  #
214
- # This alters the collection path such that it uses the product_family_id that is set on the
231
+ # This alters the collection path such that it uses the product_family_id that is set on the
215
232
  # attributes.
216
233
  def create
217
234
  pfid = begin
@@ -225,22 +242,22 @@ module Chargify
225
242
  end
226
243
  end
227
244
  end
228
-
245
+
229
246
  class ProductFamily < Base
230
247
  def self.find_by_handle(handle, attributes = {})
231
248
  ProductFamily.find(:one, :from => :lookup, :handle => handle)
232
249
  end
233
250
  end
234
-
251
+
235
252
  class Usage < Base
236
253
  def subscription_id=(i)
237
254
  self.prefix_options[:subscription_id] = i
238
255
  end
239
256
  def component_id=(i)
240
257
  self.prefix_options[:component_id] = i
241
- end
258
+ end
242
259
  end
243
-
260
+
244
261
  class Component < Base
245
262
  end
246
263
 
@@ -262,8 +279,22 @@ module Chargify
262
279
  Subscription.find(self.subscription_id).refund(attrs)
263
280
  end
264
281
  end
265
-
282
+
266
283
  class PaymentProfile < Base
267
284
  end
268
-
285
+
286
+ class Coupon < Base
287
+ def self.find_by_product_family_id_and_code(product_family_id, code)
288
+ Coupon.new get(:lookup, :product_family_id => product_family_id, :code => code)
289
+ end
290
+
291
+ def usage
292
+ get :usage
293
+ end
294
+
295
+ def archive
296
+ self.destroy
297
+ end
298
+ end
299
+
269
300
  end
@@ -5,7 +5,7 @@ describe Chargify::Customer do
5
5
  context 'find by reference' do
6
6
  before do
7
7
  @reference = 'ref0123'
8
- @existing_customer = Factory(:customer, :reference => @reference, :id => Factory.next(:customer_id))
8
+ @existing_customer = Factory(:customer, :reference => @reference, :id => FactoryGirl.generate(:customer_id))
9
9
  FakeWeb.register_uri(:get, "#{test_domain}/customers/lookup.xml?reference=#{@existing_customer.reference}", :body => @existing_customer.attributes.to_xml)
10
10
  end
11
11
 
data/spec/factories.rb CHANGED
@@ -1,62 +1,64 @@
1
- Factory.sequence :email do |n|
2
- "customer#{n}@example.com"
3
- end
1
+ FactoryGirl.define do
2
+ sequence :email do |n|
3
+ "customer#{n}@example.com"
4
+ end
4
5
 
5
- Factory.sequence :customer_id do |n|
6
- n
7
- end
6
+ sequence :customer_id do |n|
7
+ n
8
+ end
8
9
 
9
- Factory.define :customer, :class => Chargify::Customer do |c|
10
- c.first_name { Faker::Name.first_name }
11
- c.last_name { Faker::Name.last_name }
12
- c.email { Factory.next(:email) }
13
- c.organization { Faker::Company.name }
14
- c.created_at { 2.days.ago }
15
- c.updated_at { 1.hour.ago }
16
- end
10
+ factory :customer, :class => Chargify::Customer do |c|
11
+ c.first_name { Faker::Name.first_name }
12
+ c.last_name { Faker::Name.last_name }
13
+ c.email { FactoryGirl.generate(:email) }
14
+ c.organization { Faker::Company.name }
15
+ c.created_at { 2.days.ago }
16
+ c.updated_at { 1.hour.ago }
17
+ end
17
18
 
18
19
 
19
- Factory.sequence :product_id do |n|
20
- n
21
- end
20
+ sequence :product_id do |n|
21
+ n
22
+ end
22
23
 
23
- Factory.sequence :product_name do |n|
24
- "Product #{n}"
25
- end
24
+ sequence :product_name do |n|
25
+ "Product #{n}"
26
+ end
26
27
 
27
- Factory.define :product, :class => Chargify::Product do |p|
28
- p.name { Factory.next(:product_name) }
29
- end
28
+ factory :product, :class => Chargify::Product do |p|
29
+ p.name { FactoryGirl.generate(:product_name) }
30
+ end
30
31
 
31
- Factory.sequence :subscription_id do |n|
32
- n
33
- end
32
+ sequence :subscription_id do |n|
33
+ n
34
+ end
34
35
 
35
- Factory.define :subscription, :class => Chargify::Subscription do |s|
36
- s.balance_in_cents 500
37
- s.current_period_ends_at 3.days.from_now
38
- end
36
+ factory :subscription, :class => Chargify::Subscription do |s|
37
+ s.balance_in_cents 500
38
+ s.current_period_ends_at 3.days.from_now
39
+ end
39
40
 
40
- Factory.define :subscription_with_extra_attrs, :parent => :subscription do |swea|
41
- swea.customer Chargify::Customer.new
42
- swea.product Chargify::Product.new
43
- swea.credit_card "CREDIT CARD"
44
- end
41
+ factory :subscription_with_extra_attrs, :parent => :subscription do |swea|
42
+ swea.customer Chargify::Customer.new
43
+ swea.product Chargify::Product.new
44
+ swea.credit_card "CREDIT CARD"
45
+ end
45
46
 
46
- Factory.define :component, :class => Chargify::Component do |f|
47
- f.name { Faker::Company.bs }
48
- f.unit_name 'unit'
49
- end
47
+ factory :component, :class => Chargify::Component do |f|
48
+ f.name { Faker::Company.bs }
49
+ f.unit_name 'unit'
50
+ end
50
51
 
51
- Factory.define :quantity_based_component, :class => Chargify::Component do |f|
52
- f.name { Faker::Company.bs }
53
- f.unit_name 'unit'
54
- f.pricing_scheme 'tiered'
55
- f.component_type 'quantity_based_component'
56
- end
52
+ factory :quantity_based_component, :class => Chargify::Component do |f|
53
+ f.name { Faker::Company.bs }
54
+ f.unit_name 'unit'
55
+ f.pricing_scheme 'tiered'
56
+ f.component_type 'quantity_based_component'
57
+ end
57
58
 
58
- Factory.define :subscriptions_component, :class => Chargify::Subscription::Component do |f|
59
- f.name { Faker::Company.bs }
60
- f.unit_name 'unit'
61
- f.component_type 'quantity_based_component'
62
- end
59
+ factory :subscriptions_component, :class => Chargify::Subscription::Component do |f|
60
+ f.name { Faker::Company.bs }
61
+ f.unit_name 'unit'
62
+ f.component_type 'quantity_based_component'
63
+ end
64
+ end
data/spec/product_spec.rb CHANGED
@@ -5,7 +5,7 @@ describe Chargify::Product do
5
5
  context 'find by handle' do
6
6
  before do
7
7
  @handle = 'handle1'
8
- @existing_product = Factory(:product, :handle => @handle, :id => Factory.next(:product_id))
8
+ @existing_product = Factory(:product, :handle => @handle, :id => FactoryGirl.generate(:product_id))
9
9
  FakeWeb.register_uri(:get, "#{test_domain}/products/lookup.xml?handle=#{@existing_product.handle}", :body => @existing_product.attributes.to_xml)
10
10
  end
11
11
 
@@ -1,11 +1,11 @@
1
1
  require 'rubygems'
2
- require 'spec'
2
+ require 'rspec'
3
3
  $LOAD_PATH.unshift(File.dirname(__FILE__))
4
4
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', '..', 'lib'))
5
5
 
6
6
  require 'chargify_api_ares'
7
7
 
8
- Spec::Runner.configure do |config|
8
+ RSpec.configure do |config|
9
9
  config.before(:all) do
10
10
  Chargify.configure do |c|
11
11
  c.subdomain = remote_configuration['subdomain']
data/spec/spec_helper.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  require 'rubygems'
2
- require 'spec'
2
+ require 'rspec'
3
3
  $LOAD_PATH.unshift(File.dirname(__FILE__))
4
4
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
5
5
 
@@ -18,7 +18,7 @@ Chargify.configure do |c|
18
18
  c.api_key = 'test'
19
19
  end
20
20
 
21
- Spec::Runner.configure do |config|
21
+ RSpec.configure do |config|
22
22
  config.after(:each) do
23
23
  ActiveResource::FakeResource.clean
24
24
  end
@@ -4,7 +4,7 @@ describe Chargify::Subscription do
4
4
 
5
5
  context 'strips nested association attributes before saving' do
6
6
  before do
7
- @subscription = Factory.build(:subscription_with_extra_attrs)
7
+ @subscription = FactoryGirl.build(:subscription_with_extra_attrs)
8
8
  end
9
9
 
10
10
  it 'strips customer' do
@@ -26,17 +26,18 @@ describe Chargify::Subscription do
26
26
  end
27
27
 
28
28
  it 'doesn\'t strip other attrs' do
29
- subscription = Factory.build(:subscription)
29
+ subscription = FactoryGirl.build(:subscription)
30
30
 
31
31
  lambda { subscription.save! }.should_not change(subscription, :attributes)
32
32
  end
33
33
  end
34
34
 
35
35
  it 'creates a one-time charge' do
36
- id = Factory.next(:subscription_id)
36
+ id = FactoryGirl.generate(:subscription_id)
37
37
  subscription = Factory(:subscription, :id => id)
38
+ subscription.stub!(:persisted?).and_return(true)
38
39
  expected_response = {:charge => {:amount_in_cents => 1000, :memo => "one-time charge", :success => true}}.to_xml
39
- FakeWeb.register_uri(:post, "#{test_domain}/subscriptions/#{id}/charges.xml?charge%5Bamount%5D=10.00&charge%5Bmemo%5D=one-time+charge", :status => 201, :body => expected_response)
40
+ FakeWeb.register_uri(:post, "#{test_domain}/subscriptions/#{id}/charges.xml", :status => 201, :body => expected_response)
40
41
 
41
42
  response = subscription.charge(:amount => "10.00", "memo" => "one-time charge")
42
43
 
@@ -59,14 +60,17 @@ describe Chargify::Subscription do
59
60
  @subscription = Factory(:subscription, :id => 1)
60
61
  find_subscription = lambda { Chargify::Subscription.find(1) }
61
62
 
63
+ FakeWeb.register_uri(:get, "#{test_domain}/subscriptions/1.xml", :body => @subscription.attributes.to_xml)
64
+
62
65
  find_subscription.should_not raise_error
63
66
  @subscription.cancel
64
67
  find_subscription.should raise_error
65
68
  end
66
69
 
67
70
  it 'migrates the subscription' do
68
- id = Factory.next(:subscription_id)
71
+ id = FactoryGirl.generate(:subscription_id)
69
72
  subscription = Factory(:subscription, :id => id)
73
+ subscription.stub!(:persisted?).and_return(true)
70
74
  expected_response = [subscription.attributes].to_xml(:root => 'subscription')
71
75
  FakeWeb.register_uri(:post, "#{test_domain}/subscriptions/#{id}/migrations.xml?migration%5Bproduct_handle%5D=upgraded-plan", :status => 201, :body => expected_response)
72
76
 
metadata CHANGED
@@ -1,29 +1,28 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chargify_api_ares
3
3
  version: !ruby/object:Gem::Version
4
- hash: 1
5
- prerelease: false
4
+ hash: 15
5
+ prerelease:
6
6
  segments:
7
7
  - 0
8
- - 3
9
- - 9
10
- version: 0.3.9
8
+ - 4
9
+ - 0
10
+ version: 0.4.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Michael Klett
14
14
  - Nathan Verni
15
- - The Lab Rats @ Phase Two Labs
16
- - Brian Rose
15
+ - Rodrigo Franco
17
16
  autorequire:
18
17
  bindir: bin
19
18
  cert_chain: []
20
19
 
21
- date: 2011-03-02 00:00:00 -05:00
20
+ date: 2011-03-02 00:00:00 -08:00
22
21
  default_executable:
23
22
  dependencies: []
24
23
 
25
24
  description: ""
26
- email: mklett@grasshopper.com
25
+ email: support@chargify.com
27
26
  executables: []
28
27
 
29
28
  extensions: []
@@ -87,7 +86,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
87
86
  requirements: []
88
87
 
89
88
  rubyforge_project:
90
- rubygems_version: 1.3.7
89
+ rubygems_version: 1.6.2
91
90
  signing_key:
92
91
  specification_version: 3
93
92
  summary: A Chargify API wrapper for Ruby using ActiveResource