rlivsey-voorhees 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +5 -0
- data/LICENSE +20 -0
- data/README.markdown +205 -0
- data/Rakefile +48 -0
- data/VERSION +1 -0
- data/examples/twitter.rb +85 -0
- data/lib/voorhees.rb +5 -0
- data/lib/voorhees/config.rb +86 -0
- data/lib/voorhees/exceptions.rb +9 -0
- data/lib/voorhees/request.rb +132 -0
- data/lib/voorhees/resource.rb +132 -0
- data/lib/voorhees/response.rb +29 -0
- data/spec/config_spec.rb +144 -0
- data/spec/fixtures/resources.rb +18 -0
- data/spec/fixtures/user.json +31 -0
- data/spec/fixtures/users.json +1 -0
- data/spec/request_spec.rb +446 -0
- data/spec/resource_spec.rb +301 -0
- data/spec/response_spec.rb +93 -0
- data/spec/spec_helper.rb +22 -0
- data/spec/voorhees_spec.rb +1 -0
- metadata +82 -0
@@ -0,0 +1,301 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe User do
|
4
|
+
|
5
|
+
before :each do
|
6
|
+
load_json
|
7
|
+
end
|
8
|
+
|
9
|
+
describe "ClassMethods" do
|
10
|
+
describe "new_from_json" do
|
11
|
+
|
12
|
+
it "Should create a new User" do
|
13
|
+
User.should_receive(:new).once.and_return(mock(:user, :null_object => true))
|
14
|
+
user_from_json
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should assign the JSON to User#raw_json" do
|
18
|
+
mock_user = mock(:user, :null_object => true)
|
19
|
+
User.stub!(:new).and_return(mock_user)
|
20
|
+
mock_user.should_receive(:raw_json=).with(@json)
|
21
|
+
user_from_json
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should assign the hierarchy to User#json_hierarchy" do
|
25
|
+
mock_user = mock(:user, :null_object => true)
|
26
|
+
User.stub!(:new).and_return(mock_user)
|
27
|
+
mock_user.should_receive(:json_hierarchy=).with(@hierarchy)
|
28
|
+
user_from_json
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should return the new user" do
|
32
|
+
user_from_json.should be_an_instance_of(User)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe "json_request" do
|
37
|
+
|
38
|
+
before :each do
|
39
|
+
@request = mock(:request, :null_object => true)
|
40
|
+
@response = mock(:response, :null_object => true)
|
41
|
+
@objects = [mock(:object)]
|
42
|
+
|
43
|
+
Voorhees::Request.stub!(:new).and_return(@request)
|
44
|
+
@request.stub!(:perform).and_return(@response)
|
45
|
+
@response.stub!(:to_objects).and_return(@objects)
|
46
|
+
end
|
47
|
+
|
48
|
+
def perform_request
|
49
|
+
User.json_request{}
|
50
|
+
end
|
51
|
+
|
52
|
+
|
53
|
+
it "should call Request.new with the current class if no class is passed" do
|
54
|
+
Voorhees::Request.should_receive(:new).with(User).and_return(@request)
|
55
|
+
perform_request
|
56
|
+
end
|
57
|
+
|
58
|
+
it "should call Request.new with the specified class if a class is passed" do
|
59
|
+
Voorhees::Request.should_receive(:new).with(Message).and_return(@request)
|
60
|
+
User.json_request(Message) do |request|
|
61
|
+
# ...
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
|
66
|
+
it "should yeild a request" do
|
67
|
+
User.json_request do |r|
|
68
|
+
r.should == @request
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
it "should raise a LocalJumpError exception if a block is not given" do
|
73
|
+
lambda{
|
74
|
+
User.json_request
|
75
|
+
}.should raise_error(LocalJumpError)
|
76
|
+
end
|
77
|
+
|
78
|
+
it "should implicitly call Request#perform" do
|
79
|
+
@request.should_receive(:perform).once
|
80
|
+
perform_request
|
81
|
+
end
|
82
|
+
|
83
|
+
it "should return the result of Response#to_objects" do
|
84
|
+
perform_request.should == @objects
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
describe "json_service" do
|
89
|
+
|
90
|
+
before :each do
|
91
|
+
@service_name = :list
|
92
|
+
@service_attrs = {
|
93
|
+
:timeout => 100,
|
94
|
+
:required => [:monkeys]
|
95
|
+
}
|
96
|
+
end
|
97
|
+
|
98
|
+
def define_service
|
99
|
+
User.json_service @service_name, @service_attrs
|
100
|
+
end
|
101
|
+
|
102
|
+
it "should define a method with the same name as the service" do
|
103
|
+
User.should_not respond_to(@service_name)
|
104
|
+
define_service
|
105
|
+
User.should respond_to(@service_name)
|
106
|
+
end
|
107
|
+
|
108
|
+
describe "calling the defined method" do
|
109
|
+
|
110
|
+
before :each do
|
111
|
+
define_service
|
112
|
+
|
113
|
+
@request = mock(:request, :null_object => true)
|
114
|
+
@response = mock(:response, :null_object => true)
|
115
|
+
@objects = [mock(:object)]
|
116
|
+
|
117
|
+
Voorhees::Request.stub!(:new).and_return(@request)
|
118
|
+
@request.stub!(:perform).and_return(@response)
|
119
|
+
end
|
120
|
+
|
121
|
+
it "should call User#json_request" do
|
122
|
+
User.should_receive(:json_request).and_return(@response)
|
123
|
+
User.list
|
124
|
+
end
|
125
|
+
|
126
|
+
it "should pass service attributes onto the request" do
|
127
|
+
@service_attrs.each do |key, value|
|
128
|
+
@request.should_receive("#{key}=").with(value)
|
129
|
+
end
|
130
|
+
User.list
|
131
|
+
end
|
132
|
+
|
133
|
+
it "should use any hash passed in to set the request parameters" do
|
134
|
+
params = {:monkeys => true}
|
135
|
+
@request.should_receive(:parameters=).with(params)
|
136
|
+
User.list(params)
|
137
|
+
end
|
138
|
+
|
139
|
+
it "should return the result of Response#to_objects" do
|
140
|
+
@response.should_receive(:to_objects).and_return(@objects)
|
141
|
+
User.list.should == @objects
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
describe "InstanceMethods" do
|
148
|
+
|
149
|
+
describe "#raw_json" do
|
150
|
+
it "should contain the raw json" do
|
151
|
+
user_from_json.raw_json.should == @json
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
describe "#json_attributes" do
|
156
|
+
it "should contain symbols of the keys of the attributes available" do
|
157
|
+
user_from_json.json_attributes.sort.should == [:address, :email, :id, :messages, :name, :pet, :username]
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
describe "#json_request" do
|
162
|
+
|
163
|
+
before :each do
|
164
|
+
@request = mock(:request, :null_object => true)
|
165
|
+
@response = mock(:response, :null_object => true)
|
166
|
+
|
167
|
+
Voorhees::Request.stub!(:new).and_return(@request)
|
168
|
+
@request.stub!(:perform).and_return(@response)
|
169
|
+
end
|
170
|
+
|
171
|
+
def perform_request
|
172
|
+
user_from_json.json_request{}
|
173
|
+
end
|
174
|
+
|
175
|
+
it "should pass the request to the class method" do
|
176
|
+
User.should_receive(:json_request)
|
177
|
+
user_from_json.json_request{}
|
178
|
+
end
|
179
|
+
|
180
|
+
it "should raise a LocalJumpError exception if a block is not given" do
|
181
|
+
lambda{
|
182
|
+
user_from_json.json_request
|
183
|
+
}.should raise_error(LocalJumpError)
|
184
|
+
end
|
185
|
+
|
186
|
+
it "should implicitly call Request#perform" do
|
187
|
+
@request.should_receive(:perform).once
|
188
|
+
perform_request
|
189
|
+
end
|
190
|
+
|
191
|
+
it "should return the result of Request#perform" do
|
192
|
+
perform_request.should == @response
|
193
|
+
end
|
194
|
+
|
195
|
+
end
|
196
|
+
|
197
|
+
describe "calling assignment method with name of a json attribute" do
|
198
|
+
|
199
|
+
it "should define an assignment method" do
|
200
|
+
user = user_from_json
|
201
|
+
|
202
|
+
user.should_not respond_to(:email=)
|
203
|
+
user.email = "test"
|
204
|
+
user.should respond_to(:email=)
|
205
|
+
end
|
206
|
+
|
207
|
+
it "should assign the value" do
|
208
|
+
user = user_from_json
|
209
|
+
new_email = "a_new_address@example.com"
|
210
|
+
|
211
|
+
user.email = new_email
|
212
|
+
user.email.should == new_email
|
213
|
+
end
|
214
|
+
|
215
|
+
end
|
216
|
+
|
217
|
+
describe "calling method with the name of a json attribute" do
|
218
|
+
|
219
|
+
it "should define a method of the same name" do
|
220
|
+
user = user_from_json
|
221
|
+
|
222
|
+
user.should_not respond_to(:email)
|
223
|
+
user.email
|
224
|
+
user.should respond_to(:email)
|
225
|
+
end
|
226
|
+
|
227
|
+
it "should return the correct data from defined methods" do
|
228
|
+
user = user_from_json
|
229
|
+
|
230
|
+
user.email # first access, now it's defined
|
231
|
+
user.email.should == @json["email"]
|
232
|
+
end
|
233
|
+
|
234
|
+
describe "which is a simple value" do
|
235
|
+
it "should return the value of the attribute" do
|
236
|
+
user_from_json.email.should == @json["email"]
|
237
|
+
end
|
238
|
+
end
|
239
|
+
|
240
|
+
describe "which is a collection" do
|
241
|
+
it "should return an array" do
|
242
|
+
user_from_json.messages.should be_an_instance_of(Array)
|
243
|
+
end
|
244
|
+
|
245
|
+
it "should infer the type of objects based on the collection name" do
|
246
|
+
user_from_json.messages.each do |m|
|
247
|
+
m.should be_an_instance_of(Message)
|
248
|
+
end
|
249
|
+
end
|
250
|
+
end
|
251
|
+
|
252
|
+
describe "which is a sub-object" do
|
253
|
+
|
254
|
+
it "should return as a Hash if the hierarchy is not defined" do
|
255
|
+
@hierarchy = {
|
256
|
+
}
|
257
|
+
user_from_json.pet.should be_a(Hash)
|
258
|
+
end
|
259
|
+
|
260
|
+
it "should return as the right class if the hierarchy is defined as symbol" do
|
261
|
+
@hierarchy = {
|
262
|
+
:address => :address
|
263
|
+
}
|
264
|
+
user_from_json.address.should be_a(Address)
|
265
|
+
end
|
266
|
+
|
267
|
+
it "should return as the right class if the hierarchy is defined as Class" do
|
268
|
+
@hierarchy = {
|
269
|
+
:address => Address
|
270
|
+
}
|
271
|
+
user_from_json.address.should be_a(Address)
|
272
|
+
end
|
273
|
+
|
274
|
+
it "should return as the right class for multiple depths" do
|
275
|
+
@hierarchy = {
|
276
|
+
:address => [Address, {
|
277
|
+
:coords => LatLon
|
278
|
+
}]
|
279
|
+
}
|
280
|
+
user_from_json.address.coords.should be_a(LatLon)
|
281
|
+
end
|
282
|
+
|
283
|
+
end
|
284
|
+
|
285
|
+
end
|
286
|
+
|
287
|
+
end
|
288
|
+
end
|
289
|
+
|
290
|
+
def load_json
|
291
|
+
body = ''
|
292
|
+
path = File.expand_path(File.dirname(__FILE__) + '/fixtures/user.json')
|
293
|
+
File.open(path, 'r') do |f|
|
294
|
+
body = f.read
|
295
|
+
end
|
296
|
+
@json = JSON.parse(body)
|
297
|
+
end
|
298
|
+
|
299
|
+
def user_from_json
|
300
|
+
User.new_from_json(@json, @hierarchy)
|
301
|
+
end
|
@@ -0,0 +1,93 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe Voorhees::Response do
|
4
|
+
|
5
|
+
before :each do
|
6
|
+
User.stub!(:new_from_json).and_return(User.new)
|
7
|
+
end
|
8
|
+
|
9
|
+
describe "to_objects" do
|
10
|
+
|
11
|
+
describe "with no class set" do
|
12
|
+
|
13
|
+
before :each do
|
14
|
+
@klass = nil
|
15
|
+
build_response(:users)
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should return nil" do
|
19
|
+
@response.to_objects.should be_nil
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
describe "with a class which does not have Voorhees::Resource mixed in" do
|
25
|
+
|
26
|
+
before :each do
|
27
|
+
@klass = NotResource
|
28
|
+
build_response(:users)
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should raise a Voorhees::NotResourceError exception" do
|
32
|
+
lambda{
|
33
|
+
@response.to_objects
|
34
|
+
}.should raise_error(Voorhees::NotResourceError)
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
describe "with a class which has Voorhees::Resource mixed in" do
|
40
|
+
|
41
|
+
before :each do
|
42
|
+
@klass = User
|
43
|
+
end
|
44
|
+
|
45
|
+
describe "with JSON containing an array of 2 items" do
|
46
|
+
|
47
|
+
before :each do
|
48
|
+
build_response(:users)
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should return an array of 2 objects of the right class" do
|
52
|
+
users = @response.to_objects
|
53
|
+
users.length.should == 2
|
54
|
+
users.each do |u|
|
55
|
+
u.should be_an_instance_of(User)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
it "should create objects by sending the JSON and hierarchy to Class.new_from_json" do
|
60
|
+
User.should_receive(:new_from_json).with(@response.json[0], @hierarchy).ordered
|
61
|
+
User.should_receive(:new_from_json).with(@response.json[1], @hierarchy).ordered
|
62
|
+
@response.to_objects
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
describe "with JSON containing one item" do
|
67
|
+
|
68
|
+
before :each do
|
69
|
+
build_response(:user)
|
70
|
+
end
|
71
|
+
|
72
|
+
it "should return one object of the right class" do
|
73
|
+
user = @response.to_objects
|
74
|
+
user.should be_an_instance_of(User)
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def build_response(fixture)
|
83
|
+
body = ''
|
84
|
+
path = File.expand_path(File.dirname(__FILE__) + "/fixtures/#{fixture}.json")
|
85
|
+
File.open(path, 'r') do |f|
|
86
|
+
body = f.read
|
87
|
+
end
|
88
|
+
|
89
|
+
@hierarchy = {
|
90
|
+
:address => Address
|
91
|
+
}
|
92
|
+
@response = Voorhees::Response.new(JSON.parse(body), @klass, @hierarchy)
|
93
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'spec'
|
2
|
+
require 'rubygems'
|
3
|
+
require 'json'
|
4
|
+
require 'active_support'
|
5
|
+
|
6
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
7
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
8
|
+
require 'voorhees'
|
9
|
+
|
10
|
+
require File.expand_path(File.dirname(__FILE__) + '/fixtures/resources')
|
11
|
+
|
12
|
+
Spec::Runner.configure do |config|
|
13
|
+
|
14
|
+
end
|
15
|
+
|
16
|
+
# allow sorting by symbol
|
17
|
+
class Symbol
|
18
|
+
def <=>(a)
|
19
|
+
self.to_s <=> a.to_s
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
@@ -0,0 +1 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
metadata
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rlivsey-voorhees
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Richard Livsey
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-06-21 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email: richard@livsey.org
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- LICENSE
|
24
|
+
- README.markdown
|
25
|
+
files:
|
26
|
+
- .document
|
27
|
+
- .gitignore
|
28
|
+
- LICENSE
|
29
|
+
- README.markdown
|
30
|
+
- Rakefile
|
31
|
+
- VERSION
|
32
|
+
- examples/twitter.rb
|
33
|
+
- lib/voorhees.rb
|
34
|
+
- lib/voorhees/config.rb
|
35
|
+
- lib/voorhees/exceptions.rb
|
36
|
+
- lib/voorhees/request.rb
|
37
|
+
- lib/voorhees/resource.rb
|
38
|
+
- lib/voorhees/response.rb
|
39
|
+
- spec/config_spec.rb
|
40
|
+
- spec/fixtures/resources.rb
|
41
|
+
- spec/fixtures/user.json
|
42
|
+
- spec/fixtures/users.json
|
43
|
+
- spec/request_spec.rb
|
44
|
+
- spec/resource_spec.rb
|
45
|
+
- spec/response_spec.rb
|
46
|
+
- spec/spec_helper.rb
|
47
|
+
- spec/voorhees_spec.rb
|
48
|
+
has_rdoc: false
|
49
|
+
homepage: http://github.com/rlivsey/voorhees
|
50
|
+
post_install_message:
|
51
|
+
rdoc_options:
|
52
|
+
- --charset=UTF-8
|
53
|
+
require_paths:
|
54
|
+
- lib
|
55
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: "0"
|
60
|
+
version:
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: "0"
|
66
|
+
version:
|
67
|
+
requirements: []
|
68
|
+
|
69
|
+
rubyforge_project:
|
70
|
+
rubygems_version: 1.2.0
|
71
|
+
signing_key:
|
72
|
+
specification_version: 3
|
73
|
+
summary: Library to consume and interract with JSON services
|
74
|
+
test_files:
|
75
|
+
- spec/config_spec.rb
|
76
|
+
- spec/fixtures/resources.rb
|
77
|
+
- spec/request_spec.rb
|
78
|
+
- spec/resource_spec.rb
|
79
|
+
- spec/response_spec.rb
|
80
|
+
- spec/spec_helper.rb
|
81
|
+
- spec/voorhees_spec.rb
|
82
|
+
- examples/twitter.rb
|