stefl-chargify 0.3.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. data/.gitignore +23 -0
  2. data/.rspec +1 -0
  3. data/Gemfile +20 -0
  4. data/Gemfile.lock +51 -0
  5. data/LICENSE +20 -0
  6. data/README.markdown +46 -0
  7. data/Rakefile +33 -0
  8. data/VERSION +1 -0
  9. data/changelog.md +30 -0
  10. data/lib/chargify/base.rb +89 -0
  11. data/lib/chargify/config.rb +86 -0
  12. data/lib/chargify/customer.rb +101 -0
  13. data/lib/chargify/error.rb +24 -0
  14. data/lib/chargify/parser.rb +13 -0
  15. data/lib/chargify/product.rb +38 -0
  16. data/lib/chargify/product_family.rb +47 -0
  17. data/lib/chargify/subscription.rb +154 -0
  18. data/lib/chargify/transaction.rb +14 -0
  19. data/lib/chargify.rb +18 -0
  20. data/spec/fixtures/charge_subscription.json +5 -0
  21. data/spec/fixtures/charge_subscription_missing_parameters.json +4 -0
  22. data/spec/fixtures/component.json +11 -0
  23. data/spec/fixtures/components.json +24 -0
  24. data/spec/fixtures/customer.json +12 -0
  25. data/spec/fixtures/customers.json +12 -0
  26. data/spec/fixtures/deleted_subscription.json +1 -0
  27. data/spec/fixtures/invalid_subscription.json +48 -0
  28. data/spec/fixtures/list_metered_subscriptions.json +3 -0
  29. data/spec/fixtures/migrate_subscription.json +51 -0
  30. data/spec/fixtures/new_customer.json +12 -0
  31. data/spec/fixtures/product.json +17 -0
  32. data/spec/fixtures/products.json +17 -0
  33. data/spec/fixtures/subscription.json +49 -0
  34. data/spec/fixtures/subscription_not_found.json +1 -0
  35. data/spec/fixtures/subscriptions.json +49 -0
  36. data/spec/spec_helper.rb +27 -0
  37. data/spec/support/fakeweb_stubs.rb +33 -0
  38. data/spec/unit/chargify/config_spec.rb +147 -0
  39. data/spec/unit/chargify/customer_spec.rb +186 -0
  40. data/spec/unit/chargify/parser_spec.rb +7 -0
  41. data/spec/unit/chargify/product_spec.rb +40 -0
  42. data/spec/unit/chargify/subscription_spec.rb +432 -0
  43. data/spec/unit/chargify/transaction_spec.rb +11 -0
  44. data/stefl-chargify.gemspec +102 -0
  45. metadata +180 -0
@@ -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,186 @@
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.should be_a(Hashie::Mash)
23
+ end
24
+
25
+ it "should raise an error if nothing is found" do
26
+ stub_get "https://OU812:x@pengwynn.chargify.com/customers/16.json", "", 404
27
+ lambda {
28
+ Chargify::Customer.find!(16)
29
+ }.should raise_error(Chargify::Error::NotFound)
30
+ end
31
+
32
+ end
33
+
34
+ describe '.find' do
35
+
36
+ it "should return nil if nothing is found" do
37
+ stub_get "https://OU812:x@pengwynn.chargify.com/customers/16.json", "", 404
38
+ Chargify::Customer.find(16).should == nil
39
+ end
40
+
41
+ end
42
+
43
+
44
+ describe '.lookup!' do
45
+
46
+ it "should be able to be found by a <reference_id>" do
47
+ stub_get "https://OU812:x@pengwynn.chargify.com/customers/lookup.json?reference=bradleyjoyce", "customer.json"
48
+ customer = Chargify::Customer.lookup!("bradleyjoyce")
49
+ customer.should be_a(Hashie::Mash)
50
+ end
51
+
52
+ it "should raise an error if nothing is found" do
53
+ stub_get "https://OU812:x@pengwynn.chargify.com/customers/lookup.json?reference=bradleyjoyce", "", 404
54
+ lambda {
55
+ Chargify::Customer.lookup!("bradleyjoyce")
56
+ }.should raise_error(Chargify::Error::NotFound)
57
+ end
58
+
59
+ end
60
+
61
+ describe '.lookup' do
62
+
63
+ it 'should return nil if nothing is found' do
64
+ Chargify::Customer.lookup("bradleyjoyce") == nil
65
+ end
66
+
67
+ end
68
+
69
+
70
+ describe ".all" do
71
+
72
+ before do
73
+ stub_get "https://OU812:x@pengwynn.chargify.com/customers.json", "customers.json"
74
+ end
75
+
76
+ it "should return a list of customers" do
77
+ customers = Chargify::Customer.all
78
+ customers.size.should == 1
79
+ customers.first.reference.should == 'bradleyjoyce'
80
+ customers.first.organization.should == 'Squeejee'
81
+ end
82
+
83
+ end
84
+
85
+ describe '.create!' do
86
+
87
+ it "should create a new customer" do
88
+ stub_post "https://OU812:x@pengwynn.chargify.com/customers.json", "new_customer.json"
89
+ info = {
90
+ :first_name => "Wynn",
91
+ :last_name => "Netherland",
92
+ :email => "wynn@example.com"
93
+ }
94
+ customer = Chargify::Customer.create!(info)
95
+ customer.first_name.should == "Wynn"
96
+ end
97
+
98
+ it "should raise an error if customer creation fails" do
99
+ stub_post "https://OU812:x@pengwynn.chargify.com/customers.json", "", 422
100
+ lambda {
101
+ Chargify::Customer.create!
102
+ }.should raise_error(Chargify::Error::BadRequest)
103
+ end
104
+
105
+ end
106
+
107
+ describe '.create' do
108
+
109
+ it "should return false if creation fails" do
110
+ stub_post "https://OU812:x@pengwynn.chargify.com/customers.json", "", 422
111
+ Chargify::Customer.create.should == false
112
+ end
113
+
114
+ end
115
+
116
+ describe '.find_or_create' do
117
+ it "should create a user if none is found" do
118
+ stub_get "https://OU812:x@pengwynn.chargify.com/customers/lookup.json?reference=bradleyjoyce", "", 404
119
+ info = {
120
+ :first_name => "Wynn",
121
+ :last_name => "Netherland",
122
+ :email => "wynn@example.com",
123
+ :reference => "bradleyjoyce"
124
+ }
125
+ Chargify::Customer.should_receive(:create!)
126
+ Chargify::Customer.find_or_create(info)
127
+ end
128
+
129
+ it "should return the found customer if one exists with the same reference" do
130
+ stub_get "https://OU812:x@pengwynn.chargify.com/customers/lookup.json?reference=bradleyjoyce", "customer.json"
131
+ info = {
132
+ :first_name => "Wynn",
133
+ :last_name => "Netherland",
134
+ :email => "wynn@example.com",
135
+ :reference => "bradleyjoyce"
136
+ }
137
+ Chargify::Customer.should_not_receive(:create!)
138
+ Chargify::Customer.find_or_create(info)
139
+ end
140
+
141
+ end
142
+
143
+ describe '.update!' do
144
+
145
+ it "should update a customer" do
146
+ stub_put "https://OU812:x@pengwynn.chargify.com/customers/16.json", "new_customer.json"
147
+ info = {
148
+ :id => 16,
149
+ :first_name => "Wynn",
150
+ :last_name => "Netherland",
151
+ :email => "wynn@example.com"
152
+ }
153
+ customer = Chargify::Customer.update!(info)
154
+ customer.first_name.should == "Wynn"
155
+ end
156
+
157
+ it "should raise an error if the customer cannot be found" do
158
+ stub_put "https://OU812:x@pengwynn.chargify.com/customers/16.json", "", 404
159
+ lambda {
160
+ Chargify::Customer.update!(:id => 16)
161
+ }.should raise_error(Chargify::Error::NotFound)
162
+ end
163
+
164
+ end
165
+
166
+ describe '.update' do
167
+
168
+ it "should return false if the customer cannot be found" do
169
+ stub_put "https://OU812:x@pengwynn.chargify.com/customers/16.json", "", 404
170
+ Chargify::Customer.update(:id => 16).should == false
171
+ end
172
+
173
+ end
174
+
175
+ describe '.subscriptions' do
176
+
177
+ it "should return a list of customer subscriptions" do
178
+ stub_get "https://OU812:x@pengwynn.chargify.com/customers/16/subscriptions.json", "subscriptions.json"
179
+ subscriptions = Chargify::Customer.subscriptions(16)
180
+ subscriptions.size.should == 1
181
+ subscriptions.first.customer.reference.should == "bradleyjoyce"
182
+ end
183
+
184
+ end
185
+
186
+ end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe Chargify::Parser do
4
+ subject { Chargify::Parser.send(:new, 'body', :json) }
5
+
6
+ it { should respond_to(:parse) }
7
+ end
@@ -0,0 +1,40 @@
1
+ require 'spec_helper'
2
+
3
+ describe Chargify::Product do
4
+
5
+ describe '.all' do
6
+
7
+ it "should return a list of products" do
8
+ stub_get "https://OU812:x@pengwynn.chargify.com/products.json", "products.json"
9
+ products = Chargify::Product.all
10
+ products.first.accounting_code.should == 'TSMO'
11
+ end
12
+
13
+ end
14
+
15
+ describe '.find' do
16
+
17
+ it "should return info for a product" do
18
+ stub_get "https://OU812:x@pengwynn.chargify.com/products/8.json", "product.json"
19
+ product = Chargify::Product.find(8)
20
+ product.accounting_code.should == 'TSMO'
21
+ end
22
+
23
+ it "should return nil if the product does not exist for a product" do
24
+ stub_get "https://OU812:x@pengwynn.chargify.com/products/99.json", "", 404
25
+ Chargify::Product.find(99).should == nil
26
+ end
27
+
28
+ end
29
+
30
+ describe '.find_by_handle' do
31
+
32
+ it "should return info for a product by its handle" do
33
+ stub_get "https://OU812:x@pengwynn.chargify.com/products/handle/tweetsaver.json", "product.json"
34
+ product = Chargify::Product.find_by_handle('tweetsaver')
35
+ product.accounting_code.should == 'TSMO'
36
+ end
37
+
38
+ end
39
+
40
+ end