ryanwood-mousetrap 0.5.6

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,7 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
6
+ spec/integration/settings.yml
7
+ script/cheddar_getter.yml
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Jon Larkowski
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.textile ADDED
@@ -0,0 +1,25 @@
1
+ h1. Mousetrap
2
+
3
+ _CheddarGetter API Client in Ruby_
4
+
5
+ Docs coming Real Soon Now (TM).
6
+
7
+
8
+ h2. Tests
9
+
10
+ h3. Unit Tests
11
+
12
+ Run RSpec tests with the @rake spec@ task.
13
+
14
+ h3. Integration Smoke Test
15
+
16
+ This test runs against the actual CheddarGetter service, using a test Product
17
+ that you create.
18
+
19
+ # Set up a CheddarGetter account.
20
+ # Add a Product with a code named: @MOUSETRAP_TEST@
21
+ # Add a Plan with a code named: @TEST@
22
+ # Put your credentials into a _spec/integration/settings.yml_ file.
23
+ # Run the tests with: @spec -c -fn spec/integration/smoke_test.rb@
24
+
25
+ Copyright (c) 2009 Hashrocket. See MIT-LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,51 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "ryanwood-mousetrap"
8
+ gem.summary = %Q{CheddarGetter API Client in Ruby}
9
+ gem.description = %Q{CheddarGetter API Client in Ruby}
10
+ gem.email = "ryan.wood@gmail.com"
11
+ gem.homepage = "http://github.com/ryanwood/mousetrap"
12
+ gem.authors = ["Jon Larkowski", "Sandro Turriate", "Wolfram Arnold", "Corey Grusden", "Cameron Cox", "Ryan Wood"]
13
+ gem.add_dependency 'httparty', '>= 0.4.2'
14
+ gem.add_development_dependency "activesupport", '>= 2.3.3'
15
+ gem.add_development_dependency "rspec", '>= 1.2.9'
16
+ gem.add_development_dependency 'factory_girl', '>= 1.2.3'
17
+ end
18
+ rescue LoadError
19
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
20
+ end
21
+
22
+ require 'spec/rake/spectask'
23
+ Spec::Rake::SpecTask.new(:spec) do |spec|
24
+ spec.spec_opts = ['--options', 'spec/spec.opts']
25
+ spec.libs << 'lib' << 'spec'
26
+ spec.spec_files = FileList['spec/**/*_spec.rb']
27
+ end
28
+
29
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
30
+ spec.libs << 'lib' << 'spec'
31
+ spec.pattern = 'spec/**/*_spec.rb'
32
+ spec.rcov = true
33
+ end
34
+
35
+ task :spec => :check_dependencies
36
+
37
+ task :default => :spec
38
+
39
+ require 'rake/rdoctask'
40
+ Rake::RDocTask.new do |rdoc|
41
+ if File.exist?('VERSION')
42
+ version = File.read('VERSION')
43
+ else
44
+ version = ""
45
+ end
46
+
47
+ rdoc.rdoc_dir = 'rdoc'
48
+ rdoc.title = "mousetrap #{version}"
49
+ rdoc.rdoc_files.include('README*')
50
+ rdoc.rdoc_files.include('lib/**/*.rb')
51
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.5.6
@@ -0,0 +1,190 @@
1
+ module Mousetrap
2
+ class Customer < Resource
3
+ attr_accessor \
4
+ :id,
5
+ :code,
6
+ :email,
7
+ :first_name,
8
+ :last_name,
9
+ :company,
10
+ :subscription
11
+
12
+ def update_tracked_item_quantity(item_code, quantity = 1)
13
+ tracked_item_resource = if quantity == quantity.abs
14
+ 'add-item-quantity'
15
+ else
16
+ 'remove-item-quantity'
17
+ end
18
+
19
+ attributes = {
20
+ :itemCode => item_code,
21
+ :quantity => quantity.abs
22
+ }
23
+
24
+ response = self.class.put_resource 'customers', tracked_item_resource, code, attributes
25
+ raise response['error'] if response['error']
26
+ response
27
+ end
28
+
29
+ def add_item_quantity(item_code, quantity = 1)
30
+ update_tracked_item_quantity(item_code, quantity)
31
+ end
32
+
33
+ def remove_item_quantity(item, quantity = 1)
34
+ update_tracked_item_quantity(item_code, -quantity)
35
+ end
36
+
37
+ def add_custom_charge(item_code, amount = 1.0, quantity = 1, description = nil)
38
+ attributes = {
39
+ :chargeCode => item_code,
40
+ :eachAmount => amount,
41
+ :quantity => quantity,
42
+ :description => description
43
+ }
44
+
45
+ response = self.class.put_resource 'customers', 'add-charge', code, attributes
46
+ raise response['error'] if response['error']
47
+ response
48
+ end
49
+
50
+ def subscription_attributes=(attributes)
51
+ self.subscription = Subscription.new attributes
52
+ end
53
+
54
+ def attributes
55
+ {
56
+ :id => id,
57
+ :code => code,
58
+ :email => email,
59
+ :first_name => first_name,
60
+ :last_name => last_name,
61
+ :company => company
62
+ }
63
+ end
64
+
65
+ def attributes_for_api
66
+ # TODO: superclass?
67
+ self.class.attributes_for_api(attributes, new_record?)
68
+ end
69
+
70
+ def attributes_for_api_with_subscription
71
+ raise "Must have subscription" unless subscription
72
+ a = attributes_for_api
73
+ a[:subscription] = subscription.attributes_for_api
74
+ a
75
+ end
76
+
77
+ def cancel
78
+ member_action 'cancel' unless new_record?
79
+ end
80
+
81
+ def new?
82
+ if api_customer = self.class[code]
83
+ self.id = api_customer.id
84
+
85
+ return false
86
+ else
87
+ return true
88
+ end
89
+ end
90
+
91
+ def save
92
+ new? ? create : update
93
+ end
94
+
95
+ def switch_to_plan(plan_code)
96
+ raise "Can only call this on an existing CheddarGetter customer." unless exists?
97
+
98
+ attributes = { :planCode => plan_code }
99
+ self.class.put_resource('customers', 'edit-subscription', code, attributes)
100
+
101
+ # TODO: Refresh self with reload here?
102
+ end
103
+
104
+ def self.all
105
+ response = get_resources 'customers'
106
+
107
+ if response['error']
108
+ if response['error'] =~ /No customers found/
109
+ return []
110
+ else
111
+ raise response['error']
112
+ end
113
+ end
114
+
115
+ build_resources_from response
116
+ end
117
+
118
+ def self.create(attributes)
119
+ object = new(attributes)
120
+ object.send(:create)
121
+ object
122
+ end
123
+
124
+ def self.new_from_api(attributes)
125
+ customer = new(attributes_from_api(attributes))
126
+ subscription_attrs = attributes['subscriptions']['subscription']
127
+ customer.subscription = Subscription.new_from_api(subscription_attrs.kind_of?(Array) ? subscription_attrs.first : subscription_attrs)
128
+ customer
129
+ end
130
+
131
+ def self.update(customer_code, attributes)
132
+ customer = new(attributes)
133
+ customer.code = customer_code
134
+ customer.send :update
135
+ end
136
+
137
+
138
+ protected
139
+
140
+ def self.plural_resource_name
141
+ 'customers'
142
+ end
143
+
144
+ def self.singular_resource_name
145
+ 'customer'
146
+ end
147
+
148
+ def self.attributes_for_api(attributes, new_record = true)
149
+ mutated_hash = {
150
+ :email => attributes[:email],
151
+ :firstName => attributes[:first_name],
152
+ :lastName => attributes[:last_name],
153
+ :company => attributes[:company]
154
+ }
155
+ mutated_hash.merge!(:code => attributes[:code]) if new_record
156
+ mutated_hash
157
+ end
158
+
159
+ def self.attributes_from_api(attributes)
160
+ {
161
+ :id => attributes['id'],
162
+ :code => attributes['code'],
163
+ :first_name => attributes['firstName'],
164
+ :last_name => attributes['lastName'],
165
+ :company => attributes['company'],
166
+ :email => attributes['email']
167
+ }
168
+ end
169
+
170
+ def create
171
+ response = self.class.post_resource 'customers', 'new', attributes_for_api_with_subscription
172
+
173
+ raise response['error'] if response['error']
174
+
175
+ returned_customer = self.class.build_resource_from response
176
+ self.id = returned_customer.id
177
+ response
178
+ end
179
+
180
+ def update
181
+ if subscription
182
+ response = self.class.put_resource 'customers', 'edit', code, attributes_for_api_with_subscription
183
+ else
184
+ response = self.class.put_resource 'customers', 'edit-customer', code, attributes_for_api
185
+ end
186
+
187
+ raise response['error'] if response['error']
188
+ end
189
+ end
190
+ end
@@ -0,0 +1,41 @@
1
+ module Mousetrap
2
+ class Invoice < Resource
3
+ attr_accessor \
4
+ :id,
5
+ :number,
6
+ :billing_date,
7
+ :created_at
8
+
9
+ def initialize(hash = {})
10
+ super(self.class.attributes_from_api(hash))
11
+ end
12
+
13
+ protected
14
+
15
+ def self.plural_resource_name
16
+ 'invoices'
17
+ end
18
+
19
+ def self.singular_resource_name
20
+ 'invoice'
21
+ end
22
+
23
+ def attributes
24
+ {
25
+ :id => id,
26
+ :number => number,
27
+ :billing_date => billing_date,
28
+ :created_at => created_at
29
+ }
30
+ end
31
+
32
+ def self.attributes_from_api(attributes)
33
+ {
34
+ :id => attributes['id'],
35
+ :number => attributes['number'],
36
+ :billing_date => attributes['billingDatetime'],
37
+ :created_at => attributes['createdDatetime']
38
+ }
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,34 @@
1
+ module Mousetrap
2
+ class Plan < Resource
3
+ attr_accessor \
4
+ :code,
5
+ :name,
6
+ :items,
7
+ :recurring_charge_amount
8
+
9
+ def self.all
10
+ response = get_resources plural_resource_name
11
+ return [] unless response['plans']
12
+ build_resources_from response
13
+ end
14
+
15
+ protected
16
+
17
+ def self.plural_resource_name
18
+ 'plans'
19
+ end
20
+
21
+ def self.singular_resource_name
22
+ 'plan'
23
+ end
24
+
25
+ def self.attributes_from_api(attributes)
26
+ {
27
+ :code => attributes['code'],
28
+ :name => attributes['name'],
29
+ :items => attributes['items'],
30
+ :recurring_charge_amount => attributes['recurringChargeAmount']
31
+ }
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,147 @@
1
+ module Mousetrap
2
+ class Resource
3
+ include HTTParty
4
+ headers 'User-Agent' => 'Mousetrap Ruby Client'
5
+ base_uri 'https://cheddargetter.com'
6
+
7
+ def initialize(hash={})
8
+ hash.each do |key, value|
9
+ self.send("#{key}=", value)
10
+ end
11
+ end
12
+
13
+ def self.[](code)
14
+ # Example error message:
15
+ #
16
+ # { "error" => "Resource not found: Customer not found for
17
+ # code=cantfindme within productCode=MOUSETRAP_TEST"}
18
+
19
+ response = get_resource plural_resource_name, code
20
+
21
+ if response['error']
22
+ if response['error'] =~ /not found/
23
+ return nil
24
+ else
25
+ raise response['error']
26
+ end
27
+ end
28
+
29
+ build_resource_from response
30
+ end
31
+
32
+ def self.destroy_all
33
+ all.each { |object| object.destroy }
34
+ end
35
+
36
+ def self.exists?(code)
37
+ !self[code].nil?
38
+ end
39
+
40
+ def self.new_from_api(attributes)
41
+ new(attributes_from_api(attributes))
42
+ end
43
+
44
+ def destroy
45
+ member_action 'delete' unless new_record?
46
+ end
47
+
48
+ def exists?
49
+ self.class.exists?(code)
50
+ end
51
+
52
+ def new?
53
+ id.nil?
54
+ end
55
+
56
+ alias new_record? new?
57
+
58
+
59
+ protected
60
+
61
+ def self.build_resource_from(response)
62
+ resource_attributes = extract_resources(response)
63
+ new_from_api(resource_attributes)
64
+ end
65
+
66
+ def self.build_resources_from(response)
67
+ resources = []
68
+
69
+ response_resources = extract_resources(response)
70
+
71
+ if response_resources.is_a?(Array)
72
+ extract_resources(response).each do |resource_attributes|
73
+ resources << new_from_api(resource_attributes)
74
+ end
75
+ else
76
+ resources << new_from_api(response_resources)
77
+ end
78
+
79
+ resources
80
+ end
81
+
82
+ def self.delete_resource(resource, code)
83
+ member_action(resource, 'delete', code)
84
+ end
85
+
86
+ def self.extract_resources(response)
87
+ response[plural_resource_name][singular_resource_name]
88
+ end
89
+
90
+ def self.get_resource(resource, code)
91
+ get resource_path(resource, 'get', code)
92
+ rescue Errno::ECONNREFUSED
93
+ raise_api_is_down
94
+ end
95
+
96
+ def self.get_resources(resource)
97
+ get resource_path(resource, 'get')
98
+ rescue Errno::ECONNREFUSED
99
+ raise_api_is_down
100
+ end
101
+
102
+ def self.member_action(resource, action, code, attributes = nil)
103
+ path = resource_path(resource, action, code)
104
+
105
+ if attributes
106
+ post path, :body => attributes
107
+ else
108
+ post path
109
+ end
110
+ rescue Errno::ECONNREFUSED
111
+ raise_api_is_down
112
+ end
113
+
114
+ def self.post_resource(resource, action, attributes)
115
+ path = resource_path(resource, action)
116
+ post path, :body => attributes
117
+ rescue Errno::ECONNREFUSED
118
+ raise_api_is_down
119
+ end
120
+
121
+ def self.put_resource(resource, action, code, attributes)
122
+ member_action(resource, action, code, attributes)
123
+ end
124
+
125
+ def self.raise_api_unsupported_error
126
+ raise NotImplementedError, API_UNSUPPORTED
127
+ end
128
+
129
+ def self.raise_api_is_down
130
+ raise ApiDown, "CheddarGetter is currently down"
131
+ end
132
+
133
+ def self.resource_path(resource, action, code = nil)
134
+ path = "/xml/#{resource}/#{action}/productCode/#{uri_encode(Mousetrap.product_code)}"
135
+ path += "/code/#{uri_encode(code)}" if code
136
+ path
137
+ end
138
+
139
+ def self.uri_encode(value)
140
+ URI.encode(value.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
141
+ end
142
+
143
+ def member_action(action)
144
+ self.class.member_action(self.class.plural_resource_name, action, code)
145
+ end
146
+ end
147
+ end
@@ -0,0 +1,160 @@
1
+ module Mousetrap
2
+ class Subscription < Resource
3
+ # Attributes we send _to_ the API.
4
+ attr_accessor \
5
+ :plan_code,
6
+ :billing_first_name,
7
+ :billing_last_name,
8
+ :credit_card_number,
9
+ :credit_card_expiration_month,
10
+ :credit_card_expiration_year,
11
+ :credit_card_code,
12
+ :billing_country,
13
+ :billing_address,
14
+ :billing_city,
15
+ :billing_state,
16
+ :billing_zip_code,
17
+ :plan,
18
+
19
+ :customer_code # belongs to customer
20
+
21
+ # Attributes that come _from_ the API.
22
+ attr_reader \
23
+ :id,
24
+ :canceled_at,
25
+ :created_at,
26
+ :credit_card_expiration_date,
27
+ :credit_card_last_four_digits,
28
+ :credit_card_type,
29
+ :invoices,
30
+ :items
31
+
32
+ def self.[](code)
33
+ raise_api_unsupported_error
34
+ end
35
+
36
+ def self.all
37
+ raise_api_unsupported_error
38
+ end
39
+
40
+ def self.destroy_all
41
+ raise_api_unsupported_error
42
+ end
43
+
44
+ def self.exists?(code)
45
+ raise_api_unsupported_error
46
+ end
47
+
48
+ def current_invoice
49
+ invoice_attributes = if invoices['invoice'].kind_of?(Array)
50
+ invoices['invoice'][0]
51
+ else
52
+ invoices['invoice']
53
+ end
54
+
55
+ Invoice.new(invoice_attributes)
56
+ end
57
+
58
+ def attributes
59
+ {
60
+ :id => id,
61
+ :plan_code => plan_code,
62
+ :billing_first_name => billing_first_name,
63
+ :billing_last_name => billing_last_name,
64
+ :credit_card_number => credit_card_number,
65
+ :credit_card_expiration_month => credit_card_expiration_month,
66
+ :credit_card_expiration_year => credit_card_expiration_year,
67
+ :credit_card_code => credit_card_code,
68
+ :billing_country => billing_country,
69
+ :billing_address => billing_address,
70
+ :billing_city => billing_city,
71
+ :billing_state => billing_state,
72
+ :billing_zip_code => billing_zip_code,
73
+ }
74
+ end
75
+
76
+ def attributes_for_api
77
+ self.class.attributes_for_api(attributes)
78
+ end
79
+
80
+ def destroy
81
+ self.class.raise_api_unsupported_error
82
+ end
83
+
84
+ def exists?
85
+ self.class.raise_api_unsupported_error
86
+ end
87
+
88
+ def self.new_from_api(attributes)
89
+ subscription = new(attributes_from_api(attributes))
90
+ subscription.plan = Plan.new_from_api(attributes['plans']['plan'])
91
+ subscription
92
+ end
93
+
94
+ def self.update(customer_code, attributes)
95
+ mutated_attributes = attributes_for_api(attributes)
96
+
97
+ mutated_attributes.delete_if { |k, v| v.blank? }
98
+
99
+ response = put_resource(
100
+ 'customers',
101
+ 'edit-subscription',
102
+ customer_code,
103
+ mutated_attributes
104
+ )
105
+
106
+ raise response['error'] if response['error']
107
+ end
108
+
109
+
110
+ protected
111
+
112
+ attr_writer \
113
+ :id,
114
+ :canceled_at,
115
+ :created_at,
116
+ :credit_card_expiration_date,
117
+ :credit_card_last_four_digits,
118
+ :credit_card_type,
119
+ :items,
120
+ :invoices
121
+
122
+ def self.plural_resource_name
123
+ 'subscriptions'
124
+ end
125
+
126
+ def self.singular_resource_name
127
+ 'subscription'
128
+ end
129
+
130
+ def self.attributes_for_api(attributes)
131
+ {
132
+ :planCode => attributes[:plan_code],
133
+ :ccFirstName => attributes[:billing_first_name],
134
+ :ccLastName => attributes[:billing_last_name],
135
+ :ccNumber => attributes[:credit_card_number],
136
+ :ccExpMonth => ("%02d" % attributes[:credit_card_expiration_month] if attributes[:credit_card_expiration_month]),
137
+ :ccExpYear => attributes[:credit_card_expiration_year],
138
+ :ccCardCode => attributes[:credit_card_code],
139
+ :ccCountry => attributes[:billing_country],
140
+ :ccAddress => attributes[:billing_address],
141
+ :ccCity => attributes[:billing_city],
142
+ :ccState => attributes[:billing_state],
143
+ :ccZip => attributes[:billing_zip_code]
144
+ }
145
+ end
146
+
147
+ def self.attributes_from_api(attributes)
148
+ {
149
+ :id => attributes['id'],
150
+ :canceled_at => attributes['canceledDatetime'],
151
+ :created_at => attributes['createdDatetime'],
152
+ :credit_card_expiration_date => attributes['ccExpirationDate'],
153
+ :credit_card_last_four_digits => attributes['ccLastFour'],
154
+ :credit_card_type => attributes['ccType'],
155
+ :invoices => attributes['invoices'],
156
+ :items => attributes['items']
157
+ }
158
+ end
159
+ end
160
+ end