jsmestad-chargify 0.3.0.pre5

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -0,0 +1,17 @@
1
+ {
2
+ "product": {
3
+ "price_in_cents": 500,
4
+ "name": "Monthly",
5
+ "handle": "monthly",
6
+ "id": 8,
7
+ "accounting_code": "TSMO",
8
+ "product_family": {
9
+ "name": "TweetSaver",
10
+ "handle": "tweetsaver",
11
+ "id": 7,
12
+ "accounting_code": null
13
+ },
14
+ "interval_unit": "month",
15
+ "interval": 1
16
+ }
17
+ }
@@ -0,0 +1,17 @@
1
+ [{
2
+ "product": {
3
+ "price_in_cents": 500,
4
+ "name": "Monthly",
5
+ "handle": "monthly",
6
+ "id": 8,
7
+ "accounting_code": "TSMO",
8
+ "product_family": {
9
+ "name": "TweetSaver",
10
+ "handle": "tweetsaver",
11
+ "id": 7,
12
+ "accounting_code": null
13
+ },
14
+ "interval_unit": "month",
15
+ "interval": 1
16
+ }
17
+ }]
@@ -0,0 +1,49 @@
1
+ {
2
+ "subscription": {
3
+ "customer": {
4
+ "reference": "bradleyjoyce",
5
+ "updated_at": "2009-10-07T11:10:27-04:00",
6
+ "id": 16,
7
+ "organization": "Squeejee",
8
+ "first_name": "Bradley",
9
+ "last_name": "Joyce",
10
+ "email": "bradley@squeejee.com",
11
+ "created_at": "2009-10-07T11:10:27-04:00"
12
+ },
13
+ "cancellation_message": null,
14
+ "updated_at": "2009-10-07T11:10:56-04:00",
15
+ "expires_at": null,
16
+ "activated_at": "2009-10-23T16:16:59-04:00",
17
+ "current_period_started_at": "2009-11-14T11:10:56-05:00",
18
+ "credit_card": {
19
+ "card_type": "bogus",
20
+ "expiration_year": 2015,
21
+ "masked_card_number": "XXXX-XXXX-XXXX-1",
22
+ "first_name": "Bradley",
23
+ "last_name": "Joyce",
24
+ "expiration_month": 7
25
+ },
26
+ "trial_ended_at": "2009-10-14T11:10:56-04:00",
27
+ "id": 14,
28
+ "product": {
29
+ "price_in_cents": 500,
30
+ "name": "Monthly",
31
+ "handle": "monthly",
32
+ "id": 8,
33
+ "accounting_code": "TSMO",
34
+ "product_family": {
35
+ "name": "TweetSaver",
36
+ "handle": "tweetsaver",
37
+ "id": 7,
38
+ "accounting_code": null
39
+ },
40
+ "interval_unit": "month",
41
+ "interval": 1
42
+ },
43
+ "current_period_ends_at": "2009-12-14T11:10:56-05:00",
44
+ "trial_started_at": "2009-10-07T11:10:56-04:00",
45
+ "balance_in_cents": 0,
46
+ "state": "active",
47
+ "created_at": "2009-10-07T11:10:56-04:00"
48
+ }
49
+ }
@@ -0,0 +1 @@
1
+ \r\n
@@ -0,0 +1,49 @@
1
+ [{
2
+ "subscription": {
3
+ "customer": {
4
+ "reference": "bradleyjoyce",
5
+ "updated_at": "2009-10-07T11:10:27-04:00",
6
+ "id": 16,
7
+ "organization": "Squeejee",
8
+ "first_name": "Bradley",
9
+ "last_name": "Joyce",
10
+ "email": "bradley@squeejee.com",
11
+ "created_at": "2009-10-07T11:10:27-04:00"
12
+ },
13
+ "cancellation_message": null,
14
+ "updated_at": "2009-10-07T11:10:56-04:00",
15
+ "expires_at": null,
16
+ "activated_at": "2009-10-23T16:16:59-04:00",
17
+ "current_period_started_at": "2009-11-14T11:10:56-05:00",
18
+ "credit_card": {
19
+ "card_type": "bogus",
20
+ "expiration_year": 2015,
21
+ "masked_card_number": "XXXX-XXXX-XXXX-1",
22
+ "first_name": "Bradley",
23
+ "last_name": "Joyce",
24
+ "expiration_month": 7
25
+ },
26
+ "trial_ended_at": "2009-10-14T11:10:56-04:00",
27
+ "id": 14,
28
+ "product": {
29
+ "price_in_cents": 500,
30
+ "name": "Monthly",
31
+ "handle": "monthly",
32
+ "id": 8,
33
+ "accounting_code": "TSMO",
34
+ "product_family": {
35
+ "name": "TweetSaver",
36
+ "handle": "tweetsaver",
37
+ "id": 7,
38
+ "accounting_code": null
39
+ },
40
+ "interval_unit": "month",
41
+ "interval": 1
42
+ },
43
+ "current_period_ends_at": "2009-12-14T11:10:56-05:00",
44
+ "trial_started_at": "2009-10-07T11:10:56-04:00",
45
+ "balance_in_cents": 0,
46
+ "state": "active",
47
+ "created_at": "2009-10-07T11:10:56-04:00"
48
+ }
49
+ }]
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,26 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+
4
+ Bundler.require(:default, :runtime, :test)
5
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
6
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
7
+
8
+ require 'chargify'
9
+ require 'spec'
10
+ require 'spec/autorun'
11
+
12
+ # Requires supporting files with custom matchers and macros, etc,
13
+ # in ./support/ and its subdirectories.
14
+ Dir[File.expand_path(File.join(File.dirname(__FILE__),'support','**','*.rb'))].each {|f| require f}
15
+
16
+ FakeWeb.allow_net_connect = false
17
+
18
+ Spec::Runner.configure do |config|
19
+ # config.include(Rack::Test::Methods)
20
+
21
+ config.before :suite do
22
+
23
+ end
24
+
25
+ end
26
+
@@ -0,0 +1,33 @@
1
+ def fixture_file(filename)
2
+ return '' if filename == ''
3
+ file_path = File.join(File.dirname(__FILE__), '..', 'fixtures', filename)
4
+ File.read(file_path)
5
+ end
6
+
7
+ def stub_get(url, filename, status=nil)
8
+ options = {:body => fixture_file(filename)}
9
+ options.merge!({:status => status}) unless status.nil?
10
+
11
+ FakeWeb.register_uri(:get, url, options)
12
+ end
13
+
14
+ def stub_post(url, filename, status=nil)
15
+ options = {:body => fixture_file(filename)}
16
+ options.merge!({:status => status}) unless status.nil?
17
+
18
+ FakeWeb.register_uri(:post, url, options)
19
+ end
20
+
21
+ def stub_put(url, filename, status=nil)
22
+ options = {:body => fixture_file(filename)}
23
+ options.merge!({:status => status}) unless status.nil?
24
+
25
+ FakeWeb.register_uri(:put, url, options)
26
+ end
27
+
28
+ def stub_delete(url, filename, status=nil)
29
+ options = {:body => fixture_file(filename)}
30
+ options.merge!({:status => status}) unless status.nil?
31
+
32
+ FakeWeb.register_uri(:delete, url, options)
33
+ end
@@ -0,0 +1,147 @@
1
+ require 'spec_helper'
2
+
3
+ describe Chargify::Config do
4
+
5
+ before :each do
6
+
7
+ Chargify::Config.clear
8
+ Chargify::Config.setup do |c|
9
+ c[:one] = 1
10
+ c[:two] = 2
11
+ end
12
+
13
+ end
14
+
15
+ describe "logger" do
16
+
17
+ before :each do
18
+ Object.send(:remove_const, :RAILS_DEFAULT_LOGGER) if defined?(RAILS_DEFAULT_LOGGER)
19
+ end
20
+
21
+ it "should default to RAILS_DEFAULT_LOGGER if defined" do
22
+ pending
23
+ Rails.logger = "something"
24
+ Chargify::Config.reset
25
+ Chargify::Config.logger.should == "something"
26
+ end
27
+
28
+ it "should default to a Logger if RAILS_DEFAULT_LOGGER is not defined" do
29
+ Chargify::Config.reset
30
+ Chargify::Config.logger.should be_a(Logger)
31
+ end
32
+
33
+ end
34
+
35
+ describe "configuration" do
36
+
37
+ it "should return the configuration hash" do
38
+ Chargify::Config.configuration.should == {:one => 1, :two => 2}
39
+ end
40
+
41
+ end
42
+
43
+ describe "[]" do
44
+
45
+ it "should return the config option matching the key" do
46
+ Chargify::Config[:one].should == 1
47
+ end
48
+
49
+ it "should return nil if the key doesn't exist" do
50
+ Chargify::Config[:monkey].should be_nil
51
+ end
52
+
53
+ end
54
+
55
+ describe "[]=" do
56
+
57
+ it "should set the config option for the key" do
58
+ lambda{
59
+ Chargify::Config[:banana] = :yellow
60
+ }.should change(Chargify::Config, :banana).from(nil).to(:yellow)
61
+ end
62
+
63
+ end
64
+
65
+ describe "delete" do
66
+
67
+ it "should delete the config option for the key" do
68
+ lambda{
69
+ Chargify::Config.delete(:one)
70
+ }.should change(Chargify::Config, :one).from(1).to(nil)
71
+ end
72
+
73
+ it "should leave the config the same if the key doesn't exist" do
74
+ lambda{
75
+ Chargify::Config.delete(:test)
76
+ }.should_not change(Chargify::Config, :configuration)
77
+ end
78
+
79
+ end
80
+
81
+ describe "fetch" do
82
+
83
+ it "should return the config option matching the key if it exists" do
84
+ Chargify::Config.fetch(:one, 100).should == 1
85
+ end
86
+
87
+ it "should return the config default if the key doesn't exist" do
88
+ Chargify::Config.fetch(:other, 100).should == 100
89
+ end
90
+
91
+ end
92
+
93
+ describe "to_hash" do
94
+
95
+ it "should return a hash of the configuration" do
96
+ Chargify::Config.to_hash.should == {:one => 1, :two => 2}
97
+ end
98
+
99
+ end
100
+
101
+ describe "setup" do
102
+
103
+ it "should yield self" do
104
+ Chargify::Config.setup do |c|
105
+ c.should == Chargify::Config
106
+ end
107
+ end
108
+
109
+ it "should let you set items on the configuration object as a hash" do
110
+ lambda{
111
+ Chargify::Config.setup do |c|
112
+ c[:bananas] = 100
113
+ end
114
+ }.should change(Chargify::Config, :bananas).from(nil).to(100)
115
+ end
116
+
117
+ it "should let you set items on the configuration object as a method" do
118
+ lambda{
119
+ Chargify::Config.setup do |c|
120
+ c.monkeys = 100
121
+ end
122
+ }.should change(Chargify::Config, :monkeys).from(nil).to(100)
123
+ end
124
+
125
+ end
126
+
127
+ describe "calling a missing method" do
128
+
129
+ it "should retreive the config if the method matches a key" do
130
+ Chargify::Config.one.should == 1
131
+ end
132
+
133
+ it "should retreive nil if the method doesn't match a key" do
134
+ Chargify::Config.moo.should be_nil
135
+ end
136
+
137
+ it "should set the value of the config item matching the method name if it's an assignment" do
138
+ lambda{
139
+ Chargify::Config.trees = 3
140
+ }.should change(Chargify::Config, :trees).from(nil).to(3)
141
+ end
142
+
143
+ end
144
+
145
+ end
146
+
147
+
@@ -0,0 +1,111 @@
1
+ require 'spec_helper'
2
+
3
+ describe Chargify::Customer do
4
+
5
+ before(:all) do
6
+ Chargify::Config.setup do |config|
7
+ config[:api_key] = 'OU812'
8
+ config[:subdomain] = 'pengwynn'
9
+ end
10
+ end
11
+
12
+ describe '.find' do
13
+
14
+ it 'should pass to Chargified::Customer.all' do
15
+ Chargify::Customer.should_receive(:all)
16
+ Chargify::Customer.find(:all)
17
+ end
18
+
19
+ it "should be able to be found by a <chargify_id>" do
20
+ stub_get "https://OU812:x@pengwynn.chargify.com/customers/16.json", "customer.json"
21
+ customer = Chargify::Customer.find(16)
22
+ customer.success?.should == true
23
+ end
24
+
25
+ it "should return an empty Hash with success? set to false" do
26
+ stub_get "https://OU812:x@pengwynn.chargify.com/customers/16.json", "", 404
27
+ customer = Chargify::Customer.find(16)
28
+ customer.success?.should == false
29
+ end
30
+
31
+ end
32
+
33
+
34
+ describe '.lookup' do
35
+
36
+ before do
37
+ stub_get "https://OU812:x@pengwynn.chargify.com/customers/lookup.json?reference=bradleyjoyce", "customer.json"
38
+ end
39
+
40
+ it "should be able to be found by a <reference_id>" do
41
+ customer = Chargify::Customer.lookup("bradleyjoyce")
42
+ customer.success?.should == true
43
+ end
44
+
45
+ end
46
+
47
+
48
+ describe ".all" do
49
+
50
+ before do
51
+ stub_get "https://OU812:x@pengwynn.chargify.com/customers.json", "customers.json"
52
+ end
53
+
54
+ it "should return a list of customers" do
55
+ customers = Chargify::Customer.all
56
+ customers.size.should == 1
57
+ customers.first.reference.should == 'bradleyjoyce'
58
+ customers.first.organization.should == 'Squeejee'
59
+ end
60
+
61
+ end
62
+
63
+ describe '.create' do
64
+
65
+ before do
66
+ stub_post "https://OU812:x@pengwynn.chargify.com/customers.json", "new_customer.json"
67
+ end
68
+
69
+ it "should create a new customer" do
70
+ info = {
71
+ :first_name => "Wynn",
72
+ :last_name => "Netherland",
73
+ :email => "wynn@example.com"
74
+ }
75
+ customer = Chargify::Customer.create(info)
76
+ customer.first_name.should == "Wynn"
77
+ end
78
+
79
+ end
80
+
81
+ describe '.update' do
82
+
83
+ before do
84
+ stub_put "https://OU812:x@pengwynn.chargify.com/customers/16.json", "new_customer.json"
85
+ end
86
+
87
+ it "should update a customer" do
88
+ info = {
89
+ :id => 16,
90
+ :first_name => "Wynn",
91
+ :last_name => "Netherland",
92
+ :email => "wynn@example.com"
93
+ }
94
+ customer = Chargify::Customer.update(info)
95
+ customer.first_name.should == "Wynn"
96
+ end
97
+
98
+ end
99
+
100
+ describe '.subscriptions' do
101
+
102
+ it "should return a list of customer subscriptions" do
103
+ stub_get "https://OU812:x@pengwynn.chargify.com/customers/16/subscriptions.json", "subscriptions.json"
104
+ subscriptions = Chargify::Customer.subscriptions(16)
105
+ subscriptions.size.should == 1
106
+ subscriptions.first.customer.reference.should == "bradleyjoyce"
107
+ end
108
+
109
+ end
110
+
111
+ end