json_api_ruby 0.1.0 → 0.1.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 91293b5565faf0ff2b9cbd1260c8e2da3a7b774b
4
- data.tar.gz: e21ee8f390cd1fc36f0abea84201c240685b7f3a
3
+ metadata.gz: 6cf3164f2abf635e618957c75eea9210e2964c3e
4
+ data.tar.gz: f1927eafda56413ca744cc43ac5608344db1af55
5
5
  SHA512:
6
- metadata.gz: 84c0dcd1ff3fec00ff7708a2db101448dad9b10abdba31dc0fbdecfc112a5ca2a9484ecaacb4636c384ec39b4acdad956330a1027d255cc11bb389d3c80c9e71
7
- data.tar.gz: 05fe188da17d76795ce11e90074b21aa0e0046d9d359b82b1cfadf12f5907ee5669680aac1056f261a1c4ccae15ca72d18da1b5d9052c436cb4db16b286433f4
6
+ metadata.gz: 6af9abffd2f8321a35ef837a92bcfc8da35bec3685cfda916b0559db0c152bc3e00d80a39f34a5a381736a48af28f9fa15a409e0f85052c6dd30e1dd65c44aaf
7
+ data.tar.gz: 9e1a89a1741949c212c6833df0e65aa266bfc17630eca272b56f2690a4477e655d68f36442f8d370fc2a1bd7be12cc614f1bdfe3035f1547e0b9e0ad2fd6bd7b
data/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ 0.1.1
2
+ Fix issues related to empty or nil objects
3
+
4
+ 0.1.0
5
+ Added the ability to set a `resource_class` on relationships
6
+ Made resource discovery able to handle a string or a class object
7
+
1
8
  0.0.3
2
9
  Performance tuning and documentation
3
10
 
data/README.md CHANGED
@@ -6,4 +6,3 @@ See the <a href="https://github.com/teubanks/jsonapi_ruby/wiki">Wiki</a> page or
6
6
 
7
7
  ### Status
8
8
  Definitely a work in progress.
9
- This is currently an Alpha release, use at your own peril
@@ -11,7 +11,7 @@ module JsonApi
11
11
  def serialize(object, options = {})
12
12
  options.stringify_keys!
13
13
  # assume it's a collection
14
- if object.present? && object.respond_to?(:to_a)
14
+ if !object.nil? && object.respond_to?(:to_a)
15
15
  serializer = CollectionSerializer.new(object, options)
16
16
  else
17
17
  serializer = Serializer.new(object, options)
@@ -29,17 +29,21 @@ module JsonApi
29
29
  end
30
30
 
31
31
  def to_hash
32
- resource_klass = Resources::Discovery.resource_for_name(@object, resource_class: @resource_class)
33
- resource = resource_klass.new(@object, include: @includes)
34
- serialized = { 'data' => resource.to_hash }
35
- relationships = resource.relationships
36
- included_data = assemble_included_data(relationships)
37
-
38
- if included_data.present?
39
- included_data.uniq! do |inc_data|
40
- inc_data['id'] + inc_data['type']
32
+ if @object.nil?
33
+ serialized = { 'data' => nil }
34
+ else
35
+ resource_klass = Resources::Discovery.resource_for_name(@object, resource_class: @resource_class)
36
+ resource = resource_klass.new(@object, include: @includes)
37
+ serialized = { 'data' => resource.to_hash }
38
+ relationships = resource.relationships
39
+ included_data = assemble_included_data(relationships)
40
+
41
+ if included_data.present?
42
+ included_data.uniq! do |inc_data|
43
+ inc_data['id'] + inc_data['type']
44
+ end
45
+ serialized['included'] = included_data
41
46
  end
42
- serialized['included'] = included_data
43
47
  end
44
48
 
45
49
  serialized['meta'] = @meta if @meta.present?
@@ -82,8 +86,8 @@ module JsonApi
82
86
 
83
87
  serialized['data'] = data_array
84
88
 
85
- serialized['meta'] = @meta if @meta
86
- serialized['included'] = assemble_included_data(included_resources)
89
+ serialized['meta'] = @meta if @meta.present?
90
+ serialized['included'] = assemble_included_data(included_resources) if included_resources.present?
87
91
 
88
92
  serialized
89
93
  end
@@ -1,3 +1,3 @@
1
1
  module JsonApi
2
- VERSION = '0.1.0'
2
+ VERSION = '0.1.1'
3
3
  end
@@ -14,6 +14,10 @@ RSpec.describe JsonApi::Serializer do
14
14
  JsonApi.serialize(person, resource_class: 'Namespace::OneResource')
15
15
  end
16
16
 
17
+ it 'returns a nil data object for no objects' do
18
+ expect(JsonApi.serialize(nil)).to eq({'data' => nil})
19
+ end
20
+
17
21
  it 'has a top level data object' do
18
22
  expect(serialized_data).to have_data
19
23
  end
@@ -58,6 +62,10 @@ RSpec.describe JsonApi::Serializer do
58
62
  JsonApi.serialize(people, resource_class: 'Namespace::OneResource')
59
63
  end
60
64
 
65
+ it 'returns an empty data array for no objects' do
66
+ expect(JsonApi.serialize([])).to eq({'data' => []})
67
+ end
68
+
61
69
  it 'serializes the data using the passed-in resource class' do
62
70
  serialized_collection_other_resource_class['data'].each do |serialized|
63
71
  expected_keys = %w(id type links)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_api_ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tracey Eubanks