README.md in jsonapi-serializers-0.2.5 vs README.md in jsonapi-serializers-0.2.6
- old
+ new
@@ -14,10 +14,11 @@
* [Serialize an object](#serialize-an-object)
* [Serialize a collection](#serialize-a-collection)
* [Null handling](#null-handling)
* [Custom attributes](#custom-attributes)
* [More customizations](#more-customizations)
+ * [Base URL](#base-url)
* [Relationships](#relationships)
* [Compound documents and includes](#compound-documents-and-includes)
* [Relationship path handling](#relationship-path-handling)
* [Rails example](#rails-example)
* [Unfinished business](#unfinished-business)
@@ -206,12 +207,18 @@
# http://jsonapi.org/format/#document-structure-resource-objects
def meta
end
```
```ruby
+# Override this to set a base URL (http://example.com) for all links. No trailing slash.
+def base_url
+ @base_url
+end
+```
+```ruby
def self_link
- "/#{type}/#{id}"
+ "#{base_url}/#{type}/#{id}"
end
```
```ruby
def relationship_self_link(attribute_name)
"#{self_link}/links/#{format_name(attribute_name)}"
@@ -223,10 +230,74 @@
end
```
If you override `self_link`, `relationship_self_link`, or `relationship_related_link` to return `nil`, the link will be excluded from the serialized object.
+### Base URL
+
+You can override the `base_url` instance method to set a URL to be used in all links.
+
+```ruby
+class BaseSerializer
+ include JSONAPI::Serializer
+
+ def base_url
+ 'http://example.com'
+ end
+end
+
+class PostSerializer < BaseSerializer
+ attribute :title
+ attribute :content
+
+ has_one :author
+ has_many :comments
+end
+
+JSONAPI::Serializer.serialize(post)
+```
+
+Returns:
+
+```json
+{
+ "data": {
+ "id": "1",
+ "type": "posts",
+ "attributes": {
+ "title": "Hello World",
+ "content": "Your first post"
+ },
+ "links": {
+ "self": "http://example.com/posts/1"
+ },
+ "relationships": {
+ "author": {
+ "links": {
+ "self": "http://example.com/posts/1/relationships/author",
+ "related": "http://example.com/posts/1/author"
+ }
+ },
+ "comments": {
+ "links": {
+ "self": "http://example.com/posts/1/relationships/comments",
+ "related": "http://example.com/posts/1/comments"
+ },
+ }
+ }
+ }
+}
+```
+
+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:
+
+```ruby
+JSONAPI::Serializer.serialize(post, base_url: 'http://example.com')
+```
+
+Note: if you override `self_link` in your serializer and leave out `base_url`, it will not be included.
+
## Relationships
You can easily specify relationships with the `has_one` and `has_many` directives.
```ruby
@@ -437,9 +508,10 @@
* Support for the `fields` spec is planned, would love a PR contribution for this.
* Support for pagination/sorting is unlikely to be supported because it would likely involve coupling to ActiveRecord, but please open an issue if you have ideas of how to support this generically.
## Release notes
+* v0.2.6: Add `base_url` support.
* v0.2.5: Allow disabling ambiguous collection checks for Sequel support.
* v0.2.4: Improve handling for nil relationship links.
* v0.2.3: Support serializers with no attributes.
* v0.2.2: Compliance fix for excluding empty relationship objects.
* v0.2.1: Compliance fix for self links.