lucidimagination-chargify 0.2.7.2

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,26 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+ dist
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
22
+ dist/*
23
+
24
+ Gemfile.lock
25
+ *.gem
26
+ .rvmrc
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Wynn Netherland
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.markdown ADDED
@@ -0,0 +1,31 @@
1
+ # chargify
2
+
3
+ Ruby wrapper for the chargify.com SAAS and billing API
4
+
5
+ ## Installation
6
+
7
+ sudo gem install chargify
8
+
9
+ ## Example Usage
10
+
11
+ ### Create, cancel, then reactivate subscription
12
+ attributes = { :product_handle => 'basic' ... }
13
+ @client = Chargify::Client.new('InDhcXAAAAAg7juDD', 'xxx-test')
14
+ subscription = @client.create_subscription(attributes)
15
+ @client.cancel_subscription(subscription[:id], "Canceled due to bad customer service.")
16
+ @client.reactivate_subscription(subscription[:id]) #Made him an offer he couldn't refuse!
17
+
18
+ ## Note on Patches/Pull Requests
19
+
20
+ * Fork the project.
21
+ * Make your feature addition or bug fix.
22
+ * Add tests for it. This is important so I don't break it in a
23
+ future version unintentionally.
24
+ * Commit, do not mess with rakefile, version, or history.
25
+ (if you want to have your own version, that is fine but
26
+ bump version in a commit by itself I can ignore when I pull)
27
+ * Send me a pull request. Bonus points for topic branches.
28
+
29
+ ### Copyright
30
+
31
+ Copyright (c) 2009 [Wynn Netherland](http://wynnnetherland.com). See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,15 @@
1
+ require "mg"
2
+ require 'active_support/inflector'
3
+ require 'shoulda/tasks'
4
+
5
+
6
+ MG.new("lucidimagination-chargify.gemspec")
7
+
8
+ require 'rake/testtask'
9
+ Rake::TestTask.new(:test) do |test|
10
+ test.ruby_opts = ['-rubygems'] if defined? Gem
11
+ test.libs << 'lib' << 'test'
12
+ test.pattern = 'test/**/*_test.rb'
13
+ end
14
+
15
+ task :default => :test
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.2.7
data/changelog.md ADDED
@@ -0,0 +1,21 @@
1
+ # Changelog
2
+
3
+ ## 0.2.6 May 27, 2010
4
+ * Fix #charge_subscription to submit it's body as json [@will](http://github.com/will) and [@ignat](http://github.com/ignat)
5
+ * API coverage for quantity components [@will](http://github.com/will)
6
+ * API coverage for site and subscription transactions [@ignat](http://github.com/ignat)
7
+
8
+ ## 0.2.5 May 24, 2010
9
+ * Require fix from [@will](http://github.com/will)
10
+
11
+ ## 0.2.4 May 20, 2010
12
+
13
+ * Substantial new API coverage from [@miksago](http://twitter.com/miksago)
14
+
15
+ ## 0.2.0 January 26, 2010
16
+
17
+ * Substantial fixes and convenience enhancements from [@nkabbara](http://github.com/nkabbara)
18
+
19
+ ## 0.1.0 November 18, 2009
20
+
21
+ * Initial version
data/lib/chargify.rb ADDED
@@ -0,0 +1,13 @@
1
+ require 'hashie'
2
+ require 'httparty'
3
+ require 'json'
4
+
5
+ directory = File.expand_path(File.dirname(__FILE__))
6
+
7
+ Hash.send :include, Hashie::HashExtensions
8
+
9
+ require File.join(directory, 'chargify', 'client')
10
+
11
+ module Chargify
12
+ VERSION = "0.2.6".freeze
13
+ end
@@ -0,0 +1,237 @@
1
+ module Chargify
2
+ class UnexpectedResponseError < RuntimeError
3
+ end
4
+
5
+ class Parser < HTTParty::Parser
6
+ def parse
7
+ begin
8
+ Crack::JSON.parse(body)
9
+ rescue => e
10
+ raise UnexpectedResponseError, "Crack could not parse JSON. It said: #{e.message}. Chargify's raw response: #{body}"
11
+ end
12
+ end
13
+ end
14
+
15
+ class Client
16
+ include HTTParty
17
+
18
+ parser Chargify::Parser
19
+ headers 'Content-Type' => 'application/json'
20
+
21
+ attr_reader :api_key, :subdomain
22
+
23
+ # Your API key can be generated on the settings screen.
24
+ def initialize(api_key, subdomain)
25
+ @api_key = api_key
26
+ @subdomain = subdomain
27
+
28
+ self.class.base_uri "https://#{@subdomain}.chargify.com"
29
+ self.class.basic_auth @api_key, 'x'
30
+
31
+ end
32
+
33
+ # options: page
34
+ def list_customers(options={})
35
+ customers = get("/customers.json", :query => options)
36
+ customers.map{|c| Hashie::Mash.new c['customer']}
37
+ end
38
+
39
+ def customer_by_id(chargify_id)
40
+ request = 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 = 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)
51
+ end
52
+
53
+ alias customer customer_by_reference
54
+
55
+
56
+ #
57
+ # * first_name (Required)
58
+ # * last_name (Required)
59
+ # * email (Required)
60
+ # * organization (Optional) Company/Organization name
61
+ # * reference (Optional, but encouraged) The unique identifier used within your own application for this customer
62
+ #
63
+ def create_customer(info={})
64
+ response = Hashie::Mash.new(post("/customers.json", :body => {:customer => info}))
65
+ return response.customer if response.customer
66
+ response
67
+ end
68
+
69
+ # Returns all elements outputted by Chargify plus:
70
+ # response.success? -> true if response code is 200, false otherwise
71
+ def update_customer(customer_attributes = {})
72
+ customer_attributes.stringify_keys!
73
+ customer_id = customer_attributes.delete('id')
74
+ raw_response = put("/customers/#{customer_id}.json", :body => {:customer_id => customer_attributes})
75
+ updated = true if raw_response.code == 200
76
+ response = Hashie::Mash.new(raw_response)
77
+ (response.customer || response).update(:success? => updated)
78
+ end
79
+
80
+ def customer_subscriptions(chargify_id)
81
+ subscriptions = get("/customers/#{chargify_id}/subscriptions.json")
82
+ subscriptions.map{|s| Hashie::Mash.new s['subscription']}
83
+ end
84
+
85
+ def subscription(subscription_id)
86
+ raw_response = get("/subscriptions/#{subscription_id}.json")
87
+ return nil if raw_response.code != 200
88
+ Hashie::Mash.new(raw_response).subscription
89
+ end
90
+
91
+ # Returns all elements outputted by Chargify plus:
92
+ # response.success? -> true if response code is 201, false otherwise
93
+ def create_subscription(subscription_attributes={})
94
+ raw_response = post("/subscriptions.json", :body => {:subscription => subscription_attributes})
95
+ created = true if raw_response.code == 201
96
+ response = Hashie::Mash.new(raw_response)
97
+ (response.subscription || response).update(:success? => created)
98
+ end
99
+
100
+ # Returns all elements outputted by Chargify plus:
101
+ # response.success? -> true if response code is 200, false otherwise
102
+ def update_subscription(sub_id, subscription_attributes = {})
103
+ raw_response = put("/subscriptions/#{sub_id}.json", :body => {:subscription => subscription_attributes})
104
+ updated = true if raw_response.code == 200
105
+ response = Hashie::Mash.new(raw_response)
106
+ (response.subscription || response).update(:success? => updated)
107
+ end
108
+
109
+ # Returns all elements outputted by Chargify plus:
110
+ # response.success? -> true if response code is 200, false otherwise
111
+ def cancel_subscription(sub_id, message="")
112
+ raw_response = delete("/subscriptions/#{sub_id}.json", :body => {:subscription => {:cancellation_message => message} })
113
+ deleted = true if raw_response.code == 200
114
+ response = Hashie::Mash.new(raw_response)
115
+ (response.subscription || response).update(:success? => deleted)
116
+ end
117
+
118
+ def reactivate_subscription(sub_id)
119
+ raw_response = put("/subscriptions/#{sub_id}/reactivate.json", :body => "")
120
+ reactivated = true if raw_response.code == 200
121
+ response = Hashie::Mash.new(raw_response) rescue Hashie::Mash.new
122
+ (response.subscription || response).update(:success? => reactivated)
123
+ end
124
+
125
+ def charge_subscription(sub_id, subscription_attributes={})
126
+ raw_response = post("/subscriptions/#{sub_id}/charges.json", :body => { :charge => subscription_attributes })
127
+ success = raw_response.code == 201
128
+ if raw_response.code == 404
129
+ raw_response = {}
130
+ end
131
+
132
+ response = Hashie::Mash.new(raw_response)
133
+ (response.charge || response).update(:success? => success)
134
+ end
135
+
136
+ def migrate_subscription(sub_id, product_id)
137
+ raw_response = post("/subscriptions/#{sub_id}/migrations.json", :body => {:product_id => product_id })
138
+ success = true if raw_response.code == 200
139
+ response = Hashie::Mash.new(raw_response)
140
+ (response.subscription || {}).update(:success? => success)
141
+ end
142
+
143
+ def list_products
144
+ products = get("/products.json")
145
+ products.map{|p| Hashie::Mash.new p['product']}
146
+ end
147
+
148
+ def product(product_id)
149
+ Hashie::Mash.new( get("/products/#{product_id}.json")).product
150
+ end
151
+
152
+ def product_by_handle(handle)
153
+ Hashie::Mash.new(get("/products/handle/#{handle}.json")).product
154
+ end
155
+
156
+ def list_subscription_usage(subscription_id, component_id)
157
+ raw_response = get("/subscriptions/#{subscription_id}/components/#{component_id}/usages.json")
158
+ success = raw_response.code == 200
159
+ response = Hashie::Mash.new(raw_response)
160
+ response.update(:success? => success)
161
+ end
162
+
163
+ def subscription_transactions(sub_id, options={})
164
+ transactions = get("/subscriptions/#{sub_id}/transactions.json", :query => options)
165
+ transactions.map{|t| Hashie::Mash.new t['transaction']}
166
+ end
167
+
168
+ def site_transactions(options={})
169
+ transactions = get("/transactions.json", :query => options)
170
+ transactions.map{|t| Hashie::Mash.new t['transaction']}
171
+ end
172
+
173
+ def list_statements(subscription_id)
174
+ statements = get("/subscriptions/#{subscription_id}/statements.json")
175
+ statements.map{|s| Hashie::Mash.new s['statement']}
176
+ end
177
+
178
+ def list_components(subscription_id)
179
+ components = get("/subscriptions/#{subscription_id}/components.json")
180
+ components.map{|c| Hashie::Mash.new c['component']}
181
+ end
182
+
183
+ def subscription_component(subscription_id, component_id)
184
+ response = get("/subscriptions/#{subscription_id}/components/#{component_id}.json")
185
+ Hashie::Mash.new(response).component
186
+ end
187
+
188
+ def update_subscription_component_allocated_quantity(subscription_id, component_id, quantity)
189
+ update_subscription_component(subscription_id, component_id, :allocated_quantity => quantity)
190
+ end
191
+
192
+ def update_subscription_component_enabled(subscription_id, component_id, enabled)
193
+ update_subscription_component(subscription_id, component_id, :enabled => enabled)
194
+ end
195
+
196
+ def update_subscription_component(subscription_id, component_id, component = {})
197
+ component[:enabled] = (component[:enabled] ? 1 : 0) if component.keys.include?(:enabled)
198
+ response = put("/subscriptions/#{subscription_id}/components/#{component_id}.json",
199
+ :body => {:component => component})
200
+ response[:success?] = response.code == 200
201
+ Hashie::Mash.new(response)
202
+ end
203
+
204
+ alias update_metered_component update_subscription_component_allocated_quantity
205
+ alias update_component_quantity update_subscription_component_allocated_quantity
206
+ alias update_on_off_component update_subscription_component_enabled
207
+ alias update_component update_subscription_component
208
+
209
+
210
+ private
211
+
212
+ def post(path, options={})
213
+ jsonify_body!(options)
214
+ self.class.post(path, options)
215
+ end
216
+
217
+ def put(path, options={})
218
+ jsonify_body!(options)
219
+ self.class.put(path, options)
220
+ end
221
+
222
+ def delete(path, options={})
223
+ jsonify_body!(options)
224
+ self.class.delete(path, options)
225
+ end
226
+
227
+ def get(path, options={})
228
+ jsonify_body!(options)
229
+ self.class.get(path, options)
230
+ end
231
+
232
+ def jsonify_body!(options)
233
+ options[:body] = options[:body].to_json if options[:body]
234
+
235
+ end
236
+ end
237
+ end
@@ -0,0 +1,82 @@
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{lucidimagination-chargify}
8
+ s.version = "0.2.7.2"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Wynn Netherland", "Nash Kabbara"]
12
+ s.date = %q{2010-05-24}
13
+ s.description = %q{Ruby wrapper for the chargify.com SAAS and billing API}
14
+ s.email = %q{wynn.netherland@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.markdown"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.markdown",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "changelog.md",
27
+ "lucidimagination-chargify.gemspec",
28
+ "lib/chargify.rb",
29
+ "lib/chargify/client.rb",
30
+ "test/fixtures/customer.json",
31
+ "test/fixtures/customers.json",
32
+ "test/fixtures/deleted_subscription.json",
33
+ "test/fixtures/invalid_subscription.json",
34
+ "test/fixtures/new_customer.json",
35
+ "test/fixtures/product.json",
36
+ "test/fixtures/products.json",
37
+ "test/fixtures/subscription.json",
38
+ "test/fixtures/subscription_not_found.json",
39
+ "test/fixtures/subscriptions.json",
40
+ "test/helper.rb",
41
+ "test/chargify_test.rb"
42
+ ]
43
+ s.homepage = %q{http://github.com/pengwynn/chargify}
44
+ s.rdoc_options = ["--charset=UTF-8"]
45
+ s.require_paths = ["lib"]
46
+ s.rubygems_version = %q{1.3.6}
47
+ s.summary = %q{Ruby wrapper for the chargify.com SAAS and billing API}
48
+ s.test_files = [
49
+ "test/helper.rb",
50
+ "test/chargify_test.rb"
51
+ ]
52
+
53
+ if s.respond_to? :specification_version then
54
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
55
+ s.specification_version = 3
56
+
57
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
58
+ s.add_runtime_dependency(%q<hashie>, [">= 0.1.3"])
59
+ s.add_runtime_dependency(%q<httparty>, [">= 0.5.2"])
60
+ s.add_development_dependency(%q<shoulda>, [">= 2.10.1"])
61
+ s.add_development_dependency(%q<jnunemaker-matchy>, ["= 0.4.0"])
62
+ s.add_development_dependency(%q<mocha>, ["~> 0.9.8"])
63
+ s.add_development_dependency(%q<fakeweb>, [">= 1.2.5"])
64
+ s.add_development_dependency(%q<redgreen>, [">= 1.2.2"])
65
+ else
66
+ s.add_dependency(%q<hashie>, [">= 0.1.3"])
67
+ s.add_dependency(%q<httparty>, [">= 0.5.2"])
68
+ s.add_dependency(%q<shoulda>, [">= 2.10.1"])
69
+ s.add_dependency(%q<jnunemaker-matchy>, ["= 0.4.0"])
70
+ s.add_dependency(%q<mocha>, ["~> 0.9.8"])
71
+ s.add_dependency(%q<fakeweb>, [">= 1.2.5"])
72
+ end
73
+ else
74
+ s.add_dependency(%q<hashie>, [">= 0.1.3"])
75
+ s.add_dependency(%q<httparty>, [">= 0.5.2"])
76
+ s.add_dependency(%q<shoulda>, [">= 2.10.1"])
77
+ s.add_dependency(%q<jnunemaker-matchy>, ["= 0.4.0"])
78
+ s.add_dependency(%q<mocha>, ["~> 0.9.8"])
79
+ s.add_dependency(%q<fakeweb>, [">= 1.2.5"])
80
+ end
81
+ end
82
+