restful_serializer 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +60 -12
- data/lib/restful/configuration.rb +414 -0
- data/lib/restful/serializer.rb +121 -44
- data/lib/restful/version.rb +1 -1
- data/lib/restful.rb +102 -79
- data/spec/configuration_spec.rb +494 -0
- data/spec/restful_spec.rb +130 -46
- data/spec/serializer_spec.rb +179 -102
- data/spec/web_service_spec.rb +66 -0
- metadata +9 -4
data/spec/serializer_spec.rb
CHANGED
@@ -1,5 +1,8 @@
|
|
1
1
|
require File.expand_path(File.join(File.dirname(__FILE__), 'spec_helper'))
|
2
2
|
|
3
|
+
# These routes are only required by some tests, but route generation issues crop
|
4
|
+
# up depending on the order in which restful/* files are required. The reasons
|
5
|
+
# are mysterious, though they may have something to do with ActionController::UrlWriter
|
3
6
|
ActionController::Routing::Routes.clear!
|
4
7
|
ActionController::Routing::Routes.draw do |map|
|
5
8
|
map.resources :foos
|
@@ -73,15 +76,15 @@ describe Restful::Serializer do
|
|
73
76
|
end
|
74
77
|
|
75
78
|
after(:each) do
|
76
|
-
Restful.
|
77
|
-
Restful.model_configuration = {}
|
79
|
+
Restful.clear
|
78
80
|
end
|
79
81
|
|
80
|
-
|
82
|
+
def check_routing
|
81
83
|
ActionController::Routing::Routes.routes.size.should == 30
|
82
84
|
by_method = ActionController::Routing::Routes.routes.group_by { |r|
|
83
85
|
r.conditions[:method]
|
84
86
|
}
|
87
|
+
# ActionController::Routing::Routes.routes.each { |r| puts r }
|
85
88
|
by_method[:get].size.should == 4 * 4
|
86
89
|
by_method[:put].size.should == 4
|
87
90
|
by_method[:post].size.should == 4
|
@@ -89,44 +92,68 @@ describe Restful::Serializer do
|
|
89
92
|
by_method[nil].size.should == 2
|
90
93
|
end
|
91
94
|
|
92
|
-
it "should
|
93
|
-
|
94
|
-
Restful::Serializer.new('foo').should be_kind_of(Restful::Serializer)
|
95
|
+
it "should not interfere with resource route generation" do
|
96
|
+
check_routing
|
95
97
|
end
|
96
98
|
|
97
|
-
it "should
|
98
|
-
|
99
|
-
|
99
|
+
it "should not interfere with resource route generation if we configure webservices" do
|
100
|
+
Restful.register_web_service('test',
|
101
|
+
:resources => {
|
102
|
+
:foo => { :serialization => { :only => :id } },
|
103
|
+
}
|
104
|
+
)
|
105
|
+
check_routing
|
100
106
|
end
|
101
107
|
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
108
|
+
describe "with an empty web service" do
|
109
|
+
|
110
|
+
before(:each) do
|
111
|
+
@empty_ws = Restful.register_web_service('empty')
|
112
|
+
end
|
113
|
+
|
114
|
+
it "should require a subject and a web service" do
|
115
|
+
lambda { Restful::Serializer.new }.should raise_error(ArgumentError)
|
116
|
+
lambda { Restful::Serializer.new('foo') }.should raise_error(ArgumentError)
|
117
|
+
Restful::Serializer.new('foo', @empty_ws).should be_kind_of(Restful::Serializer)
|
118
|
+
end
|
119
|
+
|
120
|
+
it "should set klass" do
|
121
|
+
rs = Restful::Serializer.new(@foo, @empty_ws)
|
122
|
+
rs.klass.should == 'foo'
|
123
|
+
end
|
124
|
+
|
125
|
+
it "should serialize" do
|
126
|
+
@foo.save!
|
127
|
+
rs = Restful::Serializer.new(@foo, @empty_ws)
|
128
|
+
rs.serialize.should == {
|
109
129
|
'name' => @foo.name,
|
110
|
-
|
111
|
-
|
112
|
-
|
130
|
+
'foo' => {
|
131
|
+
'id' => @foo.id,
|
132
|
+
'name' => @foo.name,
|
133
|
+
},
|
134
|
+
'href' => "http://test.org/foos/#{@foo.id}",
|
135
|
+
}
|
136
|
+
end
|
113
137
|
end
|
114
138
|
|
115
139
|
describe "with configuration options" do
|
116
140
|
|
117
141
|
before(:each) do
|
118
|
-
Restful.
|
119
|
-
|
120
|
-
:
|
121
|
-
:
|
122
|
-
|
142
|
+
Restful.register_web_service('test',
|
143
|
+
:api_prefix => 'prefix',
|
144
|
+
:resources => {
|
145
|
+
:foo => {
|
146
|
+
:name_method => :fancy_name,
|
147
|
+
:serialization => { :only => [:name], :methods => [:a_method] },
|
148
|
+
}
|
123
149
|
}
|
124
|
-
|
150
|
+
)
|
151
|
+
@test_ws = Restful.web_service_configuration('test')
|
125
152
|
@foo.save!
|
126
153
|
end
|
127
154
|
|
128
155
|
it "should take options from configuration" do
|
129
|
-
rs = Restful::Serializer.new(@foo)
|
156
|
+
rs = Restful::Serializer.new(@foo, @test_ws)
|
130
157
|
rs.serialize.should == {
|
131
158
|
'name' => @foo.fancy_name,
|
132
159
|
'foo' => {
|
@@ -137,9 +164,9 @@ describe Restful::Serializer do
|
|
137
164
|
}
|
138
165
|
end
|
139
166
|
|
140
|
-
it "should
|
141
|
-
rs = Restful::Serializer.new(@foo,
|
142
|
-
:
|
167
|
+
it "should merge options during initialization" do
|
168
|
+
rs = Restful::Serializer.new(@foo, @test_ws,
|
169
|
+
:name_method => :name,
|
143
170
|
:serialization => { :only => [:id] },
|
144
171
|
:url_for => :custom_foo
|
145
172
|
)
|
@@ -147,21 +174,23 @@ describe Restful::Serializer do
|
|
147
174
|
'name' => @foo.name,
|
148
175
|
'foo' => {
|
149
176
|
'id' => @foo.id,
|
177
|
+
'name' => @foo.name,
|
178
|
+
'a_method' => @foo.a_method,
|
150
179
|
},
|
151
180
|
'href' => "http://test.org/custom_foo/#{@foo.id}",
|
152
181
|
}
|
153
182
|
end
|
154
183
|
|
155
|
-
it "should
|
156
|
-
rs = Restful::Serializer.new(@foo,
|
157
|
-
|
158
|
-
|
184
|
+
it "should provide fine grained configuration" do
|
185
|
+
rs = Restful::Serializer.new(@foo, @test_ws) do |config|
|
186
|
+
config.url_for = nil
|
187
|
+
config.serialization.only = :id
|
188
|
+
config.serialization.methods.clear
|
189
|
+
end
|
159
190
|
rs.serialize.should == {
|
160
191
|
'name' => @foo.fancy_name,
|
161
192
|
'foo' => {
|
162
193
|
'id' => @foo.id,
|
163
|
-
'name' => @foo.name,
|
164
|
-
'a_method' => @foo.a_method,
|
165
194
|
},
|
166
195
|
'href' => "http://test.org/prefix/foos/#{@foo.id}",
|
167
196
|
}
|
@@ -171,19 +200,22 @@ describe Restful::Serializer do
|
|
171
200
|
describe "with associations" do
|
172
201
|
|
173
202
|
before(:each) do
|
174
|
-
Restful.
|
175
|
-
:
|
176
|
-
:
|
177
|
-
|
178
|
-
|
179
|
-
:
|
180
|
-
|
181
|
-
|
203
|
+
Restful.register_web_service('test',
|
204
|
+
:resources => {
|
205
|
+
:foo => {
|
206
|
+
:associations => :bars,
|
207
|
+
},
|
208
|
+
:bar => {
|
209
|
+
:associations => { :special => :foo, :dingos => nil },
|
210
|
+
},
|
211
|
+
}
|
212
|
+
)
|
213
|
+
@test_ws = Restful.web_service_configuration('test')
|
182
214
|
generate_instances
|
183
215
|
end
|
184
216
|
|
185
217
|
it "should include references to associations" do
|
186
|
-
rs = Restful::Serializer.new(@foo)
|
218
|
+
rs = Restful::Serializer.new(@foo, @test_ws)
|
187
219
|
rs.serialize.should == {
|
188
220
|
'name' => @foo.name,
|
189
221
|
'foo' => {
|
@@ -196,7 +228,7 @@ describe Restful::Serializer do
|
|
196
228
|
end
|
197
229
|
|
198
230
|
it "should handle multiple and nested associations" do
|
199
|
-
rs = Restful::Serializer.new(@bar1)
|
231
|
+
rs = Restful::Serializer.new(@bar1, @test_ws)
|
200
232
|
rs.serialize.should == {
|
201
233
|
'name' => @bar1.name,
|
202
234
|
'bar' => {
|
@@ -214,17 +246,20 @@ describe Restful::Serializer do
|
|
214
246
|
describe "with subclasses" do
|
215
247
|
|
216
248
|
before(:each) do
|
217
|
-
Restful.
|
218
|
-
:
|
219
|
-
:
|
249
|
+
Restful.register_web_service('test',
|
250
|
+
:resources => {
|
251
|
+
:thing => {
|
252
|
+
:serialization => { :except => :secret }
|
253
|
+
}
|
220
254
|
}
|
221
|
-
|
255
|
+
)
|
256
|
+
@test_ws = Restful.web_service_configuration('test')
|
222
257
|
@thing = Thing.create!(:name => 'a thing', :secret => 'a secret')
|
223
258
|
@sub = Sub.create!(:name => 'a sub thing', :secret => 'another secret')
|
224
259
|
end
|
225
260
|
|
226
261
|
it "should pull superclass configuration up into a subclass serialization" do
|
227
|
-
rs = Restful::Serializer.new(@sub)
|
262
|
+
rs = Restful::Serializer.new(@sub, @test_ws)
|
228
263
|
rs.serialize.should == {
|
229
264
|
'name' => @sub.name,
|
230
265
|
'href' => "http://test.org/things/#{@sub.id}",
|
@@ -238,20 +273,23 @@ describe Restful::Serializer do
|
|
238
273
|
|
239
274
|
describe "with arrays" do
|
240
275
|
before(:each) do
|
241
|
-
Restful.
|
242
|
-
:
|
243
|
-
:
|
244
|
-
|
245
|
-
|
246
|
-
:
|
247
|
-
|
248
|
-
|
249
|
-
|
276
|
+
Restful.register_web_service('test',
|
277
|
+
:resources => {
|
278
|
+
:foo => {
|
279
|
+
:associations => :bars,
|
280
|
+
},
|
281
|
+
:bar => {
|
282
|
+
:serialization => { :only => [:name], :include => { :dingos => { :only => [ :name, :id] } } },
|
283
|
+
:associations => [:foo, :dingos],
|
284
|
+
},
|
285
|
+
}
|
286
|
+
)
|
287
|
+
@test_ws = Restful.web_service_configuration('test')
|
250
288
|
generate_instances
|
251
289
|
end
|
252
290
|
|
253
291
|
it "should serialize arrays" do
|
254
|
-
rs = Restful::Serializer.new(@foo.bars)
|
292
|
+
rs = Restful::Serializer.new(@foo.bars, @test_ws)
|
255
293
|
rs.serialize.should == [
|
256
294
|
{
|
257
295
|
'name' => @bar1.name,
|
@@ -271,7 +309,7 @@ describe Restful::Serializer do
|
|
271
309
|
end
|
272
310
|
|
273
311
|
it "should deeply serialize arrays if told to" do
|
274
|
-
rs = Restful::Serializer.new(@foo.bars, :shallow => false)
|
312
|
+
rs = Restful::Serializer.new(@foo.bars, @test_ws, :shallow => false)
|
275
313
|
rs.serialize.should == [
|
276
314
|
{
|
277
315
|
'name' => @bar1.name,
|
@@ -302,8 +340,8 @@ describe Restful::Serializer do
|
|
302
340
|
]
|
303
341
|
end
|
304
342
|
|
305
|
-
it "should
|
306
|
-
rs = Restful::Serializer.new(@foo.bars,
|
343
|
+
it "should merge options during initialization for each member" do
|
344
|
+
rs = Restful::Serializer.new(@foo.bars, @test_ws,
|
307
345
|
:serialization => { :only => :id }
|
308
346
|
)
|
309
347
|
rs.serialize.should == [
|
@@ -311,30 +349,31 @@ describe Restful::Serializer do
|
|
311
349
|
'name' => @bar1.name,
|
312
350
|
'href' => "http://test.org/bars/#{@bar1.id}",
|
313
351
|
'bar' => {
|
314
|
-
'
|
352
|
+
'name' => @bar1.name,
|
353
|
+
'id' => @bar1.id,
|
315
354
|
},
|
316
355
|
},
|
317
356
|
{
|
318
357
|
'name' => @bar2.name,
|
319
358
|
'href' => "http://test.org/bars/#{@bar2.id}",
|
320
359
|
'bar' => {
|
321
|
-
'
|
360
|
+
'name' => @bar2.name,
|
361
|
+
'id' => @bar2.id,
|
322
362
|
},
|
323
363
|
},
|
324
364
|
]
|
325
365
|
end
|
326
366
|
|
327
|
-
it "should
|
328
|
-
rs = Restful::Serializer.new(@foo.bars,
|
329
|
-
|
330
|
-
|
367
|
+
it "should provide fine grained configuration for each member" do
|
368
|
+
rs = Restful::Serializer.new(@foo.bars, @test_ws) do |configure|
|
369
|
+
configure.serialization.only = :id
|
370
|
+
end
|
331
371
|
rs.serialize.should == [
|
332
372
|
{
|
333
373
|
'name' => @bar1.name,
|
334
374
|
'href' => "http://test.org/bars/#{@bar1.id}",
|
335
375
|
'bar' => {
|
336
376
|
'id' => @bar1.id,
|
337
|
-
'name' => @bar1.name,
|
338
377
|
},
|
339
378
|
},
|
340
379
|
{
|
@@ -342,54 +381,92 @@ describe Restful::Serializer do
|
|
342
381
|
'href' => "http://test.org/bars/#{@bar2.id}",
|
343
382
|
'bar' => {
|
344
383
|
'id' => @bar2.id,
|
345
|
-
'name' => @bar2.name,
|
346
384
|
},
|
347
385
|
},
|
348
386
|
]
|
349
387
|
end
|
350
388
|
end
|
351
389
|
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
390
|
+
describe "extensions" do
|
391
|
+
|
392
|
+
before(:each) do
|
393
|
+
Restful.register_web_service('test',
|
394
|
+
:resources => {
|
395
|
+
:foo => {
|
396
|
+
:serialization => { :only => :name },
|
397
|
+
:associations => :bars,
|
398
|
+
},
|
399
|
+
:bar => {
|
400
|
+
:serialization => { :only => [:name], :include => { :dingos => { :only => [ :name, :id] } } },
|
401
|
+
:associations => [:foo, :dingos],
|
402
|
+
},
|
403
|
+
}
|
404
|
+
)
|
405
|
+
@test_ws = Restful.web_service_configuration('test')
|
406
|
+
generate_instances
|
407
|
+
end
|
408
|
+
|
409
|
+
it "should hook into activerecord" do
|
410
|
+
@foo.should respond_to(:restful)
|
411
|
+
result = @foo.restful('test', :serialization => { :only => [:id] })
|
412
|
+
result.should == {
|
358
413
|
'name' => @foo.name,
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
414
|
+
'foo' => {
|
415
|
+
'name' => @foo.name,
|
416
|
+
'id' => @foo.id,
|
417
|
+
},
|
418
|
+
'href' => "http://test.org/foos/#{@foo.id}",
|
419
|
+
'bars_href' => "http://test.org/bars",
|
420
|
+
}
|
421
|
+
# test default web service
|
422
|
+
@foo.restful(:serialization => {:only => :id}).should == result
|
423
|
+
end
|
424
|
+
|
425
|
+
it "should hook into associations" do
|
426
|
+
result = @foo.bars.restful('test') do |configure|
|
427
|
+
configure.serialization.only = []
|
428
|
+
end
|
363
429
|
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
{
|
368
|
-
"href" => "http://test.org/bars/#{@bar1.id}",
|
369
|
-
"name" => @bar1.name,
|
370
|
-
"bar" => {
|
430
|
+
result.should == [
|
431
|
+
{
|
432
|
+
"href" => "http://test.org/bars/#{@bar1.id}",
|
371
433
|
"name" => @bar1.name,
|
372
|
-
"
|
373
|
-
|
434
|
+
"bar" => {
|
435
|
+
"name" => @bar1.name,
|
436
|
+
"foo_id" => @bar1.foo_id,
|
437
|
+
"id" => @bar1.id,
|
438
|
+
},
|
374
439
|
},
|
375
|
-
|
376
|
-
|
377
|
-
"href" => "http://test.org/bars/#{@bar2.id}",
|
378
|
-
"name" => @bar2.name,
|
379
|
-
"bar" => {
|
440
|
+
{
|
441
|
+
"href" => "http://test.org/bars/#{@bar2.id}",
|
380
442
|
"name" => @bar2.name,
|
381
|
-
"
|
382
|
-
|
443
|
+
"bar" => {
|
444
|
+
"name" => @bar2.name,
|
445
|
+
"foo_id" => @bar2.foo_id,
|
446
|
+
"id" => @bar2.id,
|
447
|
+
},
|
383
448
|
},
|
384
|
-
|
385
|
-
|
449
|
+
]
|
450
|
+
|
451
|
+
@foo.bars.to_a.restful('test') do |configure|
|
452
|
+
configure.serialization.only = []
|
453
|
+
end.should == result
|
454
|
+
end
|
455
|
+
|
386
456
|
end
|
387
457
|
|
388
458
|
it "should work with AssociationProxy.respond_to" do
|
389
|
-
|
390
|
-
@foo.bars.should respond_to(:restful)
|
391
|
-
end
|
459
|
+
@foo.bars.should respond_to(:restful)
|
392
460
|
end
|
461
|
+
|
462
|
+
it "should extend ActiveRecord" do
|
463
|
+
@foo.should respond_to(:restful)
|
464
|
+
end
|
465
|
+
|
466
|
+
it "should extend Array" do
|
467
|
+
[].should respond_to(:restful)
|
468
|
+
end
|
469
|
+
|
393
470
|
end
|
394
471
|
|
395
472
|
describe Restful::DeepHash do
|
@@ -0,0 +1,66 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), 'spec_helper'))
|
2
|
+
|
3
|
+
describe Restful::Configuration::WebService do
|
4
|
+
|
5
|
+
after(:each) do
|
6
|
+
Restful.clear
|
7
|
+
end
|
8
|
+
|
9
|
+
describe "looking up resource configurations" do
|
10
|
+
|
11
|
+
class Super; end
|
12
|
+
class SubClass < Super; end
|
13
|
+
|
14
|
+
before(:each) do
|
15
|
+
@ws = Restful::Configuration::WebService.register('service') do |ws|
|
16
|
+
@super_config = ws.register_resource(:super)
|
17
|
+
@sub_class_config = ws.register_resource(:sub_class)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should return nil if no configuration" do
|
22
|
+
@ws.resource_configuration_for(nil).should be_nil
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should lookup entries by symbol" do
|
26
|
+
@ws.resource_configuration_for(:super).should == @super_config
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should lookup entries from a string" do
|
30
|
+
@ws.resource_configuration_for('super').should == @super_config
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should lookup entries by Class" do
|
34
|
+
@ws.resource_configuration_for(Super).should == @super_config
|
35
|
+
@ws.resource_configuration_for(SubClass).should == @sub_class_config
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should lookup entries by instance class" do
|
39
|
+
@ws.resource_configuration_for(Super.new).should == @super_config
|
40
|
+
@ws.resource_configuration_for(SubClass.new).should == @sub_class_config
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
describe "provided resource configurations" do
|
46
|
+
|
47
|
+
before(:each) do
|
48
|
+
@ws = Restful::Configuration::WebService.register('service') do |ws|
|
49
|
+
@gold_config = ws.register_resource(:super,
|
50
|
+
:serialization => { :only => :foo }
|
51
|
+
)
|
52
|
+
end
|
53
|
+
@config = @ws.resource_configuration_for(:super)
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should produce a clone" do
|
57
|
+
@config.should == @gold_config
|
58
|
+
@config.should_not equal(@gold_config)
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should produce a deep clone" do
|
62
|
+
@config.serialization.should == @gold_config.serialization
|
63
|
+
@config.serialization.should_not equal(@gold_config.serialization)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: restful_serializer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 19
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 4
|
10
|
+
version: 0.1.4
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Josh Partlow
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-
|
18
|
+
date: 2011-06-01 00:00:00 -07:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -137,14 +137,17 @@ files:
|
|
137
137
|
- README.rdoc
|
138
138
|
- Rakefile
|
139
139
|
- lib/restful.rb
|
140
|
+
- lib/restful/configuration.rb
|
140
141
|
- lib/restful/serializer.rb
|
141
142
|
- lib/restful/version.rb
|
142
143
|
- restful_serializer.gemspec
|
144
|
+
- spec/configuration_spec.rb
|
143
145
|
- spec/database.rb
|
144
146
|
- spec/restful_spec.rb
|
145
147
|
- spec/serializer_spec.rb
|
146
148
|
- spec/spec.opts
|
147
149
|
- spec/spec_helper.rb
|
150
|
+
- spec/web_service_spec.rb
|
148
151
|
has_rdoc: true
|
149
152
|
homepage: http://github.com/jpartlow/restful_serializer
|
150
153
|
licenses: []
|
@@ -183,8 +186,10 @@ signing_key:
|
|
183
186
|
specification_version: 3
|
184
187
|
summary: Helps with serializing activerecord instances as Restful resources.
|
185
188
|
test_files:
|
189
|
+
- spec/configuration_spec.rb
|
186
190
|
- spec/database.rb
|
187
191
|
- spec/restful_spec.rb
|
188
192
|
- spec/serializer_spec.rb
|
189
193
|
- spec/spec.opts
|
190
194
|
- spec/spec_helper.rb
|
195
|
+
- spec/web_service_spec.rb
|