madebyrocket-mousetrap 0.5.3.7
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +7 -0
- data/MIT-LICENSE +20 -0
- data/README.textile +25 -0
- data/Rakefile +51 -0
- data/VERSION +1 -0
- data/lib/mousetrap/customer.rb +163 -0
- data/lib/mousetrap/invoice.rb +44 -0
- data/lib/mousetrap/plan.rb +34 -0
- data/lib/mousetrap/resource.rb +135 -0
- data/lib/mousetrap/subscription.rb +157 -0
- data/lib/mousetrap.rb +21 -0
- data/mousetrap.gemspec +90 -0
- data/script/authenticate.rb +3 -0
- data/script/cheddar_getter.example.yml +3 -0
- data/script/console +1 -0
- data/spec/factories.rb +54 -0
- data/spec/integration/settings.example.yml +6 -0
- data/spec/integration/smoke_test.rb +355 -0
- data/spec/integration/spike.rb +108 -0
- data/spec/mousetrap/customer_spec.rb +396 -0
- data/spec/mousetrap/plan_spec.rb +41 -0
- data/spec/mousetrap/resource_spec.rb +188 -0
- data/spec/mousetrap/subscription_spec.rb +152 -0
- data/spec/mousetrap_spec.rb +17 -0
- data/spec/spec.opts +2 -0
- data/spec/spec_helper.rb +23 -0
- data/spec/support/fixtures.rb +189 -0
- data/spec/support/random_data.rb +21 -0
- metadata +159 -0
data/.document
ADDED
data/.gitignore
ADDED
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 = "mousetrap"
|
8
|
+
gem.summary = %Q{CheddarGetter API Client in Ruby}
|
9
|
+
gem.description = %Q{CheddarGetter API Client in Ruby}
|
10
|
+
gem.email = "jonlarkowski@gmail.com"
|
11
|
+
gem.homepage = "http://github.com/hashrocket/mousetrap"
|
12
|
+
gem.authors = ["Jon Larkowski", "Sandro Turriate", "Wolfram Arnold", "Corey Grusden"]
|
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.3.7
|
@@ -0,0 +1,163 @@
|
|
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 subscription_attributes=(attributes)
|
13
|
+
self.subscription = Subscription.new attributes
|
14
|
+
end
|
15
|
+
|
16
|
+
def attributes
|
17
|
+
{
|
18
|
+
:id => id,
|
19
|
+
:code => code,
|
20
|
+
:email => email,
|
21
|
+
:first_name => first_name,
|
22
|
+
:last_name => last_name,
|
23
|
+
:company => company
|
24
|
+
}
|
25
|
+
end
|
26
|
+
|
27
|
+
def add_item_quantity(item_code, quantity = 1)
|
28
|
+
attrs = { :itemCode => item_code, :quantity => quantity }
|
29
|
+
self.class.put_resource 'customers', 'add-item-quantity', code, attrs
|
30
|
+
end
|
31
|
+
|
32
|
+
def remove_item_quantity(item_code, quantity = 1)
|
33
|
+
attrs = { :itemCode => item_code, :quantity => quantity }
|
34
|
+
self.class.put_resource 'customers', 'remove-item-quantity', code, attrs
|
35
|
+
end
|
36
|
+
|
37
|
+
def attributes_for_api
|
38
|
+
# TODO: superclass?
|
39
|
+
self.class.attributes_for_api(attributes, new_record?)
|
40
|
+
end
|
41
|
+
|
42
|
+
def attributes_for_api_with_subscription
|
43
|
+
raise "Must have subscription" unless subscription
|
44
|
+
a = attributes_for_api
|
45
|
+
a[:subscription] = subscription.attributes_for_api
|
46
|
+
a
|
47
|
+
end
|
48
|
+
|
49
|
+
def cancel
|
50
|
+
member_action 'cancel' unless new_record?
|
51
|
+
end
|
52
|
+
|
53
|
+
def new?
|
54
|
+
if api_customer = self.class[code]
|
55
|
+
self.id = api_customer.id
|
56
|
+
|
57
|
+
return false
|
58
|
+
else
|
59
|
+
return true
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def save
|
64
|
+
new? ? create : update
|
65
|
+
end
|
66
|
+
|
67
|
+
def switch_to_plan(plan_code)
|
68
|
+
raise "Can only call this on an existing CheddarGetter customer." unless exists?
|
69
|
+
|
70
|
+
attributes = { :planCode => plan_code }
|
71
|
+
self.class.put_resource('customers', 'edit-subscription', code, attributes)
|
72
|
+
|
73
|
+
# TODO: Refresh self with reload here?
|
74
|
+
end
|
75
|
+
|
76
|
+
def self.all
|
77
|
+
response = get_resources 'customers'
|
78
|
+
|
79
|
+
if response['error']
|
80
|
+
if response['error'] =~ /No customers found/
|
81
|
+
return []
|
82
|
+
else
|
83
|
+
raise response['error']
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
build_resources_from response
|
88
|
+
end
|
89
|
+
|
90
|
+
def self.create(attributes)
|
91
|
+
object = new(attributes)
|
92
|
+
object.send(:create)
|
93
|
+
object
|
94
|
+
end
|
95
|
+
|
96
|
+
def self.new_from_api(attributes)
|
97
|
+
customer = new(attributes_from_api(attributes))
|
98
|
+
subscription_attrs = attributes['subscriptions']['subscription']
|
99
|
+
customer.subscription = Subscription.new_from_api(subscription_attrs.kind_of?(Array) ? subscription_attrs.first : subscription_attrs)
|
100
|
+
customer
|
101
|
+
end
|
102
|
+
|
103
|
+
def self.update(customer_code, attributes)
|
104
|
+
customer = new(attributes)
|
105
|
+
customer.code = customer_code
|
106
|
+
customer.send :update
|
107
|
+
end
|
108
|
+
|
109
|
+
|
110
|
+
protected
|
111
|
+
|
112
|
+
def self.plural_resource_name
|
113
|
+
'customers'
|
114
|
+
end
|
115
|
+
|
116
|
+
def self.singular_resource_name
|
117
|
+
'customer'
|
118
|
+
end
|
119
|
+
|
120
|
+
def self.attributes_for_api(attributes, new_record = true)
|
121
|
+
mutated_hash = {}
|
122
|
+
|
123
|
+
api_accessors = { :first_name => :firstName, :last_name => :lastName, :company => :company, :email => :email, :notes => :notes }
|
124
|
+
attributes.each do |key, value|
|
125
|
+
mutated_hash[api_accessors[key]] = value
|
126
|
+
end
|
127
|
+
|
128
|
+
mutated_hash.merge!(:code => attributes[:code]) if new_record
|
129
|
+
mutated_hash
|
130
|
+
end
|
131
|
+
|
132
|
+
def self.attributes_from_api(attributes)
|
133
|
+
{
|
134
|
+
:id => attributes['id'],
|
135
|
+
:code => attributes['code'],
|
136
|
+
:first_name => attributes['firstName'],
|
137
|
+
:last_name => attributes['lastName'],
|
138
|
+
:company => attributes['company'],
|
139
|
+
:email => attributes['email']
|
140
|
+
}
|
141
|
+
end
|
142
|
+
|
143
|
+
def create
|
144
|
+
response = self.class.post_resource 'customers', 'new', attributes_for_api_with_subscription
|
145
|
+
|
146
|
+
raise response['error'] if response['error']
|
147
|
+
|
148
|
+
returned_customer = self.class.build_resource_from response
|
149
|
+
self.id = returned_customer.id
|
150
|
+
response
|
151
|
+
end
|
152
|
+
|
153
|
+
def update
|
154
|
+
if subscription
|
155
|
+
response = self.class.put_resource 'customers', 'edit', code, attributes_for_api_with_subscription
|
156
|
+
else
|
157
|
+
response = self.class.put_resource 'customers', 'edit-customer', code, attributes_for_api
|
158
|
+
end
|
159
|
+
|
160
|
+
raise response['error'] if response['error']
|
161
|
+
end
|
162
|
+
end
|
163
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module Mousetrap
|
2
|
+
class Invoice < Resource
|
3
|
+
attr_accessor \
|
4
|
+
:id,
|
5
|
+
:number,
|
6
|
+
:billing_date,
|
7
|
+
:created_at,
|
8
|
+
:amount
|
9
|
+
|
10
|
+
def initialize(hash = {})
|
11
|
+
super(self.class.attributes_from_api(hash))
|
12
|
+
end
|
13
|
+
|
14
|
+
protected
|
15
|
+
|
16
|
+
def self.plural_resource_name
|
17
|
+
'invoices'
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.singular_resource_name
|
21
|
+
'invoice'
|
22
|
+
end
|
23
|
+
|
24
|
+
def attributes
|
25
|
+
{
|
26
|
+
:id => id,
|
27
|
+
:number => number,
|
28
|
+
:billing_date => billing_date,
|
29
|
+
:created_at => created_at,
|
30
|
+
:amount => amount
|
31
|
+
}
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.attributes_from_api(attributes)
|
35
|
+
{
|
36
|
+
:id => attributes['id'],
|
37
|
+
:number => attributes['number'],
|
38
|
+
:billing_date => attributes['billingDatetime'],
|
39
|
+
:created_at => attributes['createdDatetime'],
|
40
|
+
:amount => attributes['charges']['charge'].inject(0.0) {|sum, c| sum.to_f + c['eachAmount'].to_f}
|
41
|
+
}
|
42
|
+
end
|
43
|
+
end
|
44
|
+
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,135 @@
|
|
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
|
+
end
|
93
|
+
|
94
|
+
def self.get_resources(resource)
|
95
|
+
get resource_path(resource, 'get')
|
96
|
+
end
|
97
|
+
|
98
|
+
def self.member_action(resource, action, code, attributes = nil)
|
99
|
+
path = resource_path(resource, action, code)
|
100
|
+
|
101
|
+
if attributes
|
102
|
+
post path, :body => attributes
|
103
|
+
else
|
104
|
+
post path
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
def self.post_resource(resource, action, attributes)
|
109
|
+
path = resource_path(resource, action)
|
110
|
+
post path, :body => attributes
|
111
|
+
end
|
112
|
+
|
113
|
+
def self.put_resource(resource, action, code, attributes)
|
114
|
+
member_action(resource, action, code, attributes)
|
115
|
+
end
|
116
|
+
|
117
|
+
def self.raise_api_unsupported_error
|
118
|
+
raise NotImplementedError, API_UNSUPPORTED
|
119
|
+
end
|
120
|
+
|
121
|
+
def self.resource_path(resource, action, code = nil)
|
122
|
+
path = "/xml/#{resource}/#{action}/productCode/#{uri_encode(Mousetrap.product_code)}"
|
123
|
+
path += "/code/#{uri_encode(code)}" if code
|
124
|
+
path
|
125
|
+
end
|
126
|
+
|
127
|
+
def self.uri_encode(value)
|
128
|
+
URI.encode(value.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
|
129
|
+
end
|
130
|
+
|
131
|
+
def member_action(action)
|
132
|
+
self.class.member_action(self.class.plural_resource_name, action, code)
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
@@ -0,0 +1,157 @@
|
|
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
|
+
:billing_country,
|
12
|
+
:billing_address,
|
13
|
+
:billing_city,
|
14
|
+
:billing_state,
|
15
|
+
:billing_zip_code,
|
16
|
+
:plan,
|
17
|
+
|
18
|
+
:customer_code # belongs to customer
|
19
|
+
|
20
|
+
# Attributes that come _from_ the API.
|
21
|
+
attr_reader \
|
22
|
+
:id,
|
23
|
+
:canceled_at,
|
24
|
+
:created_at,
|
25
|
+
:credit_card_expiration_date,
|
26
|
+
:credit_card_last_four_digits,
|
27
|
+
:credit_card_type,
|
28
|
+
:invoices,
|
29
|
+
:items
|
30
|
+
|
31
|
+
def self.[](code)
|
32
|
+
raise_api_unsupported_error
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.all
|
36
|
+
raise_api_unsupported_error
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.destroy_all
|
40
|
+
raise_api_unsupported_error
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.exists?(code)
|
44
|
+
raise_api_unsupported_error
|
45
|
+
end
|
46
|
+
|
47
|
+
def current_invoice
|
48
|
+
invoice_attributes = if invoices['invoice'].kind_of?(Array)
|
49
|
+
invoices['invoice'][0]
|
50
|
+
else
|
51
|
+
invoices['invoice']
|
52
|
+
end
|
53
|
+
|
54
|
+
Invoice.new(invoice_attributes)
|
55
|
+
end
|
56
|
+
|
57
|
+
def attributes
|
58
|
+
{
|
59
|
+
:id => id,
|
60
|
+
:plan_code => plan_code,
|
61
|
+
:billing_first_name => billing_first_name,
|
62
|
+
:billing_last_name => billing_last_name,
|
63
|
+
:credit_card_number => credit_card_number,
|
64
|
+
:credit_card_expiration_month => credit_card_expiration_month,
|
65
|
+
:credit_card_expiration_year => credit_card_expiration_year,
|
66
|
+
:billing_country => billing_country,
|
67
|
+
:billing_address => billing_address,
|
68
|
+
:billing_city => billing_city,
|
69
|
+
:billing_state => billing_state,
|
70
|
+
:billing_zip_code => billing_zip_code,
|
71
|
+
}
|
72
|
+
end
|
73
|
+
|
74
|
+
def attributes_for_api
|
75
|
+
self.class.attributes_for_api(attributes)
|
76
|
+
end
|
77
|
+
|
78
|
+
def destroy
|
79
|
+
self.class.raise_api_unsupported_error
|
80
|
+
end
|
81
|
+
|
82
|
+
def exists?
|
83
|
+
self.class.raise_api_unsupported_error
|
84
|
+
end
|
85
|
+
|
86
|
+
def self.new_from_api(attributes)
|
87
|
+
subscription = new(attributes_from_api(attributes))
|
88
|
+
subscription.plan = Plan.new_from_api(attributes['plans']['plan'])
|
89
|
+
subscription
|
90
|
+
end
|
91
|
+
|
92
|
+
def self.update(customer_code, attributes)
|
93
|
+
mutated_attributes = attributes_for_api(attributes)
|
94
|
+
|
95
|
+
mutated_attributes.delete_if { |k, v| v.blank? }
|
96
|
+
|
97
|
+
response = put_resource(
|
98
|
+
'customers',
|
99
|
+
'edit-subscription',
|
100
|
+
customer_code,
|
101
|
+
mutated_attributes
|
102
|
+
)
|
103
|
+
|
104
|
+
raise response['error'] if response['error']
|
105
|
+
end
|
106
|
+
|
107
|
+
|
108
|
+
protected
|
109
|
+
|
110
|
+
attr_writer \
|
111
|
+
:id,
|
112
|
+
:canceled_at,
|
113
|
+
:created_at,
|
114
|
+
:credit_card_expiration_date,
|
115
|
+
:credit_card_last_four_digits,
|
116
|
+
:credit_card_type,
|
117
|
+
:items,
|
118
|
+
:invoices
|
119
|
+
|
120
|
+
def self.plural_resource_name
|
121
|
+
'subscriptions'
|
122
|
+
end
|
123
|
+
|
124
|
+
def self.singular_resource_name
|
125
|
+
'subscription'
|
126
|
+
end
|
127
|
+
|
128
|
+
def self.attributes_for_api(attributes)
|
129
|
+
{
|
130
|
+
:planCode => attributes[:plan_code],
|
131
|
+
:ccFirstName => attributes[:billing_first_name],
|
132
|
+
:ccLastName => attributes[:billing_last_name],
|
133
|
+
:ccNumber => attributes[:credit_card_number],
|
134
|
+
:ccExpMonth => ("%02d" % attributes[:credit_card_expiration_month] if attributes[:credit_card_expiration_month]),
|
135
|
+
:ccExpYear => attributes[:credit_card_expiration_year],
|
136
|
+
:ccCountry => attributes[:billing_country],
|
137
|
+
:ccAddress => attributes[:billing_address],
|
138
|
+
:ccCity => attributes[:billing_city],
|
139
|
+
:ccState => attributes[:billing_state],
|
140
|
+
:ccZip => attributes[:billing_zip_code],
|
141
|
+
}
|
142
|
+
end
|
143
|
+
|
144
|
+
def self.attributes_from_api(attributes)
|
145
|
+
{
|
146
|
+
:id => attributes['id'],
|
147
|
+
:canceled_at => attributes['canceledDatetime'],
|
148
|
+
:created_at => attributes['createdDatetime'],
|
149
|
+
:credit_card_expiration_date => attributes['ccExpirationDate'],
|
150
|
+
:credit_card_last_four_digits => attributes['ccLastFour'],
|
151
|
+
:credit_card_type => attributes['ccType'],
|
152
|
+
:invoices => attributes['invoices'],
|
153
|
+
:items => attributes['items']
|
154
|
+
}
|
155
|
+
end
|
156
|
+
end
|
157
|
+
end
|
data/lib/mousetrap.rb
ADDED
@@ -0,0 +1,21 @@
|
|
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
|
+
autoload :Customer, 'mousetrap/customer'
|
10
|
+
autoload :Plan, 'mousetrap/plan'
|
11
|
+
autoload :Resource, 'mousetrap/resource'
|
12
|
+
autoload :Subscription, 'mousetrap/subscription'
|
13
|
+
|
14
|
+
class << self
|
15
|
+
attr_accessor :product_code
|
16
|
+
|
17
|
+
def authenticate(user, password)
|
18
|
+
Resource.basic_auth user, password
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|