json_api_ruby 0.0.1 → 0.0.2
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +2 -0
- data/lib/json_api_ruby/resources/base.rb +2 -2
- data/lib/json_api_ruby/serializer.rb +13 -14
- data/lib/json_api_ruby/version.rb +1 -1
- data/spec/json_api_ruby/serializer_spec.rb +22 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3b11dfa347ef77e800e676368dd1d05cf9178baf
|
4
|
+
data.tar.gz: 4eb50ca1efdbccdab32a6055a02c64c52ff3891c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 17ee19b48621c454bce815d03529c541242d90349b81dc95b57c3637d3275bf235386fd0fffc82ce6ba7b15ea7e1c6f3c896a241fb7905af5796584f941969ef
|
7
|
+
data.tar.gz: 44f16c8e5e6001217043148803964799fb829933dffb1d84580a6a658b0ef8ef57d061a60f288125576f45738858624b3e4f4ee5cc9dab431a85715d8e7b538e
|
data/CHANGELOG.md
ADDED
@@ -8,7 +8,7 @@ module JsonApi
|
|
8
8
|
options.symbolize_keys
|
9
9
|
|
10
10
|
resource_hash = identifier_hash
|
11
|
-
resource_hash['attributes'] = attributes_hash
|
11
|
+
resource_hash['attributes'] = attributes_hash if attributes_hash.any?
|
12
12
|
|
13
13
|
relationships.each do |relationship|
|
14
14
|
resource_hash['relationships'] ||= {}
|
@@ -40,7 +40,7 @@ module JsonApi
|
|
40
40
|
|
41
41
|
def attributes_hash
|
42
42
|
attrs = {}
|
43
|
-
self.class.fields.each do |attr|
|
43
|
+
Array(self.class.fields).each do |attr|
|
44
44
|
attrs[attr.to_s] = send(attr)
|
45
45
|
end
|
46
46
|
attrs
|
@@ -21,15 +21,15 @@ module JsonApi
|
|
21
21
|
|
22
22
|
class Serializer
|
23
23
|
def initialize(object, options)
|
24
|
-
@meta
|
25
|
-
@object
|
26
|
-
@includes
|
27
|
-
resource_name
|
28
|
-
@
|
24
|
+
@meta = options.fetch('meta', Hash.new).stringify_keys
|
25
|
+
@object = object
|
26
|
+
@includes = options.fetch('include', [])
|
27
|
+
resource_name = "#{@object.class.to_s.underscore}_resource".classify
|
28
|
+
@resource_class = options.fetch('resource_class', resource_name)
|
29
29
|
end
|
30
30
|
|
31
31
|
def to_hash
|
32
|
-
resource_klass = Resources::Discovery.resource_for_name(@object, resource_class: @
|
32
|
+
resource_klass = Resources::Discovery.resource_for_name(@object, resource_class: @resource_class)
|
33
33
|
resource = resource_klass.new(@object, include: @includes)
|
34
34
|
serialized = { 'data' => resource.to_hash }
|
35
35
|
relationships = resource.relationships
|
@@ -56,10 +56,10 @@ module JsonApi
|
|
56
56
|
|
57
57
|
class CollectionSerializer
|
58
58
|
def initialize(objects, options = {})
|
59
|
-
@meta
|
60
|
-
@objects
|
61
|
-
@includes
|
62
|
-
@
|
59
|
+
@meta = options.fetch('meta', Hash.new).stringify_keys
|
60
|
+
@objects = objects
|
61
|
+
@includes = options.fetch('include', [])
|
62
|
+
@resource_class = options.fetch('resource_class', nil)
|
63
63
|
end
|
64
64
|
|
65
65
|
def to_hash
|
@@ -67,10 +67,10 @@ module JsonApi
|
|
67
67
|
included_data = []
|
68
68
|
|
69
69
|
data_array = @objects.map do |object|
|
70
|
-
resource_name
|
71
|
-
klass_name
|
70
|
+
resource_name = "#{object.class.to_s.underscore}_resource".classify
|
71
|
+
klass_name = @resource_class || resource_name
|
72
72
|
resource_klass = Resources::Discovery.resource_for_name(object, resource_class: klass_name)
|
73
|
-
resource
|
73
|
+
resource = resource_klass.new(object, include: @includes)
|
74
74
|
included_data += assemble_included_data(resource.relationships)
|
75
75
|
resource.to_hash
|
76
76
|
end
|
@@ -97,4 +97,3 @@ module JsonApi
|
|
97
97
|
end
|
98
98
|
end
|
99
99
|
end
|
100
|
-
|
@@ -10,6 +10,10 @@ RSpec.describe JsonApi::Serializer do
|
|
10
10
|
JsonApi.serialize(person, meta: { meta_key: 'meta value' })
|
11
11
|
end
|
12
12
|
|
13
|
+
subject(:serialized_data_other_resource_class) do
|
14
|
+
JsonApi.serialize(person, resource_class: 'Namespace::OneResource')
|
15
|
+
end
|
16
|
+
|
13
17
|
it 'has a top level data object' do
|
14
18
|
expect(serialized_data).to have_data
|
15
19
|
end
|
@@ -18,6 +22,12 @@ RSpec.describe JsonApi::Serializer do
|
|
18
22
|
expect(serialized_data).to have_meta
|
19
23
|
end
|
20
24
|
|
25
|
+
it 'serializes the data using the passed-in resource class' do
|
26
|
+
expected_keys = %w(id type links)
|
27
|
+
expect(serialized_data_other_resource_class['data']).to be_valid_json_api
|
28
|
+
expect(serialized_data_other_resource_class['data'].keys).to eql(expected_keys)
|
29
|
+
end
|
30
|
+
|
21
31
|
context 'with included resources' do
|
22
32
|
subject(:serialized) do
|
23
33
|
JsonApi.serialize(person, meta: { meta_key: 'meta value' }, include: [:articles])
|
@@ -44,6 +54,18 @@ RSpec.describe JsonApi::Serializer do
|
|
44
54
|
]
|
45
55
|
end
|
46
56
|
|
57
|
+
subject(:serialized_collection_other_resource_class) do
|
58
|
+
JsonApi.serialize(people, resource_class: 'Namespace::OneResource')
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'serializes the data using the passed-in resource class' do
|
62
|
+
serialized_collection_other_resource_class['data'].each do |serialized|
|
63
|
+
expected_keys = %w(id type links)
|
64
|
+
expect(serialized).to be_valid_json_api
|
65
|
+
expect(serialized.keys).to eql(expected_keys)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
47
69
|
context 'without included resources' do
|
48
70
|
subject(:serialized_resources) do
|
49
71
|
JsonApi.serialize(people, meta: {'I' => 'have meta'})
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json_api_ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tracey Eubanks
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-01-
|
11
|
+
date: 2016-01-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -103,6 +103,7 @@ extra_rdoc_files: []
|
|
103
103
|
files:
|
104
104
|
- ".gitignore"
|
105
105
|
- ".rspec"
|
106
|
+
- CHANGELOG.md
|
106
107
|
- Gemfile
|
107
108
|
- Gemfile.lock
|
108
109
|
- Guardfile
|