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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2dfadef3b0cdb67cc4f5a4b659448218f230bcba
4
- data.tar.gz: 5f83c011e2b01d6c539250e289661fde606d1d55
3
+ metadata.gz: 90d1438faf15f90e22381b152265374f8ad53543
4
+ data.tar.gz: 0fcbd12fbc3df348107fe579e5ea9e5103901529
5
5
  SHA512:
6
- metadata.gz: b13cf34cf4f9e0a4cda766e356d6fc69e249eb9944de45f3ad5e489e9fda2f9506b377540f9a798782aa5cf230ecdaa5f966df884040b98b62afe5790689266d
7
- data.tar.gz: 0d6c339987850df7d6a3e8dfe2a3e683eb2df3277878c0e98eaf613a0677f1da91eff1007bce939e9d8092d5ca976d9694143676209fd94f2406a682edc28492
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://goo.gl/rGoIQ)
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
- "artists": [
248
- {
249
- "id": "1",
250
- "name": "Radiohead",
251
- "website": "http://radiohead.com/",
252
- "href": "/artists/1"
253
- },
254
- {
255
- "id": "2",
256
- "name": "Nick Cave & The Bad Seeds",
257
- "website": "http://www.nickcave.com/",
258
- "href": "/artists/2"
259
- },
260
- {
261
- "id": "3",
262
- "name": "John Frusciante",
263
- "website": "http://johnfrusciante.com/",
264
- "href": "/artists/3"
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
- inject_to_many_links!
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 inject_to_many_links! #find and inject to_many links from related @resources
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
@@ -1,5 +1,5 @@
1
1
  module RestPack
2
2
  module Serializer
3
- VERSION = '0.4.10'
3
+ VERSION = '0.4.11'
4
4
  end
5
5
  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 to_many links' do
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: restpack_serializer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.10
4
+ version: 0.4.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gavin Joyce