ambethia-mousetrap 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -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
@@ -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.
@@ -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.
@@ -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 = "ambethia-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/ambethia/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
@@ -0,0 +1,89 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{ambethia-mousetrap}
8
+ s.version = "0.5.1"
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{2010-06-28}
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
+ "ambethia-mousetrap.gemspec",
26
+ "lib/mousetrap.rb",
27
+ "lib/mousetrap/customer.rb",
28
+ "lib/mousetrap/plan.rb",
29
+ "lib/mousetrap/resource.rb",
30
+ "lib/mousetrap/subscription.rb",
31
+ "script/authenticate.rb",
32
+ "script/cheddar_getter.example.yml",
33
+ "script/console",
34
+ "spec/factories.rb",
35
+ "spec/integration/settings.example.yml",
36
+ "spec/integration/smoke_test.rb",
37
+ "spec/integration/spike.rb",
38
+ "spec/mousetrap/customer_spec.rb",
39
+ "spec/mousetrap/plan_spec.rb",
40
+ "spec/mousetrap/resource_spec.rb",
41
+ "spec/mousetrap/subscription_spec.rb",
42
+ "spec/mousetrap_spec.rb",
43
+ "spec/spec.opts",
44
+ "spec/spec_helper.rb",
45
+ "spec/support/fixtures.rb",
46
+ "spec/support/random_data.rb"
47
+ ]
48
+ s.homepage = %q{http://github.com/ambethia/mousetrap}
49
+ s.rdoc_options = ["--charset=UTF-8"]
50
+ s.require_paths = ["lib"]
51
+ s.rubygems_version = %q{1.3.7}
52
+ s.summary = %q{CheddarGetter API Client in Ruby}
53
+ s.test_files = [
54
+ "spec/factories.rb",
55
+ "spec/integration/smoke_test.rb",
56
+ "spec/integration/spike.rb",
57
+ "spec/mousetrap/customer_spec.rb",
58
+ "spec/mousetrap/plan_spec.rb",
59
+ "spec/mousetrap/resource_spec.rb",
60
+ "spec/mousetrap/subscription_spec.rb",
61
+ "spec/mousetrap_spec.rb",
62
+ "spec/spec_helper.rb",
63
+ "spec/support/fixtures.rb",
64
+ "spec/support/random_data.rb"
65
+ ]
66
+
67
+ if s.respond_to? :specification_version then
68
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
69
+ s.specification_version = 3
70
+
71
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
72
+ s.add_runtime_dependency(%q<httparty>, [">= 0.4.2"])
73
+ s.add_development_dependency(%q<activesupport>, [">= 2.3.3"])
74
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
75
+ s.add_development_dependency(%q<factory_girl>, [">= 1.2.3"])
76
+ else
77
+ s.add_dependency(%q<httparty>, [">= 0.4.2"])
78
+ s.add_dependency(%q<activesupport>, [">= 2.3.3"])
79
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
80
+ s.add_dependency(%q<factory_girl>, [">= 1.2.3"])
81
+ end
82
+ else
83
+ s.add_dependency(%q<httparty>, [">= 0.4.2"])
84
+ s.add_dependency(%q<activesupport>, [">= 2.3.3"])
85
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
86
+ s.add_dependency(%q<factory_girl>, [">= 1.2.3"])
87
+ end
88
+ end
89
+
@@ -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,152 @@
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 attributes_for_api
28
+ # TODO: superclass?
29
+ self.class.attributes_for_api(attributes, new_record?)
30
+ end
31
+
32
+ def attributes_for_api_with_subscription
33
+ raise "Must have subscription" unless subscription
34
+ a = attributes_for_api
35
+ a[:subscription] = subscription.attributes_for_api
36
+ a
37
+ end
38
+
39
+ def cancel
40
+ member_action 'cancel' unless new_record?
41
+ end
42
+
43
+ def new?
44
+ if api_customer = self.class[code]
45
+ self.id = api_customer.id
46
+
47
+ return false
48
+ else
49
+ return true
50
+ end
51
+ end
52
+
53
+ def save
54
+ new? ? create : update
55
+ end
56
+
57
+ def switch_to_plan(plan_code)
58
+ raise "Can only call this on an existing CheddarGetter customer." unless exists?
59
+
60
+ attributes = { :planCode => plan_code }
61
+ self.class.put_resource('customers', 'edit-subscription', code, attributes)
62
+
63
+ # TODO: Refresh self with reload here?
64
+ end
65
+
66
+ def self.all
67
+ response = get_resources 'customers'
68
+
69
+ if response['error']
70
+ if response['error'] =~ /No customers found/
71
+ return []
72
+ else
73
+ raise response['error']
74
+ end
75
+ end
76
+
77
+ build_resources_from response
78
+ end
79
+
80
+ def self.create(attributes)
81
+ object = new(attributes)
82
+ object.send(:create)
83
+ object
84
+ end
85
+
86
+ def self.new_from_api(attributes)
87
+ customer = new(attributes_from_api(attributes))
88
+ subscription_attrs = attributes['subscriptions']['subscription']
89
+ customer.subscription = Subscription.new_from_api(subscription_attrs.kind_of?(Array) ? subscription_attrs.first : subscription_attrs)
90
+ customer
91
+ end
92
+
93
+ def self.update(customer_code, attributes)
94
+ customer = new(attributes)
95
+ customer.code = customer_code
96
+ customer.send :update
97
+ end
98
+
99
+
100
+ protected
101
+
102
+ def self.plural_resource_name
103
+ 'customers'
104
+ end
105
+
106
+ def self.singular_resource_name
107
+ 'customer'
108
+ end
109
+
110
+ def self.attributes_for_api(attributes, new_record = true)
111
+ mutated_hash = {
112
+ :email => attributes[:email],
113
+ :firstName => attributes[:first_name],
114
+ :lastName => attributes[:last_name],
115
+ :company => attributes[:company]
116
+ }
117
+ mutated_hash.merge!(:code => attributes[:code]) if new_record
118
+ mutated_hash
119
+ end
120
+
121
+ def self.attributes_from_api(attributes)
122
+ {
123
+ :id => attributes['id'],
124
+ :code => attributes['code'],
125
+ :first_name => attributes['firstName'],
126
+ :last_name => attributes['lastName'],
127
+ :company => attributes['company'],
128
+ :email => attributes['email']
129
+ }
130
+ end
131
+
132
+ def create
133
+ response = self.class.post_resource 'customers', 'new', attributes_for_api_with_subscription
134
+
135
+ raise response['error'] if response['error']
136
+
137
+ returned_customer = self.class.build_resource_from response
138
+ self.id = returned_customer.id
139
+ response
140
+ end
141
+
142
+ def update
143
+ if subscription
144
+ response = self.class.put_resource 'customers', 'edit', code, attributes_for_api_with_subscription
145
+ else
146
+ response = self.class.put_resource 'customers', 'edit-customer', code, attributes_for_api
147
+ end
148
+
149
+ raise response['error'] if response['error']
150
+ end
151
+ end
152
+ 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