chargify 0.2.3 → 0.2.4

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/Rakefile CHANGED
@@ -1,60 +1,15 @@
1
- require 'rake'
1
+ require "mg"
2
+ require 'active_support/inflector'
2
3
  require 'shoulda/tasks'
3
4
 
4
- begin
5
- require 'jeweler'
6
- Jeweler::Tasks.new do |gem|
7
- gem.name = "chargify"
8
- gem.summary = %Q{Ruby wrapper for the chargify.com SAAS and billing API}
9
- gem.description = %Q{Ruby wrapper for the chargify.com SAAS and billing API}
10
- gem.email = "wynn.netherland@gmail.com"
11
- gem.homepage = "http://github.com/pengwynn/chargify"
12
- gem.authors = ["Wynn Netherland","Nash Kabbara"]
13
-
14
- gem.add_dependency('hashie', '~> 0.1.3')
15
- gem.add_dependency('httparty', '~> 0.4.5')
16
5
 
17
- gem.add_development_dependency('thoughtbot-shoulda', '>= 2.10.1')
18
- gem.add_development_dependency('jnunemaker-matchy', '0.4.0')
19
- gem.add_development_dependency('mocha', '0.9.4')
20
- gem.add_development_dependency('fakeweb', '>= 1.2.5')
21
- # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
22
- end
23
- Jeweler::GemcutterTasks.new
24
- rescue LoadError
25
- puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
26
- end
6
+ MG.new("chargify.gemspec")
27
7
 
28
8
  require 'rake/testtask'
29
9
  Rake::TestTask.new(:test) do |test|
10
+ test.ruby_opts = ['-rubygems'] if defined? Gem
30
11
  test.libs << 'lib' << 'test'
31
- test.pattern = 'test/**/test_*.rb'
32
- test.verbose = true
33
- end
34
-
35
- begin
36
- require 'rcov/rcovtask'
37
- Rcov::RcovTask.new do |test|
38
- test.libs << 'test'
39
- test.pattern = 'test/**/test_*.rb'
40
- test.verbose = true
41
- end
42
- rescue LoadError
43
- task :rcov do
44
- abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
45
- end
12
+ test.pattern = 'test/**/*_test.rb'
46
13
  end
47
14
 
48
- task :test => :check_dependencies
49
-
50
15
  task :default => :test
51
-
52
- require 'rake/rdoctask'
53
- Rake::RDocTask.new do |rdoc|
54
- version = File.exist?('VERSION') ? File.read('VERSION') : ""
55
-
56
- rdoc.rdoc_dir = 'rdoc'
57
- rdoc.title = "chargify #{version}"
58
- rdoc.rdoc_files.include('README*')
59
- rdoc.rdoc_files.include('lib/**/*.rb')
60
- end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.3
1
+ 0.2.4
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{chargify}
8
- s.version = "0.2.3"
8
+ s.version = "0.2.4"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Wynn Netherland", "Nash Kabbara"]
@@ -38,7 +38,7 @@ Gem::Specification.new do |s|
38
38
  "test/fixtures/subscription_not_found.json",
39
39
  "test/fixtures/subscriptions.json",
40
40
  "test/helper.rb",
41
- "test/test_chargify.rb"
41
+ "test/chargify_test.rb"
42
42
  ]
43
43
  s.homepage = %q{http://github.com/pengwynn/chargify}
44
44
  s.rdoc_options = ["--charset=UTF-8"]
@@ -47,7 +47,7 @@ Gem::Specification.new do |s|
47
47
  s.summary = %q{Ruby wrapper for the chargify.com SAAS and billing API}
48
48
  s.test_files = [
49
49
  "test/helper.rb",
50
- "test/test_chargify.rb"
50
+ "test/chargify_test.rb"
51
51
  ]
52
52
 
53
53
  if s.respond_to? :specification_version then
@@ -56,25 +56,25 @@ Gem::Specification.new do |s|
56
56
 
57
57
  if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
58
58
  s.add_runtime_dependency(%q<hashie>, ["~> 0.1.3"])
59
- s.add_runtime_dependency(%q<httparty>, ["~> 0.4.5"])
60
- s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 2.10.1"])
59
+ s.add_runtime_dependency(%q<httparty>, ["~> 0.5.2"])
60
+ s.add_development_dependency(%q<shoulda>, [">= 2.10.1"])
61
61
  s.add_development_dependency(%q<jnunemaker-matchy>, ["= 0.4.0"])
62
- s.add_development_dependency(%q<mocha>, ["= 0.9.4"])
62
+ s.add_development_dependency(%q<mocha>, ["~> 0.9.8"])
63
63
  s.add_development_dependency(%q<fakeweb>, [">= 1.2.5"])
64
64
  else
65
65
  s.add_dependency(%q<hashie>, ["~> 0.1.3"])
66
- s.add_dependency(%q<httparty>, ["~> 0.4.5"])
67
- s.add_dependency(%q<thoughtbot-shoulda>, [">= 2.10.1"])
66
+ s.add_dependency(%q<httparty>, ["~> 0.5.2"])
67
+ s.add_dependency(%q<shoulda>, [">= 2.10.1"])
68
68
  s.add_dependency(%q<jnunemaker-matchy>, ["= 0.4.0"])
69
- s.add_dependency(%q<mocha>, ["= 0.9.4"])
69
+ s.add_dependency(%q<mocha>, ["~> 0.9.8"])
70
70
  s.add_dependency(%q<fakeweb>, [">= 1.2.5"])
71
71
  end
72
72
  else
73
73
  s.add_dependency(%q<hashie>, ["~> 0.1.3"])
74
- s.add_dependency(%q<httparty>, ["~> 0.4.5"])
75
- s.add_dependency(%q<thoughtbot-shoulda>, [">= 2.10.1"])
74
+ s.add_dependency(%q<httparty>, ["~> 0.5.2"])
75
+ s.add_dependency(%q<shoulda>, [">= 2.10.1"])
76
76
  s.add_dependency(%q<jnunemaker-matchy>, ["= 0.4.0"])
77
- s.add_dependency(%q<mocha>, ["= 0.9.4"])
77
+ s.add_dependency(%q<mocha>, ["~> 0.9.8"])
78
78
  s.add_dependency(%q<fakeweb>, [">= 1.2.5"])
79
79
  end
80
80
  end
@@ -1,9 +1,13 @@
1
1
  require 'hashie'
2
2
  require 'httparty'
3
- require "json"
3
+ require 'json/pure'
4
4
 
5
5
  directory = File.expand_path(File.dirname(__FILE__))
6
6
 
7
7
  Hash.send :include, Hashie::HashExtensions
8
8
 
9
9
  require File.join(directory, 'chargify', 'client')
10
+
11
+ module Chargify
12
+ VERSION = "0.2.4".freeze
13
+ end
@@ -1,21 +1,21 @@
1
1
  module Chargify
2
- class UnexpectedResponseError < RuntimeError;end
3
-
4
- def self.custom_parser
5
- proc do |data|
2
+ class UnexpectedResponseError < RuntimeError
3
+ end
4
+
5
+ class Parser < HTTParty::Parser
6
+ def parse
6
7
  begin
7
- Crack::JSON.parse(data)
8
+ Crack::JSON.parse(body)
8
9
  rescue => e
9
- error_msg = "Crack could not parse JSON. It said: #{e.message}. Chargify's raw response: #{data}"
10
- raise UnexpectedResponseError, error_msg
10
+ raise UnexpectedResponseError, "Crack could not parse JSON. It said: #{e.message}. Chargify's raw response: #{body}"
11
11
  end
12
12
  end
13
13
  end
14
-
14
+
15
15
  class Client
16
16
  include HTTParty
17
- format :json
18
- parser Chargify::custom_parser
17
+
18
+ parser Chargify::Parser
19
19
  headers 'Content-Type' => 'application/json'
20
20
 
21
21
  attr_reader :api_key, :subdomain
@@ -36,10 +36,23 @@ module Chargify
36
36
  customers.map{|c| Hashie::Mash.new c['customer']}
37
37
  end
38
38
 
39
- def customer(chargify_id)
40
- Hashie::Mash.new(self.class.get("/customers/lookup.json?reference=#{chargify_id}")).customer
39
+ def customer_by_id(chargify_id)
40
+ request = self.class.get("/customers/#{chargify_id}.json")
41
+ success = request.code == 200
42
+ response = Hashie::Mash.new(request).customer if success
43
+ Hashie::Mash.new(response || {}).update(:success? => success)
44
+ end
45
+
46
+ def customer_by_reference(reference_id)
47
+ request = self.class.get("/customers/lookup.json?reference=#{reference_id}")
48
+ success = request.code == 200
49
+ response = Hashie::Mash.new(request).customer if success
50
+ Hashie::Mash.new(response || {}).update(:success? => success)
41
51
  end
42
52
 
53
+ alias customer customer_by_reference
54
+
55
+
43
56
  #
44
57
  # * first_name (Required)
45
58
  # * last_name (Required)
@@ -73,6 +86,8 @@ module Chargify
73
86
  subscriptions.map{|s| Hashie::Mash.new s['subscription']}
74
87
  end
75
88
 
89
+
90
+
76
91
  def subscription(subscription_id)
77
92
  raw_response = self.class.get("/subscriptions/#{subscription_id}.json")
78
93
  return nil if raw_response.code != 200
@@ -112,6 +127,24 @@ module Chargify
112
127
  response = Hashie::Mash.new(raw_response) rescue Hashie::Mash.new
113
128
  (response.subscription || response).update(:success? => reactivated)
114
129
  end
130
+
131
+ def charge_subscription(sub_id, subscription_attributes={})
132
+ raw_response = self.class.post("/subscriptions/#{sub_id}/charges.json", :body => { :charge => subscription_attributes })
133
+ success = raw_response.code == 201
134
+ if raw_response.code == 404
135
+ raw_response = {}
136
+ end
137
+
138
+ response = Hashie::Mash.new(raw_response)
139
+ (response.charge || response).update(:success? => success)
140
+ end
141
+
142
+ def migrate_subscription(sub_id, product_id)
143
+ raw_response = self.class.post("/subscriptions/#{sub_id}/migrations.json", :body => {:product_id => product_id }.to_json)
144
+ success = true if raw_response.code == 200
145
+ response = Hashie::Mash.new(raw_response)
146
+ (response.subscription || {}).update(:success? => success)
147
+ end
115
148
 
116
149
  def list_products
117
150
  products = self.class.get("/products.json")
@@ -126,5 +159,12 @@ module Chargify
126
159
  Hashie::Mash.new(self.class.get("/products/handle/#{handle}.json")).product
127
160
  end
128
161
 
162
+ def list_subscription_usage(subscription_id, component_id)
163
+ raw_response = self.class.get("/subscriptions/#{subscription_id}/components/#{component_id}/usages.json")
164
+ success = raw_response.code == 200
165
+ response = Hashie::Mash.new(raw_response)
166
+ response.update(:success? => success)
167
+ end
168
+
129
169
  end
130
170
  end
@@ -1,26 +1,10 @@
1
1
  require 'helper'
2
2
 
3
- class TestChargify < Test::Unit::TestCase
3
+ class ChargifyTest < Test::Unit::TestCase
4
4
  context "When hitting the Chargify API" do
5
5
  setup do
6
6
  @client = Chargify::Client.new('OU812', 'pengwynn')
7
7
  end
8
-
9
- should "raise UnexpectedResponseError when reponse is invalid JSON" do
10
- stub_post "https://OU812:x@pengwynn.chargify.com/subscriptions.json", "invalid_subscription.json"
11
- options = {
12
- :product_handle => 'monthly',
13
- :customer_reference => 'bradleyjoyce',
14
- :customer_attributes => {
15
- :first_name => "Wynn",
16
- :last_name => "Netherland",
17
- :email => "wynn@example.com"
18
- }
19
- }
20
- assert_raise Chargify::UnexpectedResponseError do
21
- @client.create_subscription(options)
22
- end
23
- end
24
8
 
25
9
  should "return a list of customers" do
26
10
  stub_get "https://OU812:x@pengwynn.chargify.com/customers.json", "customers.json"
@@ -30,12 +14,24 @@ class TestChargify < Test::Unit::TestCase
30
14
  customers.first.organization.should == 'Squeejee'
31
15
  end
32
16
 
17
+ context "when finding customers" do
18
+ should "be able to be found by a <reference_id>" do
19
+ stub_get "https://OU812:x@pengwynn.chargify.com/customers/lookup.json?reference=bradleyjoyce", "customer.json"
20
+ customer = @client.customer("bradleyjoyce")
21
+ customer.success?.should == true
22
+ end
23
+
24
+ should "be able to be found by a <chargify_id>" do
25
+ stub_get "https://OU812:x@pengwynn.chargify.com/customers/16.json", "customer.json"
26
+ customer = @client.customer_by_id(16)
27
+ customer.success?.should == true
28
+ end
33
29
 
34
- should "return info for a customer" do
35
- stub_get "https://OU812:x@pengwynn.chargify.com/customers/lookup.json?reference=16", "customer.json"
36
- customer = @client.customer(16)
37
- customer.reference.should == 'bradleyjoyce'
38
- customer.organization.should == 'Squeejee'
30
+ should "return an empty Hash with success? set to false" do
31
+ stub_get "https://OU812:x@pengwynn.chargify.com/customers/16.json", "", 404
32
+ customer = @client.customer_by_id(16)
33
+ customer.success?.should == false
34
+ end
39
35
  end
40
36
 
41
37
  should "create a new customer" do
@@ -61,12 +57,25 @@ class TestChargify < Test::Unit::TestCase
61
57
  customer.first_name.should == "Wynn"
62
58
  end
63
59
 
64
- should_eventually "raise an exception if a customer is not found" do
65
-
66
- end
67
-
68
- should_eventually "delete a customer" do
69
-
60
+ # Depends on Chargify:
61
+ # should_eventually "delete a customer" do
62
+ #
63
+ # end
64
+
65
+ should "raise UnexpectedResponseError when reponse is invalid JSON" do
66
+ stub_post "https://OU812:x@pengwynn.chargify.com/subscriptions.json", "invalid_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
+ }
76
+ assert_raise Chargify::UnexpectedResponseError do
77
+ @client.create_subscription(options)
78
+ end
70
79
  end
71
80
 
72
81
  should "return a list of customer subscriptions" do
@@ -148,7 +157,23 @@ class TestChargify < Test::Unit::TestCase
148
157
  subscription = @client.create_subscription(options)
149
158
  subscription.customer.organization.should == 'Squeejee'
150
159
  end
151
-
160
+
161
+ should "create a customer subscription with a coupon code" do
162
+ stub_post "https://OU812:x@pengwynn.chargify.com/subscriptions.json", "subscription.json"
163
+ options = {
164
+ :product_handle => 'monthly',
165
+ :customer_reference => 'bradleyjoyce',
166
+ :customer_attributes => {
167
+ :first_name => "Wynn",
168
+ :last_name => "Netherland",
169
+ :email => "wynn@example.com"
170
+ },
171
+ :coupon_code => "EARLYBIRD"
172
+ }
173
+ subscription = @client.create_subscription(options)
174
+ #subscription.coupon.should == 'Squeejee'
175
+ end
176
+
152
177
  should "set success? to true when subscription is created successfully" do
153
178
  stub_post "https://OU812:x@pengwynn.chargify.com/subscriptions.json", "subscription.json", 201
154
179
  options = {
@@ -221,24 +246,64 @@ class TestChargify < Test::Unit::TestCase
221
246
  subscription.success?.should == true
222
247
  end
223
248
 
224
- should_eventually "create a one-off charge for a subscription" do
225
-
226
- end
227
-
228
- should_eventually "list metered usage for a subscription" do
229
-
249
+ context "when creating a one-off charge for a subscription" do
250
+ setup do
251
+ stub_post "https://OU812:x@pengwynn.chargify.com/subscriptions/123/charges.json", "charge_subscription.json", 201
252
+ @options = {
253
+ :meno => "This is the description of the one time charge.",
254
+ :amount => 1.00,
255
+ :amount_in_cents => 100
256
+ }
257
+ end
258
+
259
+ should "accept :amount as a parameter" do
260
+ subscription = @client.charge_subscription(123, @options)
261
+
262
+ subscription.amount_in_cents.should == @options[:amount]*100
263
+ subscription.success?.should == true
264
+ end
265
+
266
+ should "accept :amount_in_cents as a parameter" do
267
+ subscription = @client.charge_subscription(123, @options)
268
+
269
+ subscription.amount_in_cents.should == @options[:amount_in_cents]
270
+ subscription.success?.should == true
271
+ end
272
+
273
+ should "have success? as false if parameters are missing" do
274
+ stub_post "https://OU812:x@pengwynn.chargify.com/subscriptions/123/charges.json", "charge_subscription_missing_parameters.json", 422
275
+
276
+ subscription = @client.charge_subscription(123, {})
277
+ subscription.success?.should == false
278
+ end
279
+
280
+ should "have success? as false if the subscription is not found" do
281
+ stub_post "https://OU812:x@pengwynn.chargify.com/subscriptions/9999/charges.json", "", 404
282
+
283
+ subscription = @client.charge_subscription(9999, @options)
284
+ subscription.success?.should == false
285
+ end
230
286
  end
231
287
 
232
- should_eventually "record metered usage for a subscription" do
233
-
234
- end
288
+ context "for metered subscriptions" do
289
+ should_eventually "list usage for a subscription" do
290
+ stub_get "https://OU812:x@pengwynn.chargify.com/subscriptions/123/components/456/usages.json", "list_metered_subscriptions.json", 200
291
+
292
+ subscription = @client.list_subscription_usage(123, 456)
293
+ subscription.success?.should == true
294
+ end
235
295
 
236
- should_eventually "migrate a subscription to a new product" do
296
+ should_eventually "record usage for a subscription" do
237
297
 
298
+ end
238
299
  end
239
300
 
240
- should_eventually "create one-time coupons" do
301
+ should "migrate a subscription from one product to another" do
302
+ stub_post "https://OU812:x@pengwynn.chargify.com/subscriptions/123/migrations.json", "migrate_subscription.json"
241
303
 
304
+ subscription = @client.migrate_subscription(123, 354);
305
+ subscription.success?.should == true
306
+ subscription.product.id.should == 354
242
307
  end
243
308
 
244
309
  should "return a list of products" do
@@ -258,7 +323,7 @@ class TestChargify < Test::Unit::TestCase
258
323
  product = @client.product_by_handle('tweetsaver')
259
324
  product.accounting_code.should == 'TSMO'
260
325
  end
261
-
326
+
262
327
 
263
328
  end
264
329
  end
@@ -1,11 +1,5 @@
1
1
  require 'test/unit'
2
2
  require 'pathname'
3
- require 'rubygems'
4
-
5
- gem 'thoughtbot-shoulda', '>= 2.10.1'
6
- gem 'jnunemaker-matchy', '0.4.0'
7
- gem 'mocha', '0.9.4'
8
- gem 'fakeweb', '>= 1.2.5'
9
3
 
10
4
  require 'shoulda'
11
5
  require 'matchy'
@@ -21,10 +15,6 @@ end
21
15
 
22
16
  FakeWeb.allow_net_connect = false
23
17
 
24
-
25
- class Test::Unit::TestCase
26
- end
27
-
28
18
  def fixture_file(filename)
29
19
  return '' if filename == ''
30
20
  file_path = File.expand_path(File.dirname(__FILE__) + '/fixtures/' + filename)
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 2
8
- - 3
9
- version: 0.2.3
8
+ - 4
9
+ version: 0.2.4
10
10
  platform: ruby
11
11
  authors:
12
12
  - Wynn Netherland
@@ -41,13 +41,13 @@ dependencies:
41
41
  - !ruby/object:Gem::Version
42
42
  segments:
43
43
  - 0
44
- - 4
45
44
  - 5
46
- version: 0.4.5
45
+ - 2
46
+ version: 0.5.2
47
47
  type: :runtime
48
48
  version_requirements: *id002
49
49
  - !ruby/object:Gem::Dependency
50
- name: thoughtbot-shoulda
50
+ name: shoulda
51
51
  prerelease: false
52
52
  requirement: &id003 !ruby/object:Gem::Requirement
53
53
  requirements:
@@ -79,13 +79,13 @@ dependencies:
79
79
  prerelease: false
80
80
  requirement: &id005 !ruby/object:Gem::Requirement
81
81
  requirements:
82
- - - "="
82
+ - - ~>
83
83
  - !ruby/object:Gem::Version
84
84
  segments:
85
85
  - 0
86
86
  - 9
87
- - 4
88
- version: 0.9.4
87
+ - 8
88
+ version: 0.9.8
89
89
  type: :development
90
90
  version_requirements: *id005
91
91
  - !ruby/object:Gem::Dependency
@@ -133,7 +133,7 @@ files:
133
133
  - test/fixtures/subscription_not_found.json
134
134
  - test/fixtures/subscriptions.json
135
135
  - test/helper.rb
136
- - test/test_chargify.rb
136
+ - test/chargify_test.rb
137
137
  has_rdoc: true
138
138
  homepage: http://github.com/pengwynn/chargify
139
139
  licenses: []
@@ -166,4 +166,4 @@ specification_version: 3
166
166
  summary: Ruby wrapper for the chargify.com SAAS and billing API
167
167
  test_files:
168
168
  - test/helper.rb
169
- - test/test_chargify.rb
169
+ - test/chargify_test.rb