restpack_serializer 0.4.10 → 0.4.11
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE +1 -1
- data/README.md +23 -21
- data/lib/restpack_serializer/result.rb +6 -5
- data/lib/restpack_serializer/version.rb +1 -1
- data/spec/result_spec.rb +21 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 90d1438faf15f90e22381b152265374f8ad53543
|
4
|
+
data.tar.gz: 0fcbd12fbc3df348107fe579e5ea9e5103901529
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0d4d76207155ad77ae9547c4f2439c9c6d5a7c8b9c5463aabf1d93e4d21d567c9e51169536a18f5619d407919b61c867bd754829e9095eea81dc2f67b166df04
|
7
|
+
data.tar.gz: 2900f4a1a0bbfe320d5daf0e5194a72d65f917a015b159253a8a5004007a52b1470fed900e9ba9abc9ea23bfce6b1ad69a934c5633b764893f35df6720ee75be
|
data/LICENSE
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
Copyright (c) 2013 Gavin Joyce
|
1
|
+
Copyright (c) 2013-2014 Gavin Joyce
|
2
2
|
Copyright (c) 2011-2012 José Valim & Yehuda Katz (https://github.com/rails-api/active_model_serializers/blob/master/MIT-LICENSE.txt)
|
3
3
|
|
4
4
|
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
data/README.md
CHANGED
@@ -9,7 +9,7 @@ restpack_serializer allows you to quickly provide a set of RESTful endpoints for
|
|
9
9
|
|
10
10
|
---
|
11
11
|
|
12
|
-
* [An overview of RestPack](http://
|
12
|
+
* [An overview of RestPack](http://www.slideshare.net/gavinjoyce/taming-monolithic-monsters)
|
13
13
|
* [JSON API](http://jsonapi.org/)
|
14
14
|
|
15
15
|
## Serialization
|
@@ -244,26 +244,28 @@ which yields:
|
|
244
244
|
"type": "songs"
|
245
245
|
}
|
246
246
|
},
|
247
|
-
"
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
247
|
+
"linked": {
|
248
|
+
"artists": [
|
249
|
+
{
|
250
|
+
"id": "1",
|
251
|
+
"name": "Radiohead",
|
252
|
+
"website": "http://radiohead.com/",
|
253
|
+
"href": "/artists/1"
|
254
|
+
},
|
255
|
+
{
|
256
|
+
"id": "2",
|
257
|
+
"name": "Nick Cave & The Bad Seeds",
|
258
|
+
"website": "http://www.nickcave.com/",
|
259
|
+
"href": "/artists/2"
|
260
|
+
},
|
261
|
+
{
|
262
|
+
"id": "3",
|
263
|
+
"name": "John Frusciante",
|
264
|
+
"website": "http://johnfrusciante.com/",
|
265
|
+
"href": "/artists/3"
|
266
|
+
}
|
267
|
+
]
|
268
|
+
}
|
267
269
|
}
|
268
270
|
```
|
269
271
|
|
@@ -11,23 +11,23 @@ module RestPack::Serializer
|
|
11
11
|
def serialize
|
12
12
|
result = {}
|
13
13
|
|
14
|
-
result[:meta] = @meta unless @meta.empty?
|
15
|
-
result[:links] = @links unless @links.empty?
|
16
|
-
|
17
14
|
unless @resources.empty?
|
18
|
-
|
15
|
+
inject_has_many_links!
|
19
16
|
result[@resources.keys.first] = @resources.values.first
|
20
17
|
|
21
18
|
linked = @resources.except(@resources.keys.first)
|
22
19
|
result[:linked] = linked unless linked.empty?
|
23
20
|
end
|
24
21
|
|
22
|
+
result[:links] = @links unless @links.empty?
|
23
|
+
result[:meta] = @meta unless @meta.empty?
|
24
|
+
|
25
25
|
result
|
26
26
|
end
|
27
27
|
|
28
28
|
private
|
29
29
|
|
30
|
-
def
|
30
|
+
def inject_has_many_links!
|
31
31
|
@resources.keys.each do |key|
|
32
32
|
@resources[key].each do |item|
|
33
33
|
if item[:links]
|
@@ -41,6 +41,7 @@ module RestPack::Serializer
|
|
41
41
|
linked_resource[:links] ||= {}
|
42
42
|
linked_resource[:links][key] ||= []
|
43
43
|
linked_resource[:links][key] << item[:id]
|
44
|
+
linked_resource[:links][key].uniq!
|
44
45
|
end
|
45
46
|
end
|
46
47
|
end
|
data/spec/result_spec.rb
CHANGED
@@ -36,15 +36,35 @@ describe RestPack::Serializer::Result do
|
|
36
36
|
before do
|
37
37
|
subject.resources[:albums] = [{ id: '1', name: 'AMOK'}]
|
38
38
|
subject.resources[:songs] = [{ id: '91', name: 'Before Your Very Eyes...', links: { album: '1' }}]
|
39
|
+
subject.meta[:albums] = { count: 1 }
|
40
|
+
subject.meta[:songs] = { count: 1 }
|
39
41
|
subject.links['albums.songs'] = { type: 'songs', href: '/api/v1/songs?album_id={albums.id}' }
|
40
42
|
subject.links['songs.album'] = { type: 'albums', href: '/api/v1/albums/{songs.album}' }
|
41
43
|
end
|
42
44
|
|
43
|
-
it 'returns correct jsonapi.org format, including injected
|
45
|
+
it 'returns correct jsonapi.org format, including injected has_many links' do
|
44
46
|
result[:albums].should == [{ id: '1', name: 'AMOK', links: { songs: ['91'] } }]
|
45
47
|
result[:links].should == subject.links
|
46
48
|
result[:linked][:songs].should == subject.resources[:songs]
|
47
49
|
end
|
50
|
+
|
51
|
+
it 'includes resources in correct order' do
|
52
|
+
result.keys[0].should == :albums
|
53
|
+
result.keys[1].should == :linked
|
54
|
+
result.keys[2].should == :links
|
55
|
+
result.keys[3].should == :meta
|
56
|
+
end
|
57
|
+
|
58
|
+
context 'with multiple calls to serialize' do
|
59
|
+
let(:result) do
|
60
|
+
subject.serialize
|
61
|
+
subject.serialize
|
62
|
+
end
|
63
|
+
|
64
|
+
it 'does not create duplicate has_many links' do
|
65
|
+
result[:albums].first[:links][:songs].count.should == 1
|
66
|
+
end
|
67
|
+
end
|
48
68
|
end
|
49
69
|
end
|
50
70
|
end
|