jsonapi-serializers 0.2.5 → 0.2.6

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: 83d249f8286496af92b2caa669aede02fae2def8
4
- data.tar.gz: 660a8816d3b5fa6bb4c1d173cc7aceabca5c0550
3
+ metadata.gz: 7e698b9154b539960338d5350474f81c77aa0c1e
4
+ data.tar.gz: 08be38cb9c93ceb3812d05d0ce29e3d4cd318d32
5
5
  SHA512:
6
- metadata.gz: 1843eb6c3c067686497e28efc782328d1bbaf4571d25bc7fd87bf7bb46188119894db28f4d7072adaa5de3c823881a5b09699b3dcd5e7b5e6dd835f0882b7a9f
7
- data.tar.gz: 3de93fc69bd9a1350d7246054858c4b623c3b9a7cb84057ba2d322042b6b32937e275f01f8eeb79a5fc98e7a4b6ed72a5d18e6b870f80d90563af83d061ed537
6
+ metadata.gz: b2798644c46503bc5ea943cf245b855a132667ef2370219ae32b73be1629bdbb27154154b778d2c5528ae823ef96ce296e9f1e199e447e7ac5936b5ec01fca10
7
+ data.tar.gz: 8938cae3c0014f386449736e7ba7f5a703948ecb084814dcdafd2c6bc1ec925890d7296d5374845926a547f20f4825b9a5bddcb9ea0c0b8e69f92a36b6149331
data/README.md CHANGED
@@ -16,6 +16,7 @@ This library is up-to-date with the finalized v1 JSON API spec.
16
16
  * [Null handling](#null-handling)
17
17
  * [Custom attributes](#custom-attributes)
18
18
  * [More customizations](#more-customizations)
19
+ * [Base URL](#base-url)
19
20
  * [Relationships](#relationships)
20
21
  * [Compound documents and includes](#compound-documents-and-includes)
21
22
  * [Relationship path handling](#relationship-path-handling)
@@ -208,8 +209,14 @@ def meta
208
209
  end
209
210
  ```
210
211
  ```ruby
212
+ # Override this to set a base URL (http://example.com) for all links. No trailing slash.
213
+ def base_url
214
+ @base_url
215
+ end
216
+ ```
217
+ ```ruby
211
218
  def self_link
212
- "/#{type}/#{id}"
219
+ "#{base_url}/#{type}/#{id}"
213
220
  end
214
221
  ```
215
222
  ```ruby
@@ -225,6 +232,70 @@ end
225
232
 
226
233
  If you override `self_link`, `relationship_self_link`, or `relationship_related_link` to return `nil`, the link will be excluded from the serialized object.
227
234
 
235
+ ### Base URL
236
+
237
+ You can override the `base_url` instance method to set a URL to be used in all links.
238
+
239
+ ```ruby
240
+ class BaseSerializer
241
+ include JSONAPI::Serializer
242
+
243
+ def base_url
244
+ 'http://example.com'
245
+ end
246
+ end
247
+
248
+ class PostSerializer < BaseSerializer
249
+ attribute :title
250
+ attribute :content
251
+
252
+ has_one :author
253
+ has_many :comments
254
+ end
255
+
256
+ JSONAPI::Serializer.serialize(post)
257
+ ```
258
+
259
+ Returns:
260
+
261
+ ```json
262
+ {
263
+ "data": {
264
+ "id": "1",
265
+ "type": "posts",
266
+ "attributes": {
267
+ "title": "Hello World",
268
+ "content": "Your first post"
269
+ },
270
+ "links": {
271
+ "self": "http://example.com/posts/1"
272
+ },
273
+ "relationships": {
274
+ "author": {
275
+ "links": {
276
+ "self": "http://example.com/posts/1/relationships/author",
277
+ "related": "http://example.com/posts/1/author"
278
+ }
279
+ },
280
+ "comments": {
281
+ "links": {
282
+ "self": "http://example.com/posts/1/relationships/comments",
283
+ "related": "http://example.com/posts/1/comments"
284
+ },
285
+ }
286
+ }
287
+ }
288
+ }
289
+ ```
290
+
291
+ Alternatively, you can specify `base_url` as an argument to `serialize` which allows you to build the URL with different subdomains or other logic from the request:
292
+
293
+ ```ruby
294
+ JSONAPI::Serializer.serialize(post, base_url: 'http://example.com')
295
+ ```
296
+
297
+ Note: if you override `self_link` in your serializer and leave out `base_url`, it will not be included.
298
+
228
299
  ## Relationships
229
300
 
230
301
  You can easily specify relationships with the `has_one` and `has_many` directives.
@@ -439,6 +510,7 @@ end
439
510
 
440
511
  ## Release notes
441
512
 
513
+ * v0.2.6: Add `base_url` support.
442
514
  * v0.2.5: Allow disabling ambiguous collection checks for Sequel support.
443
515
  * v0.2.4: Improve handling for nil relationship links.
444
516
  * v0.2.3: Support serializers with no attributes.
@@ -24,10 +24,12 @@ module JSONAPI
24
24
  module InstanceMethods
25
25
  attr_accessor :object
26
26
  attr_accessor :context
27
+ attr_accessor :base_url
27
28
 
28
29
  def initialize(object, options = {})
29
30
  @object = object
30
31
  @context = options[:context] || {}
32
+ @base_url = options[:base_url]
31
33
 
32
34
  # Internal serializer options, not exposed through attr_accessor. No touchie.
33
35
  @_include_linkages = options[:include_linkages] || []
@@ -64,8 +66,13 @@ module JSONAPI
64
66
  def meta
65
67
  end
66
68
 
69
+ # Override this to set a base URL (http://example.com) for all links. No trailing slash.
70
+ def base_url
71
+ @base_url
72
+ end
73
+
67
74
  def self_link
68
- "/#{type}/#{id}"
75
+ "#{base_url}/#{type}/#{id}"
69
76
  end
70
77
 
71
78
  def relationship_self_link(attribute_name)
@@ -214,6 +221,7 @@ module JSONAPI
214
221
  options[:serializer] = options.delete('serializer') || options[:serializer]
215
222
  options[:context] = options.delete('context') || options[:context] || {}
216
223
  options[:skip_collection_check] = options.delete('skip_collection_check') || options[:skip_collection_check] || false
224
+ options[:base_url] = options.delete('base_url') || options[:base_url]
217
225
 
218
226
  # Normalize includes.
219
227
  includes = options[:include]
@@ -223,7 +231,8 @@ module JSONAPI
223
231
  passthrough_options = {
224
232
  context: options[:context],
225
233
  serializer: options[:serializer],
226
- include: includes
234
+ include: includes,
235
+ base_url: options[:base_url]
227
236
  }
228
237
 
229
238
  if !options[:skip_collection_check] && options[:is_collection] && !objects.respond_to?(:each)
@@ -280,10 +289,11 @@ module JSONAPI
280
289
  end
281
290
 
282
291
  result['included'] = relationship_data.map do |_, data|
283
- passthrough_options = {}
284
- passthrough_options[:serializer] = find_serializer_class(data[:object])
285
- passthrough_options[:include_linkages] = data[:include_linkages]
286
- serialize_primary(data[:object], passthrough_options)
292
+ included_passthrough_options = {}
293
+ included_passthrough_options[:base_url] = passthrough_options[:base_url]
294
+ included_passthrough_options[:serializer] = find_serializer_class(data[:object])
295
+ included_passthrough_options[:include_linkages] = data[:include_linkages]
296
+ serialize_primary(data[:object], included_passthrough_options)
287
297
  end
288
298
  end
289
299
  result
@@ -1,5 +1,5 @@
1
1
  module JSONAPI
2
2
  module Serializer
3
- VERSION = '0.2.5'
3
+ VERSION = '0.2.6'
4
4
  end
5
5
  end
@@ -705,4 +705,37 @@ describe JSONAPI::Serializer do
705
705
  xit 'is correctly passed through all serializers' do
706
706
  end
707
707
  end
708
+
709
+ describe 'base_url' do
710
+ it 'is empty by default' do
711
+ long_comments = create_list(:long_comment, 1)
712
+ post = create(:post, long_comments: long_comments)
713
+ data = JSONAPI::Serializer.serialize(post)
714
+ expect(data['data']['links']['self']).to eq('/posts/1')
715
+ expect(data['data']['relationships']['author']['links']).to eq({
716
+ 'self' => '/posts/1/relationships/author',
717
+ 'related' => '/posts/1/author'
718
+ })
719
+ end
720
+ it 'adds base_url to links if passed' do
721
+ long_comments = create_list(:long_comment, 1)
722
+ post = create(:post, long_comments: long_comments)
723
+ data = JSONAPI::Serializer.serialize(post, base_url: 'http://example.com')
724
+ expect(data['data']['links']['self']).to eq('http://example.com/posts/1')
725
+ expect(data['data']['relationships']['author']['links']).to eq({
726
+ 'self' => 'http://example.com/posts/1/relationships/author',
727
+ 'related' => 'http://example.com/posts/1/author'
728
+ })
729
+ end
730
+ it 'uses overriden base_url method if it exists' do
731
+ long_comments = create_list(:long_comment, 1)
732
+ post = create(:post, long_comments: long_comments)
733
+ data = JSONAPI::Serializer.serialize(post, serializer: MyApp::PostSerializerWithBaseUrl)
734
+ expect(data['data']['links']['self']).to eq('http://example.com/posts/1')
735
+ expect(data['data']['relationships']['author']['links']).to eq({
736
+ 'self' => 'http://example.com/posts/1/relationships/author',
737
+ 'related' => 'http://example.com/posts/1/author'
738
+ })
739
+ end
740
+ end
708
741
  end
@@ -115,6 +115,22 @@ module MyApp
115
115
  end
116
116
  end
117
117
 
118
+ class PostSerializerWithBaseUrl
119
+ include JSONAPI::Serializer
120
+
121
+ def base_url
122
+ 'http://example.com'
123
+ end
124
+
125
+ attribute :title
126
+ attribute :long_content do
127
+ object.body
128
+ end
129
+
130
+ has_one :author
131
+ has_many :long_comments
132
+ end
133
+
118
134
  class EmptySerializer
119
135
  include JSONAPI::Serializer
120
136
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonapi-serializers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.5
4
+ version: 0.2.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Fotinakis