marcguyer-cheddargetter-client 0.2.0

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,14 @@
1
+ *~
2
+ #*#
3
+ */.DS_Store
4
+ *.marks
5
+ .buildpath
6
+ .project
7
+ .settings
8
+ *.tmproj
9
+ nbproject*
10
+ *.sw?
11
+ .DS_Store
12
+ coverage
13
+ rdoc
14
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Marc Guyer
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,7 @@
1
+ = cheddargetter-client
2
+
3
+ CheddarGetter Client is a wrapper for the CheddarGetter API
4
+
5
+ == Copyright
6
+
7
+ Copyright (c) 2009 Marc Guyer. See LICENSE for details.
@@ -0,0 +1,56 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "cheddargetter-client"
8
+ gem.summary = %Q{CheddarGetter Client is a wrapper for the CheddarGetter API}
9
+ gem.email = "marc@cheddargetter.com"
10
+ gem.homepage = "http://github.com/marcguyer/cheddargetter-client"
11
+ gem.authors = ["Marc Guyer"]
12
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
13
+ end
14
+
15
+ rescue LoadError
16
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
17
+ end
18
+
19
+ require 'rake/testtask'
20
+ Rake::TestTask.new(:test) do |test|
21
+ test.libs << 'lib' << 'test'
22
+ test.pattern = 'test/**/*_test.rb'
23
+ test.verbose = true
24
+ end
25
+
26
+ begin
27
+ require 'rcov/rcovtask'
28
+ Rcov::RcovTask.new do |test|
29
+ test.libs << 'test'
30
+ test.pattern = 'test/**/*_test.rb'
31
+ test.verbose = true
32
+ end
33
+ rescue LoadError
34
+ task :rcov do
35
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
36
+ end
37
+ end
38
+
39
+
40
+ task :default => :test
41
+
42
+ require 'rake/rdoctask'
43
+ Rake::RDocTask.new do |rdoc|
44
+ if File.exist?('VERSION.yml')
45
+ config = YAML.load(File.read('VERSION.yml'))
46
+ version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
47
+ else
48
+ version = ""
49
+ end
50
+
51
+ rdoc.rdoc_dir = 'rdoc'
52
+ rdoc.title = "cheddargetter-client #{version}"
53
+ rdoc.rdoc_files.include('README*')
54
+ rdoc.rdoc_files.include('lib/**/*.rb')
55
+ end
56
+
@@ -0,0 +1,4 @@
1
+ ---
2
+ :minor: 2
3
+ :patch: 0
4
+ :major: 0
@@ -0,0 +1,46 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{cheddargetter-client}
5
+ s.version = "0.2.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Marc Guyer"]
9
+ s.date = %q{2009-07-30}
10
+ s.email = %q{marc@cheddargetter.com}
11
+ s.extra_rdoc_files = [
12
+ "LICENSE",
13
+ "README.rdoc"
14
+ ]
15
+ s.files = [
16
+ ".document",
17
+ ".gitignore",
18
+ "LICENSE",
19
+ "README.rdoc",
20
+ "Rakefile",
21
+ "VERSION.yml",
22
+ "cheddargetter-client.gemspec",
23
+ "lib/cheddargetter-client.rb",
24
+ "test/cheddargetter-client_test.rb",
25
+ "test/test_helper.rb"
26
+ ]
27
+ s.homepage = %q{http://github.com/marcguyer/cheddargetter-client}
28
+ s.rdoc_options = ["--charset=UTF-8"]
29
+ s.require_paths = ["lib"]
30
+ s.rubygems_version = %q{1.3.5}
31
+ s.summary = %q{CheddarGetter Client is a wrapper for the CheddarGetter API}
32
+ s.test_files = [
33
+ "test/cheddargetter-client_test.rb",
34
+ "test/test_helper.rb"
35
+ ]
36
+
37
+ if s.respond_to? :specification_version then
38
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
39
+ s.specification_version = 3
40
+
41
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
42
+ else
43
+ end
44
+ else
45
+ end
46
+ end
@@ -0,0 +1,246 @@
1
+ require 'rubygems'
2
+ require 'httparty'
3
+ require 'libxml'
4
+ require 'pp'
5
+ require 'json'
6
+
7
+ module CheddarGetter
8
+
9
+ class Request
10
+ include HTTParty
11
+ headers 'User-Agent' => 'CheddarGetter Client Ruby'
12
+ format :plain
13
+
14
+ def initialize(url, username, password, productCode = nil)
15
+ self.class.base_uri url
16
+ self.class.basic_auth username, password
17
+ @productCode = productCode
18
+ end
19
+
20
+ def productCode=(productCode)
21
+ @productCode = productCode
22
+ end
23
+
24
+ def plans(filters = nil)
25
+ return Response.new(request('/plans/get', filters))
26
+ end
27
+
28
+ def plan(code, id = nil)
29
+ raise ArgumentError, 'code|id is required' if code.blank? || id.blank?
30
+ return Response.new(request('/plans/get/' + (id!=nil ? id : code)))
31
+ end
32
+
33
+ def newPlan(data)
34
+ return Response.new(request('/plans/new', data))
35
+ end
36
+
37
+ def editPlan(code, id = nil, data)
38
+ raise ArgumentError, 'code|id is required' if code.blank? || id.blank?
39
+ return Response.new(request('/plans/edit/' + (id ? 'id/' + id : 'code/' + code), data))
40
+ end
41
+
42
+ def deletePlan(code, id)
43
+ raise ArgumentError, 'code|id is required' if code.blank? || id.blank?
44
+ return Response.new(request('/plans/delete/' + (id ? 'id/' + id : 'code/' + code)))
45
+ end
46
+
47
+ def customers(filters = nil)
48
+ return Response.new(request('/customers/get', filters))
49
+ end
50
+
51
+ def customer(code, id = nil)
52
+ raise ArgumentError, 'code|id is required' if code.blank? || id.blank?
53
+ return Response.new(request('/customers/get/' + (id!=nil ? id : code)))
54
+ end
55
+
56
+ def newCustomer(data)
57
+ return Response.new(request('/customers/new', data))
58
+ end
59
+
60
+ def editCustomer(code, id = nil, data)
61
+ raise ArgumentError, 'code|id is required' if code.blank? || id.blank?
62
+ return Response.new(request('/customers/edit/' + (id!=nil ? id : code), data))
63
+ end
64
+
65
+ def deleteCustomer(code, id = nil)
66
+ raise ArgumentError, 'code|id is required' if code.blank? || id.blank?
67
+ return Response.new(request('/customers/delete/' + (id!=nil ? id : code), data))
68
+ end
69
+
70
+ def editSubscription(code, id = nil, data)
71
+ raise ArgumentError, 'code|id is required' if code.blank? || id.blank?
72
+ return Response.new(request('/customers/edit-subscription/' + (id!=nil ? id : code), data))
73
+ end
74
+
75
+ def cancelSubscription(code, id = nil)
76
+ raise ArgumentError, 'code|id is required' if code.blank? || id.blank?
77
+ return Response.new(request('/customers/cancel/' + (id!=nil ? id : code)))
78
+ end
79
+
80
+ def addItemQuantity(code, id = nil, data)
81
+ raise ArgumentError, 'code|id is required' if code.blank? || id.blank?
82
+ return Response.new(request('/customers/add-item-quantity/' + (id!=nil ? id : code), data))
83
+ end
84
+
85
+ def removeItemQuantity(code, id = nil, data)
86
+ raise ArgumentError, 'code|id is required' if code.blank? || id.blank?
87
+ return Response.new(request('/customers/remove-item-quantity/' + (id!=nil ? id : code), data))
88
+ end
89
+
90
+ def setItemQuantity(code, id = nil, data)
91
+ raise ArgumentError, 'code|id is required' if code.blank? || id.blank?
92
+ return Response.new(request('/customers/set-item-quantity/' + (id!=nil ? id : code), data))
93
+ end
94
+
95
+ def request(path, args = nil)
96
+ path = '/xml/' + path + (@productCode!=nil ? '/productCode/' + @productCode : '')
97
+ # convert multiple adjacent slashes to single slashes
98
+ path = path.gsub(/(\w)\/+/, '\1/')
99
+
100
+ if (args != nil)
101
+ return Request.post(path, :query => args).to_s
102
+ else
103
+ return Request.get(path).to_s
104
+ end
105
+ end
106
+
107
+ end
108
+
109
+ class Response
110
+ include LibXML
111
+
112
+ def initialize(xml)
113
+ @document = XML::Document.string(xml)
114
+ @responseType = @document.root.name
115
+ end
116
+
117
+ def to_s
118
+ return @document.to_s
119
+ end
120
+
121
+ def to_a
122
+ if (@array != nil)
123
+ return @array
124
+ end
125
+
126
+ if (@responseType == 'error')
127
+ return [
128
+ {
129
+ 'code' => @document.root.attributes.get_attribute('code').value,
130
+ 'message' => @document.root.first.content
131
+ }
132
+ ]
133
+ end
134
+ @array = _toArray(@document.root.children);
135
+ return @array;
136
+
137
+ end
138
+
139
+ def to_json
140
+ return self.to_a.to_json
141
+ end
142
+
143
+ def plan(code = nil)
144
+ raise ArgumentException 'Can\'t get a plan from a response that isn\'t of type \'plans\'' if @responseType != 'plans'
145
+ raise 'This response contains more than one plan so you need to provide the code for the plan you wish to get' if code.blank? && @document.root.children.length > 1
146
+
147
+ if code.blank?
148
+ return to_a.first
149
+ end
150
+ return to_a[code]
151
+ end
152
+
153
+ def planItem(code = nil, itemCode = nil)
154
+ plan = plan(code)
155
+ raise ArgumentException 'This plan contains more than one item so you need to provide the code for the item you wish to get' if itemCode.blank? && plan['items'].length > 1
156
+
157
+ if itemCode.blank?
158
+ return plan['items'].first
159
+ end
160
+ return plan['items'][itemCode]
161
+ end
162
+
163
+ def customer(code = nil)
164
+ raise ArgumentException 'Can\'t get a customer from a response that isn\'t of type \'customers\'' if @responseType != 'customers'
165
+ raise ArgumentException 'This response contains more than one customer so you need to provide the code for the customer you wish to get' if code.blank? && @document.root.children.length > 1
166
+
167
+ if code.blank?
168
+ return to_a.first
169
+ end
170
+ return to_a[code]
171
+ end
172
+
173
+ def customerSubscription(code = nil)
174
+ customer = customer(code)
175
+ return customer['subscriptions'].first
176
+ end
177
+
178
+ def customerPlan(code = nil)
179
+ subscription = customerSubscription(code)
180
+ return subscription['plans'].first
181
+ end
182
+
183
+ def customerInvoice(code = nil)
184
+ subscription = customerSubscription(code)
185
+ return subscription['invoices'].first
186
+ end
187
+
188
+ def customerItemQuantity(code = nil, itemCode = nil)
189
+ subscription = customerSubscription(code)
190
+ raise ArgumentException 'This customer\'s subscription contains more than one item so you need to provide the code for the item you wish to get' if itemCode.blank? && subscription['items'].length > 1
191
+ plan = customerPlan(code)
192
+ if !itemCode.blank?
193
+ return {
194
+ 'item' => plan['items'][itemCode],
195
+ 'quantity' => subscription['items'][itemCode]
196
+ }
197
+ else
198
+ return {
199
+ 'item' => plan['items'].first,
200
+ 'quantity' => subscription['items'].first
201
+ }
202
+ end
203
+ end
204
+
205
+ private
206
+
207
+ def _toArray(nodes)
208
+ array = Hash.new(0)
209
+ nodes.each { |node|
210
+ if (!node.element?)
211
+ next
212
+ end
213
+
214
+ if (node.attributes?) #deep
215
+ if (!node.attributes['code'].blank?)
216
+ key = node.attributes['code']
217
+ array[key] = Hash.new()
218
+ if (!node.attributes['id'].blank?)
219
+ array[key] = {
220
+ 'id' => node.attributes['id']
221
+ }
222
+ end
223
+ array[key]['code'] = key
224
+ else
225
+ key = node.attributes['id']
226
+ array[key] = Hash.new(0)
227
+ end
228
+ array[key] = array[key].merge(_toArray(node.children))
229
+ else
230
+ if (node.children.length > 1) # sub array
231
+ array[node.name] = _toArray(node.children)
232
+ else
233
+ array[node.name] = node.content
234
+ end
235
+ end
236
+ }
237
+ return array
238
+ end
239
+
240
+ end
241
+
242
+ class ResponseException < Exception
243
+
244
+ end
245
+
246
+ end
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class CheddargetterClientTest < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'cheddargetter-client'
8
+
9
+ class Test::Unit::TestCase
10
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: marcguyer-cheddargetter-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Marc Guyer
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-07-30 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: marc@cheddargetter.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.rdoc
25
+ files:
26
+ - .document
27
+ - .gitignore
28
+ - LICENSE
29
+ - README.rdoc
30
+ - Rakefile
31
+ - VERSION.yml
32
+ - cheddargetter-client.gemspec
33
+ - lib/cheddargetter-client.rb
34
+ - test/cheddargetter-client_test.rb
35
+ - test/test_helper.rb
36
+ has_rdoc: false
37
+ homepage: http://github.com/marcguyer/cheddargetter-client
38
+ licenses:
39
+ post_install_message:
40
+ rdoc_options:
41
+ - --charset=UTF-8
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: "0"
49
+ version:
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: "0"
55
+ version:
56
+ requirements: []
57
+
58
+ rubyforge_project:
59
+ rubygems_version: 1.3.5
60
+ signing_key:
61
+ specification_version: 3
62
+ summary: CheddarGetter Client is a wrapper for the CheddarGetter API
63
+ test_files:
64
+ - test/cheddargetter-client_test.rb
65
+ - test/test_helper.rb