cameroncox-mousetrap 0.5.1

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 = "cameroncox-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/cameroncox/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.1
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
@@ -0,0 +1,162 @@
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 => itemCode, :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 => itemCode, :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
+ :email => attributes[:email],
123
+ :firstName => attributes[:first_name],
124
+ :lastName => attributes[:last_name],
125
+ :company => attributes[:company]
126
+ }
127
+ mutated_hash.merge!(:code => attributes[:code]) if new_record
128
+ mutated_hash
129
+ end
130
+
131
+ def self.attributes_from_api(attributes)
132
+ {
133
+ :id => attributes['id'],
134
+ :code => attributes['code'],
135
+ :first_name => attributes['firstName'],
136
+ :last_name => attributes['lastName'],
137
+ :company => attributes['company'],
138
+ :email => attributes['email']
139
+ }
140
+ end
141
+
142
+ def create
143
+ response = self.class.post_resource 'customers', 'new', attributes_for_api_with_subscription
144
+
145
+ raise response['error'] if response['error']
146
+
147
+ returned_customer = self.class.build_resource_from response
148
+ self.id = returned_customer.id
149
+ response
150
+ end
151
+
152
+ def update
153
+ if subscription
154
+ response = self.class.put_resource 'customers', 'edit', code, attributes_for_api_with_subscription
155
+ else
156
+ response = self.class.put_resource 'customers', 'edit-customer', code, attributes_for_api
157
+ end
158
+
159
+ raise response['error'] if response['error']
160
+ end
161
+ end
162
+ end
@@ -0,0 +1,30 @@
1
+ module Mousetrap
2
+ class Plan < Resource
3
+ attr_accessor \
4
+ :code,
5
+ :name
6
+
7
+ def self.all
8
+ response = get_resources plural_resource_name
9
+ return [] unless response['plans']
10
+ build_resources_from response
11
+ end
12
+
13
+ protected
14
+
15
+ def self.plural_resource_name
16
+ 'plans'
17
+ end
18
+
19
+ def self.singular_resource_name
20
+ 'plan'
21
+ end
22
+
23
+ def self.attributes_from_api(attributes)
24
+ {
25
+ :code => attributes['code'],
26
+ :name => attributes['name']
27
+ }
28
+ end
29
+ end
30
+ 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