mousetrap 0.3.3

Sign up to get free protection for your applications and to get access to all the features.
data/mousetrap.gemspec ADDED
@@ -0,0 +1,83 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{mousetrap}
8
+ s.version = "0.3.3"
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{2009-10-12}
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/plan.rb",
28
+ "lib/mousetrap/resource.rb",
29
+ "lib/mousetrap/subscription.rb",
30
+ "mousetrap.gemspec",
31
+ "spec/factories.rb",
32
+ "spec/integration/settings.example.yml",
33
+ "spec/integration/smoke_test.rb",
34
+ "spec/integration/spike.rb",
35
+ "spec/mousetrap/customer_spec.rb",
36
+ "spec/mousetrap/plan_spec.rb",
37
+ "spec/mousetrap/resource_spec.rb",
38
+ "spec/mousetrap/subscription_spec.rb",
39
+ "spec/mousetrap_spec.rb",
40
+ "spec/spec.opts",
41
+ "spec/spec_helper.rb",
42
+ "spec/support/random_data.rb"
43
+ ]
44
+ s.homepage = %q{http://github.com/hashrocket/mousetrap}
45
+ s.rdoc_options = ["--charset=UTF-8"]
46
+ s.require_paths = ["lib"]
47
+ s.rubygems_version = %q{1.3.5}
48
+ s.summary = %q{CheddarGetter API Client in Ruby}
49
+ s.test_files = [
50
+ "spec/factories.rb",
51
+ "spec/integration/smoke_test.rb",
52
+ "spec/integration/spike.rb",
53
+ "spec/mousetrap/customer_spec.rb",
54
+ "spec/mousetrap/plan_spec.rb",
55
+ "spec/mousetrap/resource_spec.rb",
56
+ "spec/mousetrap/subscription_spec.rb",
57
+ "spec/mousetrap_spec.rb",
58
+ "spec/spec_helper.rb",
59
+ "spec/support/random_data.rb"
60
+ ]
61
+
62
+ if s.respond_to? :specification_version then
63
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
64
+ s.specification_version = 3
65
+
66
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
67
+ s.add_runtime_dependency(%q<httparty>, [">= 0.4.2"])
68
+ s.add_development_dependency(%q<activesupport>, [">= 2.3.3"])
69
+ s.add_development_dependency(%q<rspec>, [">= 1.2.8"])
70
+ s.add_development_dependency(%q<thoughtbot-factory_girl>, [">= 1.2.2"])
71
+ else
72
+ s.add_dependency(%q<httparty>, [">= 0.4.2"])
73
+ s.add_dependency(%q<activesupport>, [">= 2.3.3"])
74
+ s.add_dependency(%q<rspec>, [">= 1.2.8"])
75
+ s.add_dependency(%q<thoughtbot-factory_girl>, [">= 1.2.2"])
76
+ end
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.8"])
81
+ s.add_dependency(%q<thoughtbot-factory_girl>, [">= 1.2.2"])
82
+ end
83
+ end
data/spec/factories.rb ADDED
@@ -0,0 +1,22 @@
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.code { |me| me.email }
6
+ f.add_attribute :id, nil
7
+ f.subscription_attributes { Factory.attributes_for :subscription }
8
+ end
9
+
10
+ Factory.define :existing_customer, :parent => :new_customer, :default_strategy => :stub do |f|
11
+ f.add_attribute :id, '2d1244e8-e338-102c-a92d-40402145ee8b'
12
+ end
13
+
14
+ Factory.define :subscription, :class => Mousetrap::Subscription, :default_strategy => :stub do |f|
15
+ f.plan_code 'TEST'
16
+ f.billing_first_name { random_string }
17
+ f.billing_last_name { random_string }
18
+ f.credit_card_number '4111111111111111'
19
+ f.credit_card_expiration_month '12'
20
+ f.credit_card_expiration_year '2012'
21
+ f.billing_zip_code '90210'
22
+ 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,149 @@
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 the code" do
39
+ @api_customer.code.should == @customer.code
40
+ end
41
+
42
+ it "And I should see the ID" do
43
+ @api_customer.id.should == @customer.id
44
+ end
45
+ end
46
+ end
47
+
48
+ describe "The Wrapper Gem" do
49
+ describe Mousetrap::Customer do
50
+ describe ".all" do
51
+ describe "Given a few customers on CheddarGetter" do
52
+ before :all do
53
+ 3.times { Factory(:new_customer).save }
54
+ violated "Couldn't save customers" unless Mousetrap::Customer.all.size == 3
55
+ end
56
+
57
+ describe "When I call .all" do
58
+ before :all do
59
+ @all_customers = Mousetrap::Customer.all
60
+ end
61
+
62
+ it "Then I should get all the customers" do
63
+ @all_customers.size.should == 3
64
+ end
65
+ end
66
+ end
67
+ end
68
+
69
+ describe ".create" do
70
+ describe "When I create a customer" do
71
+ before :all do
72
+ attributes = Factory.attributes_for :new_customer
73
+ @customer = Mousetrap::Customer.create attributes
74
+ end
75
+
76
+ it_should_behave_like "a Customer record from CheddarGetter"
77
+ end
78
+ end
79
+
80
+ describe ".destroy_all" do
81
+ describe "Given a few customers on CheddarGetter" do
82
+ before :all do
83
+ Mousetrap::Customer.destroy_all
84
+ 3.times { Factory(:new_customer).save }
85
+ violated "Couldn't save customers" unless Mousetrap::Customer.all.size == 3
86
+ end
87
+
88
+ describe "When I call .destroy_all" do
89
+ before :all do
90
+ Mousetrap::Customer.destroy_all
91
+ end
92
+
93
+ it "Then there should be no customers" do
94
+ Mousetrap::Customer.all.size.should == 0
95
+ end
96
+ end
97
+ end
98
+ end
99
+
100
+ describe "#cancel" do
101
+ describe "Given a customer" do
102
+ before :all do
103
+ @customer = Factory :new_customer
104
+ @customer.save
105
+ @api_customer = Mousetrap::Customer[@customer.code]
106
+ end
107
+
108
+ describe "When I cancel" do
109
+ before :all do
110
+ @api_customer.cancel
111
+ end
112
+
113
+ describe "And I get the customer" do
114
+ before :all do
115
+ @api_customer = Mousetrap::Customer[@customer.code]
116
+ end
117
+
118
+ it "Then I should see a cancelation date on subscription" do
119
+ @api_customer.subscription.canceled_at.should be
120
+ end
121
+ end
122
+ end
123
+ end
124
+ end
125
+
126
+ describe "#save" do
127
+ describe "When I save a customer" do
128
+ before :all do
129
+ @customer = Factory :new_customer
130
+ @customer.save
131
+ end
132
+
133
+ it_should_behave_like "a Customer record from CheddarGetter"
134
+
135
+ describe "And I save it again, with different attributes" do
136
+ before :all do
137
+ attributes = Factory.attributes_for :new_customer
138
+ @customer.first_name = attributes[:first_name]
139
+ @customer.last_name = attributes[:last_name]
140
+ @customer.email = attributes[:email]
141
+ @customer.save
142
+ end
143
+
144
+ it_should_behave_like "a Customer record from CheddarGetter"
145
+ end
146
+ end
147
+ end
148
+ end
149
+ end
@@ -0,0 +1,53 @@
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
+
41
+ # create_customer
42
+
43
+ code = 'igcuvfehrc@example.com'
44
+ api_customer = Mousetrap::Customer[code]
45
+ puts api_customer.to_yaml
46
+ puts '-' * 80
47
+ puts 'cancel'
48
+ puts api_customer.cancel
49
+ puts '-' * 80
50
+ api_customer = Mousetrap::Customer[code]
51
+ puts api_customer.to_yaml
52
+
53
+
@@ -0,0 +1,209 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe Mousetrap::Customer do
4
+ # customers:
5
+ # customer:
6
+ # firstName: nvnawrelyv
7
+ # lastName: vklidifvfd
8
+ # email: bvvljaeegs@example.com
9
+ # code: ablntsorai@example.com
10
+ # company:
11
+ # gatewayToken:
12
+ # id: eac1cf0e-fc5b-102c-a92d-40402145ee8b
13
+ # createdDatetime: "2009-09-27T02:16:15+00:00"
14
+ # modifiedDatetime: "2009-09-27T02:16:16+00:00"
15
+ # subscriptions:
16
+ # subscription:
17
+ # gatewayToken:
18
+ # id: eac26b4e-fc5b-102c-a92d-40402145ee8b
19
+ # createdDatetime: "2009-09-27T02:16:15+00:00"
20
+ # ccType: visa
21
+ # ccLastFour: "1111"
22
+ # ccExpirationDate: "2012-12-31T00:00:00+00:00"
23
+ # canceledDatetime:
24
+ # plans:
25
+ # plan:
26
+ # name: Test
27
+ # setupChargeAmount: "0.00"
28
+ # code: TEST
29
+ # recurringChargeAmount: "42.00"
30
+ # billingFrequencyQuantity: "1"
31
+ # trialDays: "0"
32
+ # id: 5fbb9a84-e27f-102c-a92d-40402145ee8b
33
+ # billingFrequency: monthly
34
+ # createdDatetime: "2009-08-25T04:24:34+00:00"
35
+ # recurringChargeCode: TEST_RECURRING
36
+ # isActive: "1"
37
+ # billingFrequencyUnit: months
38
+ # description: Test
39
+ # billingFrequencyPer: month
40
+ # setupChargeCode: TEST_SETUP
41
+ # invoices:
42
+ # invoice:
43
+ # number: "2"
44
+ # billingDatetime: "2009-10-27T02:16:15+00:00"
45
+ # id: eac74d62-fc5b-102c-a92d-40402145ee8b
46
+ # createdDatetime: "2009-09-27T02:16:15+00:00"
47
+ # type: subscription
48
+
49
+ def customer_attributes_for_api(customer)
50
+ {
51
+ :firstName => customer.first_name,
52
+ :lastName => customer.last_name,
53
+ :email => customer.email,
54
+ :code => customer.code,
55
+ :subscription => {
56
+ :planCode => customer.subscription.plan_code,
57
+ :ccFirstName => customer.subscription.billing_first_name,
58
+ :ccLastName => customer.subscription.billing_last_name,
59
+ :ccNumber => customer.subscription.credit_card_number,
60
+ :ccExpMonth => customer.subscription.credit_card_expiration_month,
61
+ :ccExpYear => customer.subscription.credit_card_expiration_year,
62
+ :ccZip => customer.subscription.billing_zip_code,
63
+ }
64
+ }
65
+ end
66
+
67
+ describe "when having multiple subscriptions" do
68
+ it "returns the latest one" do
69
+ Mousetrap::Customer.new_from_api(FULL_CUSTOMER).subscription.should_not be_nil
70
+ end
71
+ end
72
+
73
+ describe '.all' do
74
+ before do
75
+ Mousetrap::Customer.stub :build_resources_from
76
+ end
77
+
78
+ it "gets all customers" do
79
+ Mousetrap::Customer.should_receive(:get_resources).with('customers').and_return('some hash')
80
+ Mousetrap::Customer.all
81
+ end
82
+
83
+ it "handles kludgy 'no customers found' response" do
84
+ Mousetrap::Customer.stub :get_resources => {
85
+ 'error' => 'Resource not found: No customers found.'
86
+ }
87
+ Mousetrap::Customer.all.should == []
88
+ end
89
+
90
+ it "raises error if response has one" do
91
+ expect do
92
+ Mousetrap::Customer.stub :get_resources => { 'error' => "some other error" }
93
+ Mousetrap::Customer.all
94
+ end.to raise_error(RuntimeError, "some other error")
95
+ end
96
+
97
+ it "builds resources from the response" do
98
+ Mousetrap::Customer.stub :get_resources => 'some hash'
99
+ Mousetrap::Customer.should_receive(:build_resources_from).with('some hash')
100
+ Mousetrap::Customer.all
101
+ end
102
+ end
103
+
104
+ describe '.create' do
105
+ before do
106
+ @customer_hash = Factory.attributes_for :new_customer
107
+ @customer = Mousetrap::Customer.new @customer_hash
108
+ @customer.stub(:save)
109
+ Mousetrap::Customer.stub(:new => @customer)
110
+ Mousetrap::Customer.stub(:build_resource_from => stub(:id => 0))
111
+ end
112
+
113
+ it 'instantiates a customer with a hash of attributes' do
114
+ Mousetrap::Customer.should_receive(:new).with(@customer_hash).and_return(@customer)
115
+ Mousetrap::Customer.create(@customer_hash)
116
+ end
117
+
118
+ it 'saves the new customer instance' do
119
+ @customer.should_receive(:save)
120
+ Mousetrap::Customer.create(@customer_hash)
121
+ end
122
+
123
+ it 'sets the id of the newly created customer' do
124
+ Mousetrap::Customer.stub(:build_resource_from => stub(:id => 1))
125
+ @customer.should_receive(:id=).with(1)
126
+ Mousetrap::Customer.create(@customer_hash)
127
+ end
128
+
129
+ it 'returns an instance of Mousetrap::Customer' do
130
+ Mousetrap::Customer.create(@customer_hash).should be_instance_of(Mousetrap::Customer)
131
+ end
132
+ end
133
+
134
+ describe ".new" do
135
+ subject do
136
+ Mousetrap::Customer.new \
137
+ :first_name => 'Jon',
138
+ :last_name => 'Larkowski',
139
+ :email => 'lark@example.com',
140
+ :code => 'asfkhw0'
141
+ end
142
+
143
+ it { should be_instance_of(Mousetrap::Customer) }
144
+ it { should be_new_record }
145
+
146
+ describe "sets" do
147
+ it 'first_name' do
148
+ subject.first_name.should == 'Jon'
149
+ end
150
+
151
+ it 'last_name' do
152
+ subject.last_name.should == 'Larkowski'
153
+ end
154
+
155
+ it 'email' do
156
+ subject.email.should == 'lark@example.com'
157
+ end
158
+
159
+ it 'code' do
160
+ subject.code.should == 'asfkhw0'
161
+ end
162
+ end
163
+ end
164
+
165
+ describe '#cancel' do
166
+ context "for existing records" do
167
+ it 'cancels' do
168
+ customer = Factory :existing_customer
169
+ customer.should_receive(:member_action).with('cancel')
170
+ customer.cancel
171
+ end
172
+ end
173
+
174
+ context "for new records" do
175
+ it "does nothing" do
176
+ customer = Factory.build :new_customer
177
+ customer.should_not_receive(:member_action).with('cancel')
178
+ customer.cancel
179
+ end
180
+ end
181
+ end
182
+
183
+ describe '#save' do
184
+ context "for existing records" do
185
+ before do
186
+ @customer = Factory :existing_customer
187
+ end
188
+
189
+ it 'posts to edit-customer action' do
190
+ attributes_for_api = customer_attributes_for_api(@customer)
191
+
192
+ # We don't send code for existing API resources.
193
+ attributes_for_api.delete(:code)
194
+
195
+ @customer.class.should_receive(:put_resource).with(
196
+ 'customers', 'edit-customer', @customer.code, attributes_for_api)
197
+ @customer.save
198
+ end
199
+ end
200
+
201
+ context "for new records" do
202
+ it 'calls create' do
203
+ customer = Factory :new_customer
204
+ customer.should_receive(:create)
205
+ customer.save
206
+ end
207
+ end
208
+ end
209
+ end