hashrocket-mousetrap 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.1
1
+ 0.3.0
data/lib/mousetrap.rb CHANGED
@@ -5,6 +5,7 @@ require 'httparty'
5
5
 
6
6
  module Mousetrap
7
7
  NO_BUSINESS_NEED = "No business need at this time."
8
+ API_UNSUPPORTED = "CheddarGetter API doesn't support this."
8
9
 
9
10
  autoload :Customer, 'mousetrap/customer'
10
11
  autoload :Plan, 'mousetrap/plan'
@@ -32,14 +32,28 @@ module Mousetrap
32
32
  a
33
33
  end
34
34
 
35
- def destroy
36
- self.class.delete_resource('customers', code) unless new_record?
35
+ def cancel
36
+ member_action 'cancel' unless new_record?
37
37
  end
38
38
 
39
39
  def save
40
40
  new? ? create : update
41
41
  end
42
42
 
43
+ def self.all
44
+ response = get_resources plural_resource_name
45
+
46
+ if response['error']
47
+ if response['error'] == "Bad request: No customers found."
48
+ return []
49
+ else
50
+ raise response['error']
51
+ end
52
+ end
53
+
54
+ build_resources_from response
55
+ end
56
+
43
57
  def self.create(attributes)
44
58
  object = new(attributes)
45
59
  response = object.save
@@ -88,7 +102,13 @@ module Mousetrap
88
102
  end
89
103
 
90
104
  def create
91
- self.class.post_resource 'customers', 'new', attributes_for_api
105
+ response = self.class.post_resource 'customers', 'new', attributes_for_api
106
+
107
+ raise response['error'] if response['error']
108
+
109
+ returned_customer = self.class.build_resource_from response
110
+ self.id = returned_customer.id
111
+ response
92
112
  end
93
113
 
94
114
  def update
@@ -5,6 +5,11 @@ module Mousetrap
5
5
  :code,
6
6
  :name
7
7
 
8
+ def self.all
9
+ response = get_resources plural_resource_name
10
+ return [] unless response['plans']
11
+ build_resources_from response
12
+ end
8
13
 
9
14
  protected
10
15
 
@@ -15,11 +15,6 @@ module Mousetrap
15
15
  build_resource_from response
16
16
  end
17
17
 
18
- def self.all
19
- response = get_resources plural_resource_name
20
- build_resources_from response
21
- end
22
-
23
18
  def self.create(attributes = {})
24
19
  raise NotImplementedError, NO_BUSINESS_NEED
25
20
  end
@@ -28,12 +23,16 @@ module Mousetrap
28
23
  raise NotImplementedError, NO_BUSINESS_NEED
29
24
  end
30
25
 
26
+ def self.destroy_all
27
+ all.each { |object| object.destroy }
28
+ end
29
+
31
30
  def self.exists?(code)
32
31
  raise NotImplementedError, NO_BUSINESS_NEED
33
32
  end
34
33
 
35
34
  def destroy
36
- raise NotImplementedError, NO_BUSINESS_NEED
35
+ member_action 'delete' unless new_record?
37
36
  end
38
37
 
39
38
  def exists?(code)
@@ -57,28 +56,44 @@ module Mousetrap
57
56
 
58
57
  protected
59
58
 
59
+ def member_action(action)
60
+ self.class.member_action(self.class.plural_resource_name, action, code)
61
+ end
62
+
63
+ def self.resource_path(resource, action, code = nil)
64
+ path = "/xml/#{resource}/#{action}/productCode/#{Mousetrap.product_code}"
65
+ path += "/code/#{code}" if code
66
+ path
67
+ end
68
+
69
+ def self.member_action(resource, action, code, attributes = nil)
70
+ path = resource_path(resource, action, code)
71
+
72
+ if attributes
73
+ post path, :body => attributes
74
+ else
75
+ post path
76
+ end
77
+ end
78
+
60
79
  def self.delete_resource(resource, code)
61
- path = "/xml/#{resource}/delete/productCode/#{Mousetrap.product_code}/code/#{code}"
62
- post path
80
+ member_action(resource, 'delete', code)
81
+ end
82
+
83
+ def self.put_resource(resource, action, code, attributes)
84
+ member_action(resource, action, code, attributes)
63
85
  end
64
86
 
65
87
  def self.get_resource(resource, code)
66
- path = "/xml/#{resource}/get/productCode/#{Mousetrap.product_code}/code/#{code}"
67
- get path
88
+ get resource_path(resource, 'get', code)
68
89
  end
69
90
 
70
91
  def self.get_resources(resource)
71
- path = "/xml/#{resource}/get/productCode/#{Mousetrap.product_code}"
72
- get path
92
+ get resource_path(resource, 'get')
73
93
  end
74
94
 
75
95
  def self.post_resource(resource, action, attributes)
76
- path = "/xml/#{resource}/#{action}/productCode/#{Mousetrap.product_code}"
77
- post path, :body => attributes
78
- end
79
-
80
- def self.put_resource(resource, action, resource_code, attributes)
81
- path = "/xml/#{resource}/#{action}/productCode/#{Mousetrap.product_code}/code/#{resource_code}"
96
+ path = resource_path(resource, action)
82
97
  post path, :body => attributes
83
98
  end
84
99
 
@@ -10,9 +10,10 @@ module Mousetrap
10
10
  :credit_card_expiration_year,
11
11
  :billing_zip_code,
12
12
 
13
- :customer_code, # belongs to customer
13
+ :customer_code # belongs to customer
14
14
 
15
- # Attributes that come _from_ the API.
15
+ # Attributes that come _from_ the API.
16
+ attr_reader \
16
17
  :id,
17
18
  :canceled_at,
18
19
  :created_at,
@@ -39,6 +40,10 @@ module Mousetrap
39
40
  self.class.attributes_for_api(attributes)
40
41
  end
41
42
 
43
+ def destroy
44
+ raise NotImplementedError, API_UNSUPPORTED
45
+ end
46
+
42
47
  def save
43
48
  mutated_attributes = attributes_for_api(attributes)
44
49
  self.class.put_resource('customers', 'edit-subscription', customer_code, mutated_attributes)
@@ -47,6 +52,14 @@ module Mousetrap
47
52
 
48
53
  protected
49
54
 
55
+ attr_writer \
56
+ :id,
57
+ :canceled_at,
58
+ :created_at,
59
+ :credit_card_expiration_date,
60
+ :credit_card_last_four_digits,
61
+ :credit_card_type
62
+
50
63
  def self.plural_resource_name
51
64
  'subscriptions'
52
65
  end
data/mousetrap.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{mousetrap}
8
- s.version = "0.2.1"
8
+ s.version = "0.3.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Jon Larkowski", "Sandro Turriate", "Wolfram Arnold", "Corey Grusden"]
12
- s.date = %q{2009-09-01}
12
+ s.date = %q{2009-09-27}
13
13
  s.description = %q{CheddarGetter API Client in Ruby}
14
14
  s.email = %q{jonlarkowski@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -1,7 +1,10 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
- require File.dirname(__FILE__) + '/../../lib/mousetrap'
1
+ require File.expand_path('../../../lib/mousetrap', __FILE__)
2
+
3
+ require 'spec'
4
+ require 'spec/autorun'
5
+ require 'factory_girl'
6
+ require 'active_support'
3
7
  require 'yaml'
4
- require 'activesupport'
5
8
 
6
9
  Dir["#{File.dirname(__FILE__)}/../support/**/*.rb"].each {|f| require f}
7
10
 
@@ -9,8 +12,138 @@ settings = YAML.load_file(File.dirname(__FILE__) + '/settings.yml')
9
12
  Mousetrap.authenticate(settings['user'], settings['password'])
10
13
  Mousetrap.product_code = settings['product_code']
11
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
+
12
48
  describe "The Wrapper Gem" do
13
- it "works" do
14
- true.should be_true
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
15
148
  end
16
149
  end
@@ -21,21 +21,33 @@ def plans
21
21
  puts test_plan.to_yaml
22
22
  end
23
23
 
24
- def customers
24
+ def all_customers
25
+ all_customers = Mousetrap::Customer.all
26
+ puts all_customers.to_yaml
27
+ end
28
+
29
+ def create_customer
25
30
  customer = Factory :new_customer
26
31
  customer.save
27
32
 
28
33
  api_customer = Mousetrap::Customer[customer.code]
29
34
  puts api_customer.to_yaml
30
-
31
- #all_customers = Mousetrap::Customer.all
32
- #puts all_customers.to_yaml
33
35
  end
34
36
 
35
37
  def destroy_all_customers
36
- all_customers = Mousetrap::Customer.all
37
- all_customers.each { |c| c.destroy }
38
+ Mousetrap::Customer.destroy_all
38
39
  end
39
40
 
40
- #destroy_all_customers
41
- #customers
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
+
@@ -1,6 +1,51 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
2
 
3
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
+
4
49
  def customer_attributes_for_api(customer)
5
50
  {
6
51
  :firstName => customer.first_name,
@@ -19,6 +64,35 @@ describe Mousetrap::Customer do
19
64
  }
20
65
  end
21
66
 
67
+ describe '.all' do
68
+ before do
69
+ Mousetrap::Customer.stub :build_resources_from
70
+ end
71
+
72
+ it "gets all customers" do
73
+ Mousetrap::Customer.should_receive(:get_resources).with('customers').and_return('some hash')
74
+ Mousetrap::Customer.all
75
+ end
76
+
77
+ it "handles kludgy 'no customers found' response" do
78
+ Mousetrap::Customer.stub :get_resources => { 'error' => "Bad request: No customers found." }
79
+ Mousetrap::Customer.all.should == []
80
+ end
81
+
82
+ it "raises error if response has one" do
83
+ expect do
84
+ Mousetrap::Customer.stub :get_resources => { 'error' => "some other error" }
85
+ Mousetrap::Customer.all
86
+ end.to raise_error(RuntimeError, "some other error")
87
+ end
88
+
89
+ it "builds resources from the response" do
90
+ Mousetrap::Customer.stub :get_resources => 'some hash'
91
+ Mousetrap::Customer.should_receive(:build_resources_from).with('some hash')
92
+ Mousetrap::Customer.all
93
+ end
94
+ end
95
+
22
96
  describe '.create' do
23
97
  before do
24
98
  @customer_hash = Factory.attributes_for :new_customer
@@ -80,20 +154,20 @@ describe Mousetrap::Customer do
80
154
  end
81
155
  end
82
156
 
83
- describe '#destroy' do
157
+ describe '#cancel' do
84
158
  context "for existing records" do
85
- it 'destroys' do
159
+ it 'cancels' do
86
160
  customer = Factory :existing_customer
87
- Mousetrap::Customer.should_receive(:delete_resource).with('customers', customer.code)
88
- customer.destroy
161
+ customer.should_receive(:member_action).with('cancel')
162
+ customer.cancel
89
163
  end
90
164
  end
91
165
 
92
166
  context "for new records" do
93
167
  it "does nothing" do
94
- customer = Factory :new_customer
95
- Mousetrap::Customer.should_not_receive(:delete_resource)
96
- customer.destroy
168
+ customer = Factory.build :new_customer
169
+ customer.should_not_receive(:member_action).with('cancel')
170
+ customer.cancel
97
171
  end
98
172
  end
99
173
  end
@@ -16,4 +16,26 @@ describe Mousetrap::Plan do
16
16
  # billingFrequencyUnit: months
17
17
  # setupChargeAmount: "0.00"
18
18
  # billingFrequencyPer: month
19
+
20
+ describe ".all" do
21
+ before do
22
+ Mousetrap::Plan.stub :build_resources_from
23
+ end
24
+
25
+ it "gets all plans" do
26
+ Mousetrap::Plan.should_receive(:get_resources).with('plans').and_return('some hash')
27
+ Mousetrap::Plan.all
28
+ end
29
+
30
+ it "handles no-plans case" do
31
+ Mousetrap::Plan.stub :get_resources => { 'plans' => nil }
32
+ Mousetrap::Plan.all.should == []
33
+ end
34
+
35
+ it "builds resources from the response" do
36
+ Mousetrap::Plan.stub :get_resources => { 'plans' => 'some hash' }
37
+ Mousetrap::Plan.should_receive(:build_resources_from).with({ 'plans' => 'some hash' })
38
+ Mousetrap::Plan.all
39
+ end
40
+ end
19
41
  end
@@ -64,18 +64,32 @@ describe Mousetrap::Resource do
64
64
  end
65
65
  end
66
66
 
67
- describe '.all' do
68
- it 'gets widgets resources' do
69
- Mousetrap::Widget.stub(:build_resources_from)
70
- Mousetrap::Widget.should_receive(:get_resources).with('widgets')
71
- Mousetrap::Widget.all
67
+ describe ".destroy_all" do
68
+ it "destroys each resource" do
69
+ all_widgets = [stub, stub, stub]
70
+ Mousetrap::Widget.stub :all => all_widgets
71
+ all_widgets.each { |w| w.should_receive :destroy }
72
+ Mousetrap::Widget.destroy_all
73
+ end
74
+ end
75
+
76
+ describe '#destroy' do
77
+ context "for existing records" do
78
+ it 'destroys' do
79
+ widget = Mousetrap::Widget.new
80
+ widget.stub :new_record? => false
81
+ widget.should_receive(:member_action).with('delete')
82
+ widget.destroy
83
+ end
72
84
  end
73
85
 
74
- it 'builds widget resources' do
75
- response = stub
76
- Mousetrap::Widget.stub(:get_resources => response)
77
- Mousetrap::Widget.should_receive(:build_resources_from).with(response)
78
- Mousetrap::Widget.all
86
+ context "for new records" do
87
+ it "does nothing" do
88
+ widget = Mousetrap::Widget.new
89
+ widget.stub :new_record? => true
90
+ Mousetrap::Customer.should_not_receive(:member_action)
91
+ widget.destroy
92
+ end
79
93
  end
80
94
  end
81
95
 
@@ -108,7 +122,7 @@ describe Mousetrap::Resource do
108
122
  end
109
123
 
110
124
  describe ".put_resource" do
111
- it "put to /xml/<resource>/<action>/productCode/<product_code>/code/<resource_code>" do
125
+ it "puts to /xml/<resource>/<action>/productCode/<product_code>/code/<resource_code>" do
112
126
  subject.should_receive(:post).with(
113
127
  '/xml/widgets/some_action/productCode/my_product_code/code/some_widget_code',
114
128
  :body => 'some_hash')
@@ -9,4 +9,12 @@ describe Mousetrap::Subscription do
9
9
  # id: 46ad3f1c-e472-102c-a92d-40402145ee8b
10
10
  # ccLastFour: "1111"
11
11
  # canceledDatetime:
12
+
13
+ describe '#destroy' do
14
+ it "raises a NotImplementedError" do
15
+ expect do
16
+ Mousetrap::Subscription.new.destroy
17
+ end.to raise_error(NotImplementedError)
18
+ end
19
+ end
12
20
  end
data/spec/spec.opts CHANGED
@@ -1,4 +1,2 @@
1
1
  --colour
2
- --format specdoc
3
- --loadby mtime
4
- --reverse
2
+ --format nested
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hashrocket-mousetrap
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jon Larkowski
@@ -12,7 +12,7 @@ autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
14
 
15
- date: 2009-09-01 00:00:00 -07:00
15
+ date: 2009-09-27 00:00:00 -07:00
16
16
  default_executable:
17
17
  dependencies:
18
18
  - !ruby/object:Gem::Dependency