Permalink
Browse files

First commit

  • Loading branch information...
0 parents commit 5d92e5067bb6bf8a69ab8e5e04478a7d7dfc5258 @moklett moklett committed Nov 17, 2009
Showing with 322 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +21 −0 LICENSE.txt
  3. +79 −0 README.md
  4. +51 −0 examples/customers.rb
  5. +24 −0 examples/products.rb
  6. +43 −0 examples/subscriptions.rb
  7. +103 −0 lib/chargify_api_ares.rb
1 .gitignore
@@ -0,0 +1 @@
+config
21 LICENSE.txt
@@ -0,0 +1,21 @@
+The MIT License
+
+Copyright (c) 2009 Grasshopper Group, LLC
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
79 README.md
@@ -0,0 +1,79 @@
+Chargify API wrapper for Ruby (using ActiveResource)
+====================================================
+
+chargify_api_ares
+-----------------
+
+This is a Ruby wrapper for the [Chargify](http://chargify.com) API that leverages ActiveResource.
+It allows you to interface with the Chargify API using simple ActiveRecord-like syntax, i.e.:
+
+ Chargify::Subscription.create(
+ :customer_reference => 'moklett',
+ :product_handle => 'chargify-api-ares-test',
+ :credit_card_attributes => {
+ :first_name => "Michael",
+ :last_name => "Klett",
+ :expiration_month => 1,
+ :expiration_year => 2010,
+ :full_number => "1234-1234-1234-1234"
+ }
+ )
+
+ subscription.credit_card_attributes = {:expiration_year => 2013}
+ subscription.save
+
+ subscription.cancel
+
+See the `examples` directory for more usage examples.
+
+
+### Installation
+
+This library can be installed as a gem. It is hosted on [Gemcutter](http://gemcutter.org).
+
+If you don't have your system set up to use gemcutter, follow the instructions on their site
+<http://gemcutter.org>, i.e.:
+
+$ gem install gemcutter
+$ gem tumble
+
+This will install Gemcutter and set your gem sources to search the gemcutter repos.
+
+Then you can install this library as a gem:
+
+$ gem install chargify_api_ares
+
+
+### Requirements
+
+This library requires ActiveResource version 2.3.4.
+
+$ gem install activeresource
+
+
+### Usage
+
+Simply require this library before you use it:
+
+ require 'chargify_api_ares'
+
+
+If you're using Rails, you could include this gem in your configuration, i.e. in `environment.rb`
+
+ config.gem 'chargify_api_ares'
+
+
+Now you'll have access to classes the interact with the Chargify API, such as:
+
+`Chargify::Product`
+`Chargify::Customer`
+`Chargifiy::Subscription`
+
+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)
+
+
+
+### Contributors
+
+* Michael Klett (Grasshopper Labs and Chargify)
51 examples/customers.rb
@@ -0,0 +1,51 @@
+$: << File.expand_path(File.dirname(__FILE__) + '/../lib')
+
+require 'chargify_api_ares'
+
+# You could load your credentials from a file...
+chargify_config = YAML::load_file(File.join(File.dirname(__FILE__), '..', 'config', 'chargify.yml'))
+
+Chargify.configure do |c|
+ c.subdomain = chargify_config['subdomain']
+ c.api_key = chargify_config['api_key']
+end
+
+
+# Retrieve a list of all your customers
+Chargify::Customer.find(:all)
+# => [#<Chargify::Customer:0x102d0cef8 @prefix_options={}, @attributes={"reference"=>"moklett", "updated_at"=>Mon Nov 16 23:19:25 UTC 2009, "id"=>325, "first_name"=>"Michael", "organization"=>"Chargify", "last_name"=>"Klett", "email"=>"moklett@example.com", "created_at"=>Mon Nov 16 23:19:25 UTC 2009}>]
+
+
+customer = Chargify::Customer.find(325)
+
+customer.first_name
+# => Michael
+
+customer.last_name
+# => Klett
+
+# Update a customer - success!
+customer.first_name = "Miguel"
+customer.save
+# => true
+
+customer.first_name
+# => Miguel
+
+# Update a customer - fail!
+customer.first_name = ""
+customer.save
+# => false
+
+customer.errors.full_messages.inspect
+# => ["First name: cannot be blank."]
+
+
+# Create a new customer - success!
+Chargify::Customer.create(
+ :first_name => "Charlie",
+ :last_name => "Bull",
+ :email => "charlie@example.com",
+ :organization => "Chargify"
+)
+# => #<Chargify::Customer:0x102c27970 @prefix_options={}, @attributes={"reference"=>nil, "updated_at"=>Mon Nov 16 23:43:33 UTC 2009, "id"=>327, "organization"=>"Chargify", "first_name"=>"Charlie", "last_name"=>"Bull", "created_at"=>Mon Nov 16 23:43:33 UTC 2009, "email"=>"charlie@example.com"}>
24 examples/products.rb
@@ -0,0 +1,24 @@
+$: << File.expand_path(File.dirname(__FILE__) + '/../lib')
+
+require 'chargify_api_ares'
+
+# You could load your credentials from a file...
+chargify_config = YAML::load_file(File.join(File.dirname(__FILE__), '..', 'config', 'chargify.yml'))
+
+Chargify.configure do |c|
+ c.subdomain = chargify_config['subdomain']
+ c.api_key = chargify_config['api_key']
+end
+
+
+# Retrieve a list of all your products
+products = Chargify::Product.find(:all)
+# => [#<Chargify::Product:0x102cdcac8 @prefix_options={}, @attributes={"name"=>"Chargify API Ares Test", "price_in_cents"=>0, "handle"=>"chargify-api-ares-test", "product_family"=>#<Chargify::Product::ProductFamily:0x102cdbad8 @prefix_options={}, @attributes={"name"=>"Chargify API ARes Test", "handle"=>"chargify-api-ares-test", "id"=>78, "accounting_code"=>nil}>, "id"=>152, "accounting_code"=>nil, "interval_unit"=>"month", "interval"=>1}>]
+
+# Find a single product by id
+product = Chargify::Product.find(products.first.id)
+# => #<Chargify::Product:0x102ce7540 @prefix_options={}, @attributes={"price_in_cents"=>0, "name"=>"Chargify API Ares Test", "handle"=>"chargify-api-ares-test", "product_family"=>#<Chargify::Product::ProductFamily:0x102ce6ca8 @prefix_options={}, @attributes={"name"=>"Chargify API ARes Test", "handle"=>"chargify-api-ares-test", "id"=>78, "accounting_code"=>nil}>, "id"=>152, "accounting_code"=>nil, "interval_unit"=>"month", "interval"=>1}>
+
+# Find a single product by its handle
+product = Chargify::Product.find_by_handle(products.first.handle)
+# => #<Chargify::Product:0x102c7a828 @prefix_options={}, @attributes={"price_in_cents"=>0, "name"=>"Chargify API Ares Test", "handle"=>"chargify-api-ares-test", "product_family"=>#<Chargify::Product::ProductFamily:0x102c798b0 @prefix_options={}, @attributes={"name"=>"Chargify API ARes Test", "handle"=>"chargify-api-ares-test", "id"=>78, "accounting_code"=>nil}>, "id"=>152, "accounting_code"=>nil, "interval_unit"=>"month", "interval"=>1}>
43 examples/subscriptions.rb
@@ -0,0 +1,43 @@
+$: << File.expand_path(File.dirname(__FILE__) + '/../lib')
+
+require 'chargify_api_ares'
+
+# You could load your credentials from a file...
+chargify_config = YAML::load_file(File.join(File.dirname(__FILE__), '..', 'config', 'chargify.yml'))
+
+Chargify.configure do |c|
+ c.subdomain = chargify_config['subdomain']
+ c.api_key = chargify_config['api_key']
+end
+
+
+# Retrieve a list of all your customers
+subscription = Chargify::Subscription.find(:all)
+# => [#<Chargify::Subscription:0x1020fff70 @prefix_options={}, @attributes={"cancellation_message"=>nil, "activated_at"=>Tue Nov 17 15:51:42 UTC 2009, "expires_at"=>nil, "updated_at"=>Tue Nov 17 15:51:41 UTC 2009, "credit_card"=>#<Chargify::Subscription::CreditCard:0x1020f24d8 @prefix_options={}, @attributes={"card_type"=>"bogus", "expiration_year"=>2015, "masked_card_number"=>"XXXX-XXXX-XXXX-2", "first_name"=>"Michael", "expiration_month"=>1, "last_name"=>"Klett"}>, "product"=>#<Chargify::Product:0x1020f5a70 @prefix_options={}, @attributes={"price_in_cents"=>0, "name"=>"Chargify API Ares Test", "handle"=>"chargify-api-ares-test", "product_family"=>#<Chargify::Product::ProductFamily:0x1020f48f0 @prefix_options={}, @attributes={"name"=>"Chargify API ARes Test", "handle"=>"chargify-api-ares-test", "id"=>79, "accounting_code"=>nil}>, "id"=>153, "accounting_code"=>nil, "interval_unit"=>"month", "interval"=>1}>, "trial_ended_at"=>nil, "id"=>328, "current_period_ends_at"=>Thu Dec 17 15:51:41 UTC 2009, "trial_started_at"=>nil, "customer"=>#<Chargify::Customer:0x1020f6d80 @prefix_options={}, @attributes={"reference"=>"moklett", "updated_at"=>Tue Nov 17 15:51:02 UTC 2009, "id"=>331, "first_name"=>"Michael", "organization"=>"Chargify", "last_name"=>"Klett", "email"=>"moklett@example.com", "created_at"=>Tue Nov 17 15:51:02 UTC 2009}>, "balance_in_cents"=>0, "current_period_started_at"=>Tue Nov 17 15:51:41 UTC 2009, "state"=>"canceled", "created_at"=>Tue Nov 17 15:51:41 UTC 2009}>]]
+
+
+# Create a subscription from a customer reference
+subscription = Chargify::Subscription.create(
+ :customer_reference => 'moklett',
+ :product_handle => 'chargify-api-ares-test',
+ :credit_card_attributes => {
+ :first_name => "Michael",
+ :last_name => "Klett",
+ :expiration_month => 1,
+ :expiration_year => 2020,
+ :full_number => "1"
+ }
+)
+# => #<Chargify::Subscription:0x1020ed4b0 @prefix_options={}, @attributes={"cancellation_message"=>nil, "activated_at"=>Tue Nov 17 16:00:17 UTC 2009, "expires_at"=>nil, "updated_at"=>Tue Nov 17 16:00:17 UTC 2009, "credit_card"=>#<Chargify::Subscription::CreditCard:0x102046b10 @prefix_options={}, @attributes={"card_type"=>"bogus", "expiration_year"=>2020, "masked_card_number"=>"XXXX-XXXX-XXXX-1", "first_name"=>"Michael", "expiration_month"=>1, "last_name"=>"Klett"}>, "product"=>#<Chargify::Product:0x10204a2d8 @prefix_options={}, @attributes={"price_in_cents"=>0, "name"=>"Chargify API Ares Test", "handle"=>"chargify-api-ares-test", "product_family"=>#<Chargify::Product::ProductFamily:0x1020490b8 @prefix_options={}, @attributes={"name"=>"Chargify API ARes Test", "handle"=>"chargify-api-ares-test", "id"=>79, "accounting_code"=>nil}>, "id"=>153, "accounting_code"=>nil, "interval_unit"=>"month", "interval"=>1}>, "credit_card_attributes"=>#<Chargify::Subscription::CreditCardAttributes:0x1020ecab0 @prefix_options={}, @attributes={"expiration_year"=>2020, "full_number"=>"1", "first_name"=>"Michael", "expiration_month"=>1, "last_name"=>"Klett"}>, "trial_ended_at"=>nil, "id"=>331, "current_period_ends_at"=>Thu Dec 17 16:00:17 UTC 2009, "product_handle"=>"chargify-api-ares-test", "trial_started_at"=>nil, "customer"=>#<Chargify::Customer:0x10204b688 @prefix_options={}, @attributes={"reference"=>"moklett", "updated_at"=>Tue Nov 17 15:51:02 UTC 2009, "id"=>331, "first_name"=>"Michael", "organization"=>"Chargify", "last_name"=>"Klett", "email"=>"moklett@example.com", "created_at"=>Tue Nov 17 15:51:02 UTC 2009}>, "balance_in_cents"=>0, "current_period_started_at"=>Tue Nov 17 16:00:17 UTC 2009, "state"=>"active", "created_at"=>Tue Nov 17 16:00:17 UTC 2009, "customer_reference"=>"moklett"}>
+
+
+# Update credit card information
+subscription.credit_card_attributes = {:full_number => "2", :expiration_year => "2015"}
+subscription.save
+# => #<Chargify::Subscription:0x1020ed4b0 @prefix_options={}, @attributes={"cancellation_message"=>nil, "activated_at"=>Tue Nov 17 16:00:17 UTC 2009, "expires_at"=>nil, "updated_at"=>Tue Nov 17 16:00:17 UTC 2009, "credit_card"=>#<Chargify::Subscription::CreditCard:0x1023ba878 @prefix_options={}, @attributes={"card_type"=>"bogus", "expiration_year"=>2015, "masked_card_number"=>"XXXX-XXXX-XXXX-2", "first_name"=>"Michael", "expiration_month"=>1, "last_name"=>"Klett"}>, "product"=>#<Chargify::Product:0x1023baa80 @prefix_options={}, @attributes={"price_in_cents"=>0, "name"=>"Chargify API Ares Test", "handle"=>"chargify-api-ares-test", "product_family"=>#<Chargify::Product::ProductFamily:0x1023c04d0 @prefix_options={}, @attributes={"name"=>"Chargify API ARes Test", "handle"=>"chargify-api-ares-test", "id"=>79, "accounting_code"=>nil}>, "id"=>153, "accounting_code"=>nil, "interval_unit"=>"month", "interval"=>1}>, "credit_card_attributes"=>{:full_number=>"2", :expiration_year=>"2015"}, "trial_ended_at"=>nil, "id"=>331, "current_period_ends_at"=>Thu Dec 17 16:00:17 UTC 2009, "product_handle"=>"chargify-api-ares-test", "customer"=>#<Chargify::Customer:0x1023bae40 @prefix_options={}, @attributes={"reference"=>"moklett", "updated_at"=>Tue Nov 17 15:51:02 UTC 2009, "id"=>331, "first_name"=>"Michael", "organization"=>"Chargify", "last_name"=>"Klett", "email"=>"moklett@example.com", "created_at"=>Tue Nov 17 15:51:02 UTC 2009}>, "trial_started_at"=>nil, "balance_in_cents"=>0, "current_period_started_at"=>Tue Nov 17 16:00:17 UTC 2009, "state"=>"active", "created_at"=>Tue Nov 17 16:00:17 UTC 2009, "customer_reference"=>"moklett"}>
+
+
+# Cancel a subscription
+subscription.cancel
+subscription.reload
+# => #<Chargify::Subscription:0x1020ed4b0 @prefix_options={}, @attributes={"cancellation_message"=>nil, "activated_at"=>Tue Nov 17 16:00:17 UTC 2009, "expires_at"=>nil, "updated_at"=>Tue Nov 17 16:00:17 UTC 2009, "credit_card"=>#<Chargify::Subscription::CreditCard:0x10234f168 @prefix_options={}, @attributes={"card_type"=>"bogus", "expiration_year"=>2015, "masked_card_number"=>"XXXX-XXXX-XXXX-2", "first_name"=>"Michael", "expiration_month"=>1, "last_name"=>"Klett"}>, "product"=>#<Chargify::Product:0x10234f370 @prefix_options={}, @attributes={"price_in_cents"=>0, "name"=>"Chargify API Ares Test", "handle"=>"chargify-api-ares-test", "product_family"=>#<Chargify::Product::ProductFamily:0x102354708 @prefix_options={}, @attributes={"name"=>"Chargify API ARes Test", "handle"=>"chargify-api-ares-test", "id"=>79, "accounting_code"=>nil}>, "id"=>153, "accounting_code"=>nil, "interval_unit"=>"month", "interval"=>1}>, "credit_card_attributes"=>{:full_number=>"2", :expiration_year=>"2015"}, "trial_ended_at"=>nil, "id"=>331, "current_period_ends_at"=>Thu Dec 17 16:00:17 UTC 2009, "product_handle"=>"chargify-api-ares-test", "customer"=>#<Chargify::Customer:0x10234f730 @prefix_options={}, @attributes={"reference"=>"moklett", "updated_at"=>Tue Nov 17 15:51:02 UTC 2009, "id"=>331, "first_name"=>"Michael", "organization"=>"Chargify", "last_name"=>"Klett", "email"=>"moklett@example.com", "created_at"=>Tue Nov 17 15:51:02 UTC 2009}>, "trial_started_at"=>nil, "balance_in_cents"=>0, "current_period_started_at"=>Tue Nov 17 16:00:17 UTC 2009, "state"=>"canceled", "created_at"=>Tue Nov 17 16:00:17 UTC 2009, "customer_reference"=>"moklett"}>
103 lib/chargify_api_ares.rb
@@ -0,0 +1,103 @@
+# Chargify API Wrapper using ActiveResource.
+#
+begin
+ require 'activeresource'
+rescue LoadError
+ begin
+ require 'rubygems'
+ require 'activeresource'
+ rescue LoadError
+ abort <<-ERROR
+The 'activeresource' library could not be loaded. If you have RubyGems
+installed you can install ActiveResource by doing "gem install activeresource".
+ERROR
+ end
+end
+
+
+# Version check
+module Chargify
+ ARES_VERSION = '2.3.4'
+end
+require 'active_resource/version'
+unless ActiveResource::VERSION::STRING == Chargify::ARES_VERSION
+ abort <<-ERROR
+ ActiveResource version #{Chargify::ARES_VERSION} is required.
+ ERROR
+end
+
+
+# A patch for ActiveResource until a version after 2.3.4 fixes it.
+module ActiveResource
+ # Errors returned from the API layer were not getting put into our object as of Rails 2.3.4
+ # See http://github.com/rails/rails/commit/1488c6cc9e6237ce794e3c4a6201627b9fd4ca09
+ class Base
+ def save
+ save_without_validation
+ true
+ rescue ResourceInvalid => error
+ case error.response['Content-Type']
+ when /application\/xml/
+ errors.from_xml(error.response.body)
+ when /application\/json/
+ errors.from_json(error.response.body)
+ end
+ false
+ end
+ end
+end
+
+
+module Chargify
+
+ class << self
+ attr_accessor :subdomain, :api_key, :site, :format
+
+ def configure
+ yield self
+
+ Base.user = api_key
+ Base.password = 'X'
+ Base.site = site.blank? ? "https://#{subdomain}.chargify.com" : site
+ end
+ end
+
+ class Base < ActiveResource::Base
+ class << self
+ def element_name
+ name.split(/::/).last.underscore
+ end
+ end
+
+ def to_xml(options = {})
+ options.merge!(:dasherize => false)
+ super
+ end
+ end
+
+ class Customer < Base
+ def self.find_by_reference(reference)
+ find(:all, :params => {:reference => reference})
+ end
+ end
+
+ class Subscription < Base
+ # Strip off nested attributes of associations before saving, or type-mismatch errors will occur
+ def save
+ self.attributes.delete('customer')
+ self.attributes.delete('product')
+ self.attributes.delete('credit_card')
+ super
+ end
+
+ def cancel
+ destroy
+ end
+ end
+
+ class Product < Base
+ def self.find_by_handle(handle)
+ find(:first, :conditions => {:handle => handle})
+ end
+ end
+end

0 comments on commit 5d92e50

Please sign in to comment.