simple-json-api-serializer 0.5.4 → 1.0.0

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: 31c823d362c1724ed551195ad91cee3a58aa45a5
4
- data.tar.gz: 6c9c9528d78dd58bd850415360a55881107eb501
3
+ metadata.gz: 73e1d5468c21f1ebba9494d32054c89f112d071b
4
+ data.tar.gz: abe7e22604cdffa704f3c87a52b8a462880854e6
5
5
  SHA512:
6
- metadata.gz: 7e66b11275359cd4d252a0e036e73e7a3c27ebcafad2198e5dc4a4ae840298e154564a549fb4c4c5cf51c6c922d31118eeb1266a39428a11154ba0b7e4bd40ba
7
- data.tar.gz: 985f81ac44f84ec7f61f971fbe291f986a68b2e6620fafb085450affb1bf5c52f74e4b6dd42595b19c72f0ce6c7a8c9fee5d874054a33a9e64c97cf6483b01db
6
+ metadata.gz: 75b165c7f5e8930893f1b059b665cc56cf89e99f7a3f06385b55b56429246daaf86666b0b5bb704adcf4397ac1c9331fe40700feb06afce28f4cf0b58570efd3
7
+ data.tar.gz: ccef0b3e809c4ec57b1ca1d75e246dd613d7637efc11f72c5541cd60ac097b407f2506ed0c2a028378c66eaea8c4e4d6df22989d7be9ab4695903141e535159c
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- simple-json-api-serializer (0.5.4)
4
+ simple-json-api-serializer (1.0.0)
5
5
  activesupport (~> 4.2)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -1,9 +1,8 @@
1
1
  # Simple JSONApi::Serializer
2
2
 
3
3
  * An extremely simple JSON Api serializer.
4
- * It supports serializing any Ruby object.
4
+ * It supports serializing any Ruby object.
5
5
  * It does not target a specific framework.
6
- * Does not (yet) support links.
7
6
 
8
7
  ## Installation
9
8
 
@@ -68,7 +67,7 @@ MyObjectSerializer.hashify(my_object)
68
67
  ```ruby
69
68
  class PersonSerializer < JSONApi::ObjectSerializerDefinition
70
69
  id_attribute :ssn
71
-
70
+
72
71
  attributes :first_name, :last_name
73
72
  end
74
73
 
@@ -98,7 +97,7 @@ Generates:
98
97
  ```ruby
99
98
  class PostSerializer < JSONApi::ObjectSerializerDefinition
100
99
  attributes :title, :content
101
-
100
+
102
101
  has_one :author
103
102
  has_many :comments
104
103
  end
@@ -107,7 +106,7 @@ Post = Struct.new(:id, :title, :content, :author, :comments) do
107
106
  def author_id
108
107
  author.id
109
108
  end
110
-
109
+
111
110
  def comment_ids
112
111
  comments.map(&:id)
113
112
  end
@@ -134,10 +133,9 @@ Generates:
134
133
  "data": { "type": "authors", "id": "42" },
135
134
  },
136
135
  "comments": {
137
- "data": [
138
- { "type": "comments", "id": "1" },
139
- { "type": "comments", "id": "2" }
140
- ]
136
+ "links": {
137
+ "related": "/posts/1/comments"
138
+ }
141
139
  }
142
140
  }
143
141
  }
@@ -146,9 +144,15 @@ Generates:
146
144
 
147
145
  If your object does not abide by the `_id` or `_ids` convention for relations,
148
146
  you can specify what method should be called to retrieve the foreign key with
149
- `has_one :author, foreign_key: :username`
147
+ `has_one :author, foreign_key: :username`
148
+
149
+ You can also specify the type of the related object with:
150
+ `has_one :author, type: :user`.
150
151
 
151
- You can also specify the type of the related object with: `has_one :author, type: :user`.
152
+ By default `has_one` relationships will include a `data` object but not `links`
153
+ and `has_many` relationships will include a `links` object but not `data`. This
154
+ can be customized by setting the `links` and `data` options to `true` or
155
+ `false`.
152
156
 
153
157
  #### Includes
154
158
 
@@ -2,7 +2,6 @@ require 'active_support/json'
2
2
  require 'json_api/utils'
3
3
  require 'json_api/relationship_serializer'
4
4
 
5
-
6
5
  module JSONApi
7
6
  class ObjectSerializer
8
7
  def serialize(object, **options)
@@ -40,6 +39,10 @@ module JSONApi
40
39
  result[:relationships] = relationships_for(object, options)
41
40
  end
42
41
 
42
+ if options[:links] == true
43
+ result[:links] = links_for(object, options)
44
+ end
45
+
43
46
  result
44
47
  end
45
48
 
@@ -57,9 +60,15 @@ module JSONApi
57
60
 
58
61
  def relationships_for(object, options)
59
62
  relationship_serializer = RelationshipSerializer.new
63
+
64
+ defaults = {
65
+ parent_type: type_for(object, options),
66
+ base_url: options[:base_url]
67
+ }
68
+
60
69
  options[:relationships].each_with_object({}) do |relationship, hash|
61
70
  relationship_key = Utils.canonicalize_attribute_name(relationship[:name])
62
- data = relationship_serializer.as_json(object, relationship)
71
+ data = relationship_serializer.as_json(object, defaults.merge(relationship))
63
72
  hash[relationship_key] = data unless data.nil?
64
73
  end
65
74
  end
@@ -71,6 +80,13 @@ module JSONApi
71
80
  end
72
81
  end
73
82
 
83
+ def links_for(object, **options)
84
+ id = id_for(object, options)
85
+ type = type_for(object, options)
86
+
87
+ { self: "#{options[:base_url] || ""}/#{type}/#{id}" }
88
+ end
89
+
74
90
  def id_for(object, **options)
75
91
  id_attribute = options[:id_attribute] || :id
76
92
  canonicalize_id(object.send(id_attribute))
@@ -17,6 +17,7 @@ module JSONApi
17
17
  specialization.attributes(*attributes)
18
18
  specialization.relationships(*relationships)
19
19
  specialization.id_attribute(id_attribute)
20
+ specialization.base_url(base_url)
20
21
  end
21
22
 
22
23
  def attributes(*attrs)
@@ -28,6 +29,10 @@ module JSONApi
28
29
  @id_attribute = attr
29
30
  end
30
31
 
32
+ def base_url(url = @base_url)
33
+ @base_url = url
34
+ end
35
+
31
36
  def relationship(name, **config)
32
37
  config[:name] = name
33
38
  relationships(config)
@@ -3,17 +3,20 @@ require 'json_api/utils'
3
3
  module JSONApi
4
4
  class RelationshipSerializer
5
5
  def as_json(object, **options)
6
- data =
7
- if options[:to] == :many
8
- ToManySerializer.new.data_for(object, options)
9
- else
10
- ToOneSerializer.new.data_for(object, options)
11
- end
12
-
13
- if data.nil? || data == []
6
+ serializer =
7
+ (options[:to] == :many ? ToManySerializer : ToOneSerializer).new
8
+
9
+ data = serializer.data_for(object, options)
10
+ links = serializer.links_for(object, options)
11
+
12
+ result = {}
13
+ result[:data] = data unless data.nil? || data.empty?
14
+ result[:links] = links unless links.nil? || links.empty?
15
+
16
+ if result.empty?
14
17
  nil
15
18
  else
16
- { data: data }
19
+ result
17
20
  end
18
21
  end
19
22
 
@@ -56,10 +59,21 @@ module JSONApi
56
59
  end
57
60
 
58
61
  def data_for(object, options)
62
+ return if options[:data] != true
63
+
59
64
  ids = relationship_for(object, options)
60
65
  ids.map { |id| resource_identifier_for(type_for(object, options), id) }
61
66
  .compact
62
67
  end
68
+
69
+ def links_for(object, options)
70
+ return if options[:links] == false
71
+
72
+ id = Utils.canonicalize_id(object.send(options[:id_attribute] || :id))
73
+ type = options[:type] || options[:name]
74
+
75
+ { related: "#{options[:base_url] || ""}/#{options[:parent_type]}/#{id}/#{type}" }
76
+ end
63
77
  end
64
78
 
65
79
  class ToOneSerializer < RelationshipSerializer
@@ -68,9 +82,20 @@ module JSONApi
68
82
  end
69
83
 
70
84
  def data_for(object, options)
85
+ return if options[:data] == false
86
+
71
87
  id = relationship_for(object, options)
72
88
  resource_identifier_for(type_for(object, options), id)
73
89
  end
90
+
91
+ def links_for(object, options)
92
+ return if options[:links] != true
93
+
94
+ id = Utils.canonicalize_id(object.send(options[:id_attribute] || :id))
95
+ type = (options[:type] || options[:name]).to_s.singularize
96
+
97
+ { related: "#{options[:base_url] || ""}/#{options[:parent_type]}/#{id}/#{type}" }
98
+ end
74
99
  end
75
100
  end
76
101
  end
@@ -1,3 +1,3 @@
1
1
  module JSONApi
2
- VERSION = '0.5.4'
2
+ VERSION = '1.0.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple-json-api-serializer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.4
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marten Schilstra
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-10-26 00:00:00.000000000 Z
11
+ date: 2015-11-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport