README.md in jsonapi-serializers-0.16.1 vs README.md in jsonapi-serializers-0.16.2
- old
+ new
@@ -191,20 +191,20 @@
```ruby
# Override this to customize the JSON:API "id" for this object.
# Always return a string from this method to conform with the JSON:API spec.
def id
- object.id.to_s
+ object.slug.to_s
end
```
```ruby
# Override this to customize the JSON:API "type" for this object.
# By default, the type is the object's class name lowercased, pluralized, and dasherized,
# per the spec naming recommendations: http://jsonapi.org/recommendations/#naming
# For example, 'MyApp::LongCommment' will become the 'long-comments' type.
def type
- object.class.name.demodulize.tableize.dasherize
+ 'long-comments'
end
```
```ruby
# Override this to customize how attribute names are formatted.
# By default, attribute names are dasherized per the spec naming recommendations:
@@ -697,13 +697,15 @@
ActionDispatch::ParamsParser::DEFAULT_PARSERS[Mime::Type.lookup(JSONAPI::MIMETYPE)] = lambda do |body|
JSON.parse(body)
end
# Rails 5 moved DEFAULT_PARSERS
-ActionDispatch::Http::Parameters::DEFAULT_PARSERS[Mime::Type.lookup(JSONAPI::MIMETYPE)] = lambda do |body|
+ActionDispatch::Http::Parameters::DEFAULT_PARSERS[:api_json] = lambda do |body|
JSON.parse(body)
end
+ActionDispatch::Request.parameter_parsers = ActionDispatch::Request::DEFAULT_PARSERS
+
```
## Sinatra example
Here's an example using [Sinatra](http://www.sinatrarb.com) and
@@ -721,11 +723,11 @@
to JSONAPI::Serializer to greatly reduce the number of queries performed.
```ruby
require 'sequel'
require 'sinatra/base'
-require 'json/ext'
+require 'json'
require 'jsonapi-serializers'
class Post < Sequel::Model
plugin :tactical_eager_loading
@@ -757,24 +759,25 @@
end
module Api
class V1 < Sinatra::Base
configure do
- mime_type :jsonapi, 'application/vnd.api+json'
+ mime_type :api_json, 'application/vnd.api+json'
set :database, Sequel.connect
end
helpers do
- # Parse the body of the request depending on its content-type:
def parse_request_body
- request.body.rewind
+ return unless request.body.respond_to?(:size) &&
+ request.body.size > 0
- case request.content_type && request.content_type[/^([^;]+)/]
- when /json$/, /javascript$/
- JSON.parse(request.body.read, symbolize_names: true)
- end
+ halt 415 unless request.content_type &&
+ request.content_type[/^[^;]+/] == mime_type(:api_json)
+
+ request.body.rewind
+ JSON.parse(request.body.read, symbolize_names: true)
end
# Convenience methods for serializing models:
def serialize_model(model, options = {})
options[:is_collection] = false
@@ -787,26 +790,30 @@
JSONAPI::Serializer.serialize(models, options)
end
end
before do
- @data = parse_request_body if request.body.size > 0
+ halt 406 unless request.preferred_type.entry == mime_type(:api_json)
+ @data = parse_request_body
+ content_type :api_json
end
- get '/posts', provides: :jsonapi do
+ get '/posts' do
posts = Post.all
- not_found if posts.empty?
serialize_models(posts).to_json
end
- get '/posts/:id', provides: :jsonapi do
+ get '/posts/:id' do
post = Post[params[:id].to_i]
not_found if post.nil?
serialize_model(post, include: 'comments').to_json
end
end
end
```
+
+See also: [Sinja](https://github.com/mwpastore/sinja), which extends Sinatra
+and leverages jsonapi-serializers to provide a JSON:API framework.
## Changelog
See [Releases](https://github.com/fotinakis/jsonapi-serializers/releases).