ryanwood-mousetrap 0.5.6

Sign up to get free protection for your applications and to get access to all the features.
data/lib/mousetrap.rb ADDED
@@ -0,0 +1,25 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+
3
+ begin; require 'rubygems'; rescue LoadError; end
4
+ require 'httparty'
5
+
6
+ module Mousetrap
7
+ API_UNSUPPORTED = "CheddarGetter API doesn't support this."
8
+
9
+ class ApiDown < Exception
10
+ end
11
+
12
+ autoload :Customer, 'mousetrap/customer'
13
+ autoload :Plan, 'mousetrap/plan'
14
+ autoload :Resource, 'mousetrap/resource'
15
+ autoload :Invoice, 'mousetrap/invoice'
16
+ autoload :Subscription, 'mousetrap/subscription'
17
+
18
+ class << self
19
+ attr_accessor :product_code
20
+
21
+ def authenticate(user, password)
22
+ Resource.basic_auth user, password
23
+ end
24
+ end
25
+ end
data/mousetrap.gemspec ADDED
@@ -0,0 +1,90 @@
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{mousetrap}
8
+ s.version = "0.5.6"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Jon Larkowski", "Sandro Turriate", "Wolfram Arnold", "Corey Grusden"]
12
+ s.date = %q{2010-05-20}
13
+ s.description = %q{CheddarGetter API Client in Ruby}
14
+ s.email = %q{jonlarkowski@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "README.textile"
17
+ ]
18
+ s.files = [
19
+ ".document",
20
+ ".gitignore",
21
+ "MIT-LICENSE",
22
+ "README.textile",
23
+ "Rakefile",
24
+ "VERSION",
25
+ "lib/mousetrap.rb",
26
+ "lib/mousetrap/customer.rb",
27
+ "lib/mousetrap/invoice.rb",
28
+ "lib/mousetrap/plan.rb",
29
+ "lib/mousetrap/resource.rb",
30
+ "lib/mousetrap/subscription.rb",
31
+ "mousetrap.gemspec",
32
+ "script/authenticate.rb",
33
+ "script/cheddar_getter.example.yml",
34
+ "script/console",
35
+ "spec/factories.rb",
36
+ "spec/integration/settings.example.yml",
37
+ "spec/integration/smoke_test.rb",
38
+ "spec/integration/spike.rb",
39
+ "spec/mousetrap/customer_spec.rb",
40
+ "spec/mousetrap/plan_spec.rb",
41
+ "spec/mousetrap/resource_spec.rb",
42
+ "spec/mousetrap/subscription_spec.rb",
43
+ "spec/mousetrap_spec.rb",
44
+ "spec/spec.opts",
45
+ "spec/spec_helper.rb",
46
+ "spec/support/fixtures.rb",
47
+ "spec/support/random_data.rb"
48
+ ]
49
+ s.homepage = %q{http://github.com/hashrocket/mousetrap}
50
+ s.rdoc_options = ["--charset=UTF-8"]
51
+ s.require_paths = ["lib"]
52
+ s.rubygems_version = %q{1.3.6}
53
+ s.summary = %q{CheddarGetter API Client in Ruby}
54
+ s.test_files = [
55
+ "spec/factories.rb",
56
+ "spec/integration/smoke_test.rb",
57
+ "spec/integration/spike.rb",
58
+ "spec/mousetrap/customer_spec.rb",
59
+ "spec/mousetrap/plan_spec.rb",
60
+ "spec/mousetrap/resource_spec.rb",
61
+ "spec/mousetrap/subscription_spec.rb",
62
+ "spec/mousetrap_spec.rb",
63
+ "spec/spec_helper.rb",
64
+ "spec/support/fixtures.rb",
65
+ "spec/support/random_data.rb"
66
+ ]
67
+
68
+ if s.respond_to? :specification_version then
69
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
70
+ s.specification_version = 3
71
+
72
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
73
+ s.add_runtime_dependency(%q<httparty>, [">= 0.4.2"])
74
+ s.add_development_dependency(%q<activesupport>, [">= 2.3.3"])
75
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
76
+ s.add_development_dependency(%q<factory_girl>, [">= 1.2.3"])
77
+ else
78
+ s.add_dependency(%q<httparty>, [">= 0.4.2"])
79
+ s.add_dependency(%q<activesupport>, [">= 2.3.3"])
80
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
81
+ s.add_dependency(%q<factory_girl>, [">= 1.2.3"])
82
+ end
83
+ else
84
+ s.add_dependency(%q<httparty>, [">= 0.4.2"])
85
+ s.add_dependency(%q<activesupport>, [">= 2.3.3"])
86
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
87
+ s.add_dependency(%q<factory_girl>, [">= 1.2.3"])
88
+ end
89
+ end
90
+
@@ -0,0 +1,3 @@
1
+ settings = YAML.load_file(File.expand_path('../cheddar_getter.yml', __FILE__))
2
+ Mousetrap.authenticate settings['user'], settings['password']
3
+ Mousetrap.product_code = settings['product_code']
@@ -0,0 +1,3 @@
1
+ user: you@example.com
2
+ password: abc123
3
+ product_code: MOUSETRAP_TEST
data/script/console ADDED
@@ -0,0 +1 @@
1
+ irb -r ubygems -r lib/mousetrap.rb -r factory_girl -r spec/support/random_data.rb -r script/authenticate.rb
data/spec/factories.rb ADDED
@@ -0,0 +1,34 @@
1
+ Factory.define :new_customer, :class => Mousetrap::Customer, :default_strategy => :stub do |f|
2
+ f.email { random_email_address }
3
+ f.first_name { random_string }
4
+ f.last_name { random_string }
5
+ f.company { random_string }
6
+ f.code { |me| me.email }
7
+ f.add_attribute :id, nil
8
+ f.subscription_attributes { Factory.attributes_for :subscription }
9
+ end
10
+
11
+ Factory.define :existing_customer, :parent => :new_customer, :default_strategy => :stub do |f|
12
+ f.add_attribute :id, '2d1244e8-e338-102c-a92d-40402145ee8b'
13
+ end
14
+
15
+ Factory.define :subscription, :class => Mousetrap::Subscription, :default_strategy => :stub do |f|
16
+ f.plan_code 'TEST'
17
+ f.billing_first_name { random_string }
18
+ f.billing_last_name { random_string }
19
+ f.credit_card_number '4111111111111111'
20
+ f.credit_card_expiration_month '12'
21
+ f.credit_card_expiration_year '2012'
22
+ f.billing_address "400 W 7th St\n#200"
23
+ f.billing_city 'Bloomington'
24
+ f.billing_state 'IN'
25
+ f.billing_country 'US'
26
+ f.billing_zip_code '47404'
27
+ end
28
+
29
+ Factory.define :alternate_subscription, :parent => :subscription, :default_strategy => :stub do |f|
30
+ f.credit_card_number '5555555555554444'
31
+ f.credit_card_expiration_month '7'
32
+ f.credit_card_expiration_year '2013'
33
+ f.billing_zip_code '12345'
34
+ end
@@ -0,0 +1,6 @@
1
+ # Replace user and password with your CheddarGetter credentials.
2
+ user: 'lark@example.com'
3
+ password: 'abc123'
4
+
5
+ # Go create a product in the CheddarGetter web admin interface named "mousetrap_test".
6
+ product_code: 'mousetrap_test'
@@ -0,0 +1,355 @@
1
+ require File.expand_path('../../../lib/mousetrap', __FILE__)
2
+
3
+ require 'spec'
4
+ require 'spec/autorun'
5
+ require 'factory_girl'
6
+ require 'active_support'
7
+ require 'yaml'
8
+
9
+ Dir["#{File.dirname(__FILE__)}/../support/**/*.rb"].each {|f| require f}
10
+
11
+ settings = YAML.load_file(File.dirname(__FILE__) + '/settings.yml')
12
+ Mousetrap.authenticate(settings['user'], settings['password'])
13
+ Mousetrap.product_code = settings['product_code']
14
+
15
+ Spec::Runner.configure do |config|
16
+ config.before :suite do
17
+ begin
18
+ Mousetrap::Customer.destroy_all
19
+ rescue
20
+ end
21
+ end
22
+ end
23
+
24
+ shared_examples_for "a Customer record from CheddarGetter" do
25
+ describe "And I get the customer" do
26
+ before :all do
27
+ @api_customer = Mousetrap::Customer[@customer.code]
28
+ end
29
+
30
+ it "Then I should see first name" do
31
+ @api_customer.first_name.should == @customer.first_name
32
+ end
33
+
34
+ it "And I should see last name" do
35
+ @api_customer.last_name.should == @customer.last_name
36
+ end
37
+
38
+ it "And I should see company" do
39
+ @api_customer.company.should == @customer.company
40
+ end
41
+
42
+ it "And I should see the code" do
43
+ @api_customer.code.should == @customer.code
44
+ end
45
+
46
+ it "And I should see the ID" do
47
+ @api_customer.id.should == @customer.id
48
+ end
49
+ end
50
+ end
51
+
52
+ shared_examples_for "an active Subscription record from CheddarGetter" do
53
+ describe "And I get the subscription" do
54
+ before :all do
55
+ @api_customer = Mousetrap::Customer[@customer.code]
56
+ @api_subscription = @api_customer.subscription
57
+ end
58
+
59
+ it "Then ID is set" do
60
+ @api_subscription.id.should be
61
+ end
62
+
63
+ it "Then canceled_at is not set" do
64
+ @api_subscription.canceled_at.should be_nil
65
+ end
66
+
67
+ it "Then created_at is set" do
68
+ @api_subscription.created_at.should be
69
+ end
70
+
71
+ it "Then I should see the credit card expiration" do
72
+ expiration_date_from_api = Date.parse(@api_subscription.credit_card_expiration_date)
73
+ expiration_date = Date.parse("#{@customer.subscription.credit_card_expiration_year}-#{@customer.subscription.credit_card_expiration_month}-31")
74
+ expiration_date_from_api.should == expiration_date
75
+ end
76
+
77
+ it "Then I should see the credit card last four digits" do
78
+ @api_subscription.credit_card_last_four_digits.should == @customer.subscription.credit_card_number[-4..-1]
79
+ end
80
+
81
+ it "Then I should see the credit card type" do
82
+ @api_subscription.credit_card_type.should == (@credit_card_type || 'visa')
83
+ end
84
+ end
85
+ end
86
+
87
+ describe "The Wrapper Gem" do
88
+ describe Mousetrap::Customer do
89
+ describe ".all" do
90
+ describe "Given a few customers on CheddarGetter" do
91
+ before :all do
92
+ 3.times { Factory(:new_customer).save }
93
+ violated "Couldn't save customers" unless Mousetrap::Customer.all.size == 3
94
+ end
95
+
96
+ describe "When I call .all" do
97
+ before :all do
98
+ @all_customers = Mousetrap::Customer.all
99
+ end
100
+
101
+ it "Then I should get all the customers" do
102
+ @all_customers.size.should == 3
103
+ end
104
+ end
105
+ end
106
+ end
107
+
108
+ describe ".create" do
109
+ describe "When I create a customer" do
110
+ before :all do
111
+ attributes = Factory.attributes_for :new_customer
112
+ @customer = Mousetrap::Customer.create attributes
113
+ end
114
+
115
+ it_should_behave_like "a Customer record from CheddarGetter"
116
+ it_should_behave_like "an active Subscription record from CheddarGetter"
117
+ end
118
+
119
+ describe "When I create a customer with international card" do
120
+ before :all do
121
+ attributes = Factory.attributes_for :alt_new_customer
122
+ @customer = Mousetrap::Customer.create attributes_for
123
+ end
124
+
125
+ it_should_behave_like "a Customer record from CheddarGetter"
126
+ it_should_behave_like "an active Subscription record from CheddarGetter"
127
+ end
128
+ end
129
+
130
+ describe ".destroy_all" do
131
+ describe "Given a few customers on CheddarGetter" do
132
+ before :all do
133
+ Mousetrap::Customer.destroy_all
134
+ 3.times { Factory(:new_customer).save }
135
+ violated "Couldn't save customers" unless Mousetrap::Customer.all.size == 3
136
+ end
137
+
138
+ describe "When I call .destroy_all" do
139
+ before :all do
140
+ Mousetrap::Customer.destroy_all
141
+ end
142
+
143
+ it "Then there should be no customers" do
144
+ Mousetrap::Customer.all.size.should == 0
145
+ end
146
+ end
147
+ end
148
+ end
149
+
150
+ describe ".update" do
151
+ describe "Given a customer" do
152
+ before :all do
153
+ @customer = Factory :new_customer
154
+ @api_customer = nil
155
+
156
+ # TODO: figure out why multiple records are being created even though
157
+ # we use "before :all". Until then, here's the kludge...
158
+ if Mousetrap::Customer.all.size == 1
159
+ @api_customer = Mousetrap::Customer.all.first
160
+ @customer = @api_customer
161
+ else
162
+ @customer.save
163
+ @api_customer = Mousetrap::Customer[@customer.code]
164
+ end
165
+ end
166
+
167
+ describe "When I update the customer, with only customer attributes" do
168
+ before :all do
169
+ @api_customer = Mousetrap::Customer[@customer.code]
170
+
171
+ updated_attributes = {
172
+ :first_name => 'new_first',
173
+ :last_name => 'new_last',
174
+ :email => 'new_email@example.com',
175
+ :company => 'new_company'
176
+ }
177
+
178
+ @customer = Mousetrap::Customer.new updated_attributes
179
+ @customer.code = @api_customer.code
180
+ @customer.id = @api_customer.id
181
+
182
+ Mousetrap::Customer.update(@api_customer.code, updated_attributes)
183
+ end
184
+
185
+ it_should_behave_like "a Customer record from CheddarGetter"
186
+ end
187
+
188
+ describe "When I update the customer, with customer and subscription attributes" do
189
+ before :all do
190
+ @api_customer = Mousetrap::Customer[@customer.code]
191
+
192
+ updated_attributes = {
193
+ :first_name => 'new_first',
194
+ :last_name => 'new_last',
195
+ :email => 'new_email@example.com',
196
+ :company => 'new_company',
197
+ :subscription_attributes => {
198
+ :plan_code => 'TEST',
199
+ :billing_first_name => 'new_billing_first',
200
+ :billing_last_name => 'new_billing_last',
201
+ :credit_card_number => '5555555555554444',
202
+ :credit_card_expiration_month => '01',
203
+ :credit_card_expiration_year => '2013',
204
+ :billing_zip_code => '12345'
205
+ }
206
+ }
207
+
208
+ @credit_card_type = 'mc'
209
+
210
+ @customer = Mousetrap::Customer.new updated_attributes
211
+
212
+ # set up our test comparison objects as if they came from API gets
213
+ @customer.code = @api_customer.code
214
+ @customer.id = @api_customer.id
215
+ @customer.subscription.send(:id=, @api_customer.subscription.id)
216
+
217
+ Mousetrap::Customer.update(@api_customer.code, updated_attributes)
218
+ end
219
+
220
+ it_should_behave_like "a Customer record from CheddarGetter"
221
+ it_should_behave_like "an active Subscription record from CheddarGetter"
222
+ end
223
+ end
224
+ end
225
+
226
+ describe "#cancel" do
227
+ describe "Given a customer" do
228
+ before :all do
229
+ @customer = Factory :new_customer
230
+ @api_customer = nil
231
+
232
+ if Mousetrap::Customer.all.size == 1
233
+ @api_customer = Mousetrap::Customer.all.first
234
+ @customer = @api_customer
235
+ else
236
+ @customer.save
237
+ @api_customer = Mousetrap::Customer[@customer.code]
238
+ end
239
+ end
240
+
241
+ describe "When I cancel" do
242
+ before :all do
243
+ @api_customer.cancel
244
+ end
245
+
246
+ describe "And I get the customer" do
247
+ before :all do
248
+ @api_customer = Mousetrap::Customer[@customer.code]
249
+ end
250
+
251
+ it "Then I should see a cancellation date on subscription" do
252
+ @api_customer.subscription.canceled_at.should be
253
+ end
254
+
255
+ describe "When I resubscribe them" do
256
+ before :all do
257
+ @customer = Factory :new_customer, :email => @api_customer.email, :code => @api_customer.email
258
+ @customer.save
259
+ end
260
+
261
+ it_should_behave_like "a Customer record from CheddarGetter"
262
+ it_should_behave_like "an active Subscription record from CheddarGetter"
263
+ end
264
+ end
265
+ end
266
+ end
267
+ end
268
+
269
+ describe "#save" do
270
+ describe "When I save a customer" do
271
+ before :all do
272
+ @customer = Factory :new_customer
273
+ @customer.save
274
+ end
275
+
276
+ it_should_behave_like "a Customer record from CheddarGetter"
277
+
278
+ describe "When I save it again, with different attributes" do
279
+ before :all do
280
+ attributes = Factory.attributes_for :new_customer
281
+ @customer.first_name = attributes[:first_name]
282
+ @customer.last_name = attributes[:last_name]
283
+ @customer.email = attributes[:email]
284
+ @customer.company = attributes[:company]
285
+ @customer.save
286
+ end
287
+
288
+ it_should_behave_like "a Customer record from CheddarGetter"
289
+ end
290
+
291
+ context "When I update subscription information" do
292
+ before :all do
293
+ @subscription = Mousetrap::Subscription.new(Factory.attributes_for(:alternate_subscription))
294
+ @credit_card_type = 'mc'
295
+ @customer.subscription = @subscription
296
+ @customer.save
297
+ end
298
+
299
+ it_should_behave_like "an active Subscription record from CheddarGetter"
300
+ end
301
+ end
302
+ end
303
+
304
+ describe '#switch_to_plan' do
305
+ describe "Given an existing CheddarGetter customer" do
306
+ before :all do
307
+ @customer = Factory :new_customer
308
+ @customer.save
309
+ end
310
+
311
+ describe 'When I switch plans' do
312
+ before :all do
313
+ @customer.switch_to_plan('TEST_2')
314
+ end
315
+
316
+ describe "And I get the customer" do
317
+ before :all do
318
+ @api_customer = Mousetrap::Customer[@customer.code]
319
+ end
320
+
321
+ it 'Then they should be on the new plan' do
322
+ @api_customer.subscription.plan.code.should == 'TEST_2'
323
+ end
324
+ end
325
+ end
326
+ end
327
+ end
328
+ end
329
+
330
+ describe Mousetrap::Subscription do
331
+ describe "Given a customer on CheddarGetter" do
332
+ before :all do
333
+ @customer = Factory :new_customer
334
+ violated "Use a visa for setup" unless @customer.subscription.credit_card_number == '4111111111111111'
335
+ @customer.save
336
+ end
337
+
338
+ describe "When I update a subscription field" do
339
+ before :all do
340
+ Mousetrap::Subscription.update @customer.code, :credit_card_number => '5555555555554444'
341
+ end
342
+
343
+ describe "And I get the customer" do
344
+ before :all do
345
+ @api_customer = Mousetrap::Customer[@customer.code]
346
+ end
347
+
348
+ it 'Then I should see the updated field' do
349
+ @api_customer.subscription.credit_card_last_four_digits.should == '4444'
350
+ end
351
+ end
352
+ end
353
+ end
354
+ end
355
+ end
@@ -0,0 +1,108 @@
1
+ # This script is for hacking and playing around.
2
+
3
+ require File.dirname(__FILE__) + '/../../lib/mousetrap'
4
+ require 'activesupport'
5
+ require 'factory_girl'
6
+ require 'ruby-debug'
7
+ require 'yaml'
8
+
9
+ Dir["#{File.dirname(__FILE__)}/../support/**/*.rb"].each {|f| require f}
10
+
11
+ settings = YAML.load_file(File.dirname(__FILE__) + '/settings.yml')
12
+ Mousetrap.authenticate(settings['user'], settings['password'])
13
+ Mousetrap.product_code = settings['product_code']
14
+
15
+
16
+ def plans
17
+ all_plans = Mousetrap::Plan.all
18
+ puts all_plans.to_yaml
19
+
20
+ test_plan = Mousetrap::Plan['TEST']
21
+ puts test_plan.to_yaml
22
+ end
23
+
24
+ def all_customers
25
+ all_customers = Mousetrap::Customer.all
26
+ puts all_customers.to_yaml
27
+ end
28
+
29
+ def create_customer
30
+ customer = Factory :new_customer
31
+ customer.save
32
+
33
+ api_customer = Mousetrap::Customer[customer.code]
34
+ puts api_customer.to_yaml
35
+ end
36
+
37
+ def destroy_all_customers
38
+ Mousetrap::Customer.destroy_all
39
+ end
40
+ #create_customer
41
+
42
+ destroy_all_customers
43
+ customer = Factory :new_customer
44
+ customer.save
45
+
46
+ api_customer = Mousetrap::Customer[customer.code]
47
+ puts api_customer.to_yaml
48
+ puts '-' * 80
49
+
50
+ Mousetrap::Subscription.update customer.code, {
51
+ :billing_first_name => 'x',
52
+ :billing_last_name => 'y',
53
+ :credit_card_number => '5555555555554444',
54
+ :credit_card_expiration_month => '05',
55
+ :credit_card_expiration_year => '2013',
56
+ :billing_zip_code => '31415'
57
+ }
58
+
59
+ api_customer = Mousetrap::Customer[customer.code]
60
+ puts api_customer.to_yaml
61
+ puts '-' * 80
62
+
63
+ #customer_only_fields = Mousetrap::Customer.new \
64
+ #:first_name => 'first',
65
+ #:last_name => 'last',
66
+ #:company => 'company',
67
+ #:email => 'random@example.com',
68
+ #:code => customer.code
69
+
70
+ #customer_only_fields.save
71
+
72
+ #api_customer = Mousetrap::Customer[customer.code]
73
+ #puts api_customer.to_yaml
74
+
75
+
76
+ #code = "rvhljmvenp@example.com"
77
+ #api_customer = Mousetrap::Customer[code]
78
+ ##puts api_customer.to_yaml
79
+
80
+ #customer = Factory :new_customer
81
+ #customer.save
82
+ #api_customer = Mousetrap::Customer[customer.code]
83
+
84
+ #puts '-' * 80
85
+ #p Mousetrap::Customer[customer.code]
86
+
87
+ #puts '-' * 80
88
+ #p Mousetrap::Customer.exists? customer.code
89
+
90
+ #puts '-' * 80
91
+ #p Mousetrap::Customer['cantfindme']
92
+
93
+ #puts '-' * 80
94
+ #p Mousetrap::Customer.exists? 'cantfindme'
95
+
96
+ # create_customer
97
+
98
+ #code = 'igcuvfehrc@example.com'
99
+ #api_customer = Mousetrap::Customer[code]
100
+ #puts api_customer.to_yaml
101
+ #puts '-' * 80
102
+ #puts 'cancel'
103
+ #puts api_customer.cancel
104
+ #puts '-' * 80
105
+ #api_customer = Mousetrap::Customer[code]
106
+ #puts api_customer.to_yaml
107
+
108
+