jsmestad-chargify 0.3.0.pre5

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 (43) hide show
  1. data/.gitignore +23 -0
  2. data/Gemfile +20 -0
  3. data/LICENSE +20 -0
  4. data/README.markdown +38 -0
  5. data/Rakefile +38 -0
  6. data/VERSION +1 -0
  7. data/changelog.md +24 -0
  8. data/jsmestad-chargify.gemspec +97 -0
  9. data/lib/chargify.rb +22 -0
  10. data/lib/chargify/base.rb +88 -0
  11. data/lib/chargify/config.rb +86 -0
  12. data/lib/chargify/customer.rb +72 -0
  13. data/lib/chargify/error.rb +23 -0
  14. data/lib/chargify/parser.rb +13 -0
  15. data/lib/chargify/product.rb +26 -0
  16. data/lib/chargify/subscription.rb +93 -0
  17. data/lib/chargify/transaction.rb +14 -0
  18. data/spec/fixtures/charge_subscription.json +5 -0
  19. data/spec/fixtures/charge_subscription_missing_parameters.json +4 -0
  20. data/spec/fixtures/component.json +11 -0
  21. data/spec/fixtures/components.json +24 -0
  22. data/spec/fixtures/customer.json +12 -0
  23. data/spec/fixtures/customers.json +12 -0
  24. data/spec/fixtures/deleted_subscription.json +1 -0
  25. data/spec/fixtures/invalid_subscription.json +48 -0
  26. data/spec/fixtures/list_metered_subscriptions.json +3 -0
  27. data/spec/fixtures/migrate_subscription.json +51 -0
  28. data/spec/fixtures/new_customer.json +12 -0
  29. data/spec/fixtures/product.json +17 -0
  30. data/spec/fixtures/products.json +17 -0
  31. data/spec/fixtures/subscription.json +49 -0
  32. data/spec/fixtures/subscription_not_found.json +1 -0
  33. data/spec/fixtures/subscriptions.json +49 -0
  34. data/spec/spec.opts +1 -0
  35. data/spec/spec_helper.rb +26 -0
  36. data/spec/support/fakeweb_stubs.rb +33 -0
  37. data/spec/unit/chargify/config_spec.rb +147 -0
  38. data/spec/unit/chargify/customer_spec.rb +111 -0
  39. data/spec/unit/chargify/parser_spec.rb +7 -0
  40. data/spec/unit/chargify/product_spec.rb +35 -0
  41. data/spec/unit/chargify/subscription_spec.rb +304 -0
  42. data/spec/unit/chargify/transaction_spec.rb +11 -0
  43. metadata +154 -0
data/.gitignore ADDED
@@ -0,0 +1,23 @@
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
+ .bundle
21
+
22
+ ## PROJECT::SPECIFIC
23
+ dist/*
data/Gemfile ADDED
@@ -0,0 +1,20 @@
1
+ source 'http://rubygems.org'
2
+
3
+ group :runtime do
4
+ gem 'httparty', '~> 0.6.1'
5
+ gem 'hashie', '~> 0.1.8'
6
+ gem 'json'
7
+ end
8
+
9
+ group :test do
10
+ gem 'jeweler'
11
+ gem 'rake'
12
+ gem 'bundler', '~> 0.9.5'
13
+ gem 'fakeweb', '>= 1.2.5'
14
+ gem 'mocha', '~> 0.9.8'
15
+ gem 'rspec', :require => 'spec'
16
+ gem 'activesupport', '>= 2.3.5', :require => 'active_support'
17
+ end
18
+
19
+
20
+ # vim: ft=ruby
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Wynn Netherland, Justin Smestad
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,38 @@
1
+ # chargify
2
+
3
+ Ruby wrapper for the chargify.com SAAS and billing API
4
+
5
+ ### Important Notice
6
+
7
+ This fork breaks all compatibility with previous versions (<= 0.2.6)
8
+
9
+ ## Installation
10
+
11
+ gem install chargify
12
+
13
+ ## Example Usage
14
+
15
+ ### Create, cancel, then reactivate subscription
16
+ attributes = { :product_handle => 'basic' ... }
17
+ subscription = Chargify::Subscription.create(attributes)
18
+ Chargify::Subscription.cancel(subscription[:id], "Canceled due to bad customer service.")
19
+ Chargify::Subscription.reactivate(subscription[:id]) #Made him an offer he couldn't refuse!
20
+
21
+ ## Rails Usage
22
+
23
+ ### config/initializers/chargify.rb
24
+ Chargify::Config.setup do |config|
25
+ config[:subdomain] = 'xxx-test'
26
+ config[:api_key] = 'InDhcXAAAAAg7juDD'
27
+ end
28
+
29
+ ## Contributing (requires Bundler >= 0.9.7):
30
+
31
+ $ git clone git://github.com/jsmestad/chargify.git
32
+ $ cd chargify
33
+ $ bundle install
34
+ $ bundle exec rake
35
+
36
+ ### Copyright
37
+
38
+ See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,38 @@
1
+ require 'rake'
2
+ require 'bundler'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "jsmestad-chargify"
8
+ gem.summary = %Q{Ruby wrapper for the Chargify API}
9
+ gem.email = "justin.smestad@gmail.com"
10
+ gem.homepage = "http://github.com/jsmestad/chargify"
11
+ gem.authors = ["Wynn Netherland", "Justin Smestad"]
12
+
13
+ bundle = Bundler::Definition.from_gemfile('Gemfile')
14
+ bundle.dependencies.each do |dep|
15
+ next unless dep.groups.include?(:runtime)
16
+ gem.add_dependency(dep.name, dep.requirement.to_s)
17
+ end
18
+ end
19
+ Jeweler::GemcutterTasks.new
20
+ rescue LoadError
21
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
22
+ end
23
+
24
+
25
+ require 'spec/rake/spectask'
26
+ Spec::Rake::SpecTask.new(:spec) do |spec|
27
+ spec.libs << 'lib' << 'spec'
28
+ spec.spec_files = FileList['spec/**/*_spec.rb']
29
+ end
30
+
31
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
32
+ spec.libs << 'lib' << 'spec'
33
+ spec.pattern = 'spec/**/*_spec.rb'
34
+ spec.rcov = true
35
+ end
36
+
37
+ task :default => :spec
38
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.3.0.pre5
data/changelog.md ADDED
@@ -0,0 +1,24 @@
1
+ # Changelog
2
+
3
+ ## 0.3.0.pre July 08, 2010
4
+ * Complete API Rewrite, please view wiki for API documentation
5
+
6
+ ## 0.2.6 May 27, 2010
7
+ * Fix #charge_subscription to submit it's body as json [@will](http://github.com/will) and [@ignat](http://github.com/ignat)
8
+ * API coverage for quantity components [@will](http://github.com/will)
9
+ * API coverage for site and subscription transactions [@ignat](http://github.com/ignat)
10
+
11
+ ## 0.2.5 May 24, 2010
12
+ * Require fix from [@will](http://github.com/will)
13
+
14
+ ## 0.2.4 May 20, 2010
15
+
16
+ * Substantial new API coverage from [@miksago](http://twitter.com/miksago)
17
+
18
+ ## 0.2.0 January 26, 2010
19
+
20
+ * Substantial fixes and convenience enhancements from [@nkabbara](http://github.com/nkabbara)
21
+
22
+ ## 0.1.0 November 18, 2009
23
+
24
+ * Initial version
@@ -0,0 +1,97 @@
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{jsmestad-chargify}
8
+ s.version = "0.3.0.pre5"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new("> 1.3.1") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Wynn Netherland", "Justin Smestad"]
12
+ s.date = %q{2010-07-11}
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
+ "Gemfile",
21
+ "LICENSE",
22
+ "README.markdown",
23
+ "Rakefile",
24
+ "VERSION",
25
+ "changelog.md",
26
+ "jsmestad-chargify.gemspec",
27
+ "lib/chargify.rb",
28
+ "lib/chargify/base.rb",
29
+ "lib/chargify/config.rb",
30
+ "lib/chargify/customer.rb",
31
+ "lib/chargify/error.rb",
32
+ "lib/chargify/parser.rb",
33
+ "lib/chargify/product.rb",
34
+ "lib/chargify/subscription.rb",
35
+ "lib/chargify/transaction.rb",
36
+ "spec/fixtures/charge_subscription.json",
37
+ "spec/fixtures/charge_subscription_missing_parameters.json",
38
+ "spec/fixtures/component.json",
39
+ "spec/fixtures/components.json",
40
+ "spec/fixtures/customer.json",
41
+ "spec/fixtures/customers.json",
42
+ "spec/fixtures/deleted_subscription.json",
43
+ "spec/fixtures/invalid_subscription.json",
44
+ "spec/fixtures/list_metered_subscriptions.json",
45
+ "spec/fixtures/migrate_subscription.json",
46
+ "spec/fixtures/new_customer.json",
47
+ "spec/fixtures/product.json",
48
+ "spec/fixtures/products.json",
49
+ "spec/fixtures/subscription.json",
50
+ "spec/fixtures/subscription_not_found.json",
51
+ "spec/fixtures/subscriptions.json",
52
+ "spec/spec.opts",
53
+ "spec/spec_helper.rb",
54
+ "spec/support/fakeweb_stubs.rb",
55
+ "spec/unit/chargify/config_spec.rb",
56
+ "spec/unit/chargify/customer_spec.rb",
57
+ "spec/unit/chargify/parser_spec.rb",
58
+ "spec/unit/chargify/product_spec.rb",
59
+ "spec/unit/chargify/subscription_spec.rb",
60
+ "spec/unit/chargify/transaction_spec.rb"
61
+ ]
62
+ s.homepage = %q{http://github.com/jsmestad/chargify}
63
+ s.rdoc_options = ["--charset=UTF-8"]
64
+ s.require_paths = ["lib"]
65
+ s.rubygems_version = %q{1.3.6}
66
+ s.summary = %q{Ruby wrapper for the Chargify API}
67
+ s.test_files = [
68
+ "spec/spec_helper.rb",
69
+ "spec/support/fakeweb_stubs.rb",
70
+ "spec/unit/chargify/config_spec.rb",
71
+ "spec/unit/chargify/customer_spec.rb",
72
+ "spec/unit/chargify/parser_spec.rb",
73
+ "spec/unit/chargify/product_spec.rb",
74
+ "spec/unit/chargify/subscription_spec.rb",
75
+ "spec/unit/chargify/transaction_spec.rb"
76
+ ]
77
+
78
+ if s.respond_to? :specification_version then
79
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
80
+ s.specification_version = 3
81
+
82
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
83
+ s.add_runtime_dependency(%q<httparty>, ["~> 0.6.1"])
84
+ s.add_runtime_dependency(%q<hashie>, ["~> 0.1.8"])
85
+ s.add_runtime_dependency(%q<json>, [">= 0"])
86
+ else
87
+ s.add_dependency(%q<httparty>, ["~> 0.6.1"])
88
+ s.add_dependency(%q<hashie>, ["~> 0.1.8"])
89
+ s.add_dependency(%q<json>, [">= 0"])
90
+ end
91
+ else
92
+ s.add_dependency(%q<httparty>, ["~> 0.6.1"])
93
+ s.add_dependency(%q<hashie>, ["~> 0.1.8"])
94
+ s.add_dependency(%q<json>, [">= 0"])
95
+ end
96
+ end
97
+
data/lib/chargify.rb ADDED
@@ -0,0 +1,22 @@
1
+ require 'hashie'
2
+ require 'httparty'
3
+ require 'json'
4
+
5
+ Hash.send :include, Hashie::HashExtensions
6
+
7
+ module Chargify
8
+
9
+ autoload :Base, 'chargify/base'
10
+ autoload :Config, 'chargify/config'
11
+ autoload :Customer, 'chargify/customer'
12
+ autoload :Error, 'chargify/error'
13
+ autoload :Parser, 'chargify/parser'
14
+ autoload :Product, 'chargify/product'
15
+ autoload :Subscription, 'chargify/subscription'
16
+ autoload :Transaction, 'chargify/transaction'
17
+
18
+ # TODO: migrate away from generic errors
19
+ # Replace with Chargify::Errors
20
+
21
+
22
+ end
@@ -0,0 +1,88 @@
1
+ module Chargify
2
+ class Base
3
+ include HTTParty
4
+
5
+ parser Chargify::Parser
6
+ headers 'Content-Type' => 'application/json'
7
+ headers 'User-Agent' => 'Chargify Ruby Client'
8
+
9
+ class << self
10
+
11
+ def api_request(type, path, options={})
12
+ self.base_uri "https://#{Chargify::Config.subdomain}.chargify.com"
13
+
14
+ # This is to allow bang methods
15
+ raise_errors = options.delete(:raise_errors)
16
+
17
+ # Build options hash for HTTParty
18
+ options[:body] = options[:body].to_json if options[:body]
19
+ options[:basic_auth] = {:username => Chargify::Config.api_key, :password => 'x'}
20
+
21
+ Chargify::Config.logger.debug("[CHARGIFY] Sending #{self.base_uri}#{path} a payload of #{options[:body]}") if Chargify::Config.debug
22
+
23
+ begin
24
+ response = self.send(type.to_s, path, options)
25
+ rescue SocketError
26
+ raise(Chargify::Error::ConnectionFailed.new, "Failed to connect to payment gateway.")
27
+ end
28
+
29
+ if raise_errors
30
+ case response.code.to_i
31
+ when 401
32
+ raise(Chargify::Error::AccessDenied.new(response), response.body)
33
+ when 403
34
+ raise(Chargify::Error::Forbidden.new(response), response.body)
35
+ when 422
36
+ raise(Chargify::Error::BadRequest.new(response), response.body)
37
+ when 404
38
+ raise(Chargify::Error::NotFound.new(response), response.body)
39
+ when 504
40
+ raise(Chargify::Error::GatewayTimeout.new(response), response.body)
41
+ end
42
+ end
43
+
44
+ Chargify::Config.logger.debug("[CHARGIFY] Response from #{self.base_uri}#{path} was #{response.code}: #{response.body}") if Chargify::Config.debug
45
+
46
+ response
47
+ end
48
+
49
+ end
50
+
51
+ attr_reader :errors
52
+
53
+ def initialize(options={})
54
+ Chargify::Config.api_key = options[:api_key] if options[:api_key]
55
+ Chargify::Config.subdomain = options[:subdomain] if options[:subdomain]
56
+
57
+ @errors = []
58
+ self.attributes = attrs
59
+ end
60
+
61
+ def attributes=(attrs)
62
+ attrs.each do |k, v|
63
+ self.send(:"#{k}=", v) if self.respond_to?(:"#{k}=")
64
+ end
65
+ end
66
+
67
+ def api_request(type, path, options={})
68
+ @errors = []
69
+ begin
70
+ self.class.api_request(type, path, options)
71
+ rescue Chargify::Error::Base => e
72
+ if e.response.is_a?(Hash)
73
+ if e.response.has_key?("errors")
74
+ @errors = [*e.response["errors"]["error"]]
75
+ else
76
+ @errors = [e.response.body]
77
+ end
78
+ else
79
+ @errors = [e.message]
80
+ end
81
+ raise
82
+ end
83
+ end
84
+
85
+
86
+ end
87
+ end
88
+
@@ -0,0 +1,86 @@
1
+ require 'pp'
2
+ module Chargify
3
+ module Config
4
+ class << self
5
+
6
+ # the configuration hash itself
7
+ def configuration
8
+ @configuration ||= defaults
9
+ end
10
+
11
+ def defaults
12
+ {
13
+ :logger => defined?(Rails.logger) ? Rails.logger : Logger.new(STDOUT),
14
+ :debug => false,
15
+ :subdomain => "your-site-name",
16
+ :api_key => "your-api-key"
17
+ }
18
+ end
19
+
20
+ def [](key)
21
+ configuration[key]
22
+ end
23
+
24
+ def []=(key, val)
25
+ configuration[key] = val
26
+ end
27
+
28
+ # remove an item from the configuration
29
+ def delete(key)
30
+ configuration.delete(key)
31
+ end
32
+
33
+ # Return the value of the key, or the default if doesn't exist
34
+ #
35
+ # ==== Examples
36
+ #
37
+ # Chargify::Config.fetch(:monkey, false)
38
+ # => false
39
+ #
40
+ def fetch(key, default)
41
+ configuration.fetch(key, default)
42
+ end
43
+
44
+ def to_hash
45
+ configuration
46
+ end
47
+
48
+ # Yields the configuration.
49
+ #
50
+ # ==== Examples
51
+ # Chargify::Config.use do |config|
52
+ # config[:debug] = true
53
+ # config.something = false
54
+ # end
55
+ #
56
+ def setup
57
+ yield self
58
+ nil
59
+ end
60
+
61
+ def clear
62
+ @configuration = {}
63
+ end
64
+
65
+ def reset
66
+ @configuration = defaults
67
+ end
68
+
69
+ # allow getting and setting properties via Chargify::Config.xxx
70
+ #
71
+ # ==== Examples
72
+ # Chargify::Config.debug
73
+ # Chargify::Config.debug = false
74
+ #
75
+ def method_missing(method, *args)
76
+ if method.to_s[-1,1] == '='
77
+ configuration[method.to_s.tr('=','').to_sym] = *args
78
+ else
79
+ configuration[method]
80
+ end
81
+ end
82
+
83
+ end
84
+ end
85
+ end
86
+