Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions rails5/en/chapter00-before.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@ A big "thank you" to all Github contributors who make this book alive. In alphab
* https://github.com/cuilei5205189[cuilei5205189]
* https://github.com/franklinjosmell[franklinjosmell]
* https://github.com/notapatch[notapatch]
* https://github.com/promisepreston[promisepreston]
* https://github.com/tacataca[tacataca]
1 change: 1 addition & 0 deletions rails5/fr/chapter00-before.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,5 @@ Un grand merci à tous les contributeurs de Github qui rendent ce livre vivant.
* https://github.com/cuilei5205189[cuilei5205189]
* https://github.com/franklinjosmell[franklinjosmell]
* https://github.com/notapatch[notapatch]
* https://github.com/promisepreston[promisepreston]
* https://github.com/tacataca[tacataca]
1 change: 1 addition & 0 deletions rails6/en/chapter00-before.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,5 @@ A big "thank you" to all Github contributors who keep this book alive. In alphab
* https://github.com/cuilei5205189[cuilei5205189]
* https://github.com/franklinjosmell[franklinjosmell]
* https://github.com/notapatch[notapatch]
* https://github.com/promisepreston[promisepreston]
* https://github.com/tacataca[tacataca]
38 changes: 19 additions & 19 deletions rails6/en/chapter06-improve-json.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,20 @@ The content of the `data` key is also quite strict:
* properties of the objects must be placed in an `attributes` key
* links of the objects must be placed in a `relationships` key

In this chapter we will customize the JSON output using Netflix's gem: https://github.com/Netflix/fast_jsonapi[fast_jsonapi]. Luckily for us, they already implement all https://jsonapi.org/[JSON:API] specifications.
In this chapter we will customize the JSON output using https://github.com/jsonapi-serializer/jsonapi-serializer[jsonapi-serializer] gem (fork of Netflix's https://github.com/Netflix/fast_jsonapi[fast_jsonapi] gem) . Luckily for us, it already implements all https://jsonapi.org/[JSON:API] specifications.

So let's install the gem `fast_jsonapi`:
So let's install the gem `jsonapi-serializer`:

[source,bash]
----
$ bundle add fast_jsonapi
$ bundle add jsonapi-serializer
----

You should be ready to continue with this tutorial.

== Serialize user

FastJSON API uses *serializers*. Serializers represent Ruby classes that will be responsible to converting a model into an https://ruby-doc.org/core-2.6.3/Hash.html[`Hash`] or a JSON.
JSON:API Serializer uses *serializers*. Serializers represent Ruby classes that will be responsible to converting a model into an https://ruby-doc.org/core-2.6.3/Hash.html[`Hash`] or a JSON.

So we need to add a `user_serializer.rb` file. We can do it manually but the gem provides a command-line interface to do it:

Expand All @@ -87,7 +87,7 @@ This has created a file called `user_serializer.rb` under the `app/serializers`
.app/serializers/user_serializer.rb
----
class UserSerializer
include FastJsonapi::ObjectSerializer
include JSONAPI::Serializer
attributes :email
end
----
Expand All @@ -111,12 +111,12 @@ There you go. As you can see this is easy. Now we can use our new _serializer_ i
class Api::V1::UsersController < ApplicationController
# ...
def show
render json: UserSerializer.new(@user).serializable_hash
render json: UserSerializer.new(@user).serializable_hash.to_json
end

def update
if @user.update(user_params)
render json: UserSerializer.new(@user).serializable_hash
render json: UserSerializer.new(@user).serializable_hash.to_json
else
# ...
end
Expand All @@ -125,7 +125,7 @@ class Api::V1::UsersController < ApplicationController
def create
# ...
if @user.save
render json: UserSerializer.new(@user).serializable_hash, status: :created
render json: UserSerializer.new(@user).serializable_hash.to_json, status: :created
else
# ...
end
Expand Down Expand Up @@ -194,7 +194,7 @@ Now let's add attributes to serialize the product:
.app/serializers/product_serializer.rb
----
class ProductSerializer
include FastJsonapi::ObjectSerializer
include JSONAPI::Serializer
attributes :title, :price, :published
end
----
Expand All @@ -208,25 +208,25 @@ class Api::V1::ProductsController < ApplicationController
# ...
def index
@products = Product.all
render json: ProductSerializer.new(@products).serializable_hash
render json: ProductSerializer.new(@products).serializable_hash.to_json
end

def show
render json: ProductSerializer.new(@product).serializable_hash
render json: ProductSerializer.new(@product).serializable_hash.to_json
end

def create
product = current_user.products.build(product_params)
if product.save
render json: ProductSerializer.new(product).serializable_hash, status: :created
render json: ProductSerializer.new(product).serializable_hash.to_json, status: :created
else
# ...
end
end

def update
if @product.update(product_params)
render json: ProductSerializer.new(@product).serializable_hash
render json: ProductSerializer.new(@product).serializable_hash.to_json
else
# ...
end
Expand Down Expand Up @@ -493,7 +493,7 @@ To pass this test we will start by including the relationship in the _serializer
.app/serializers/product_serializer.rb
----
class ProductSerializer
include FastJsonapi::ObjectSerializer
include JSONAPI::Serializer
attributes :title, :price, :published
belongs_to :user
end
Expand Down Expand Up @@ -533,7 +533,7 @@ class Api::V1::ProductsController < ApplicationController
# ...
def show
options = { include: [:user] }
render json: ProductSerializer.new(@product, options).serializable_hash
render json: ProductSerializer.new(@product, options).serializable_hash.to_json
end
# ...
end
Expand Down Expand Up @@ -607,7 +607,7 @@ _serializer_:
.app/serializers/user_serializer.rb
----
class UserSerializer
include FastJsonapi::ObjectSerializer
include JSONAPI::Serializer
attributes :email
has_many :products
end
Expand All @@ -622,7 +622,7 @@ class Api::V1::UsersController < ApplicationController
# ...
def show
options = { include: [:products] }
render json: UserSerializer.new(@user, options).serializable_hash
render json: UserSerializer.new(@user, options).serializable_hash.to_json
end
# ...
end
Expand Down Expand Up @@ -948,7 +948,7 @@ class Api::V1::ProductsController < ApplicationController
# ...
def index
@products = Product.search(params)
render json: ProductSerializer.new(@products).serializable_hash
render json: ProductSerializer.new(@products).serializable_hash.to_json
end
# ...
end
Expand Down Expand Up @@ -980,4 +980,4 @@ $ git merge chapter06

== Conclusion

Until now it was easy thanks to the gem https://github.com/Netflix/fast_jsonapi_jsonapi[fast_jsonapi]. In the coming chapters, we will start building the `Order` model that will involve users in the products.
Until now it was easy thanks to the gem https://github.com/jsonapi-serializer/jsonapi-serializer[jsonapi-serializer]. In the coming chapters, we will start building the `Order` model that will involve users in the products.
6 changes: 3 additions & 3 deletions rails6/en/chapter07-placing-orders.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ And let's add relationships:
[source,ruby]
----
class OrderSerializer
include FastJsonapi::ObjectSerializer
include JSONAPI::Serializer
belongs_to :user
has_many :products
end
Expand All @@ -261,7 +261,7 @@ class Api::V1::OrdersController < ApplicationController
before_action :check_login, only: %i[index]

def index
render json: OrderSerializer.new(current_user.orders).serializable_hash
render json: OrderSerializer.new(current_user.orders).serializable_hash.to_json
end
end
----
Expand Down Expand Up @@ -335,7 +335,7 @@ class Api::V1::OrdersController < ApplicationController

if order
options = { include: [:products] }
render json: OrderSerializer.new(order, options).serializable_hash
render json: OrderSerializer.new(order, options).serializable_hash.to_json
else
head 404
end
Expand Down
22 changes: 11 additions & 11 deletions rails6/en/chapter09-optimization.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class Api::V1::ProductsController < ApplicationController
.per(params[:per_page])
.search(params)

render json: ProductSerializer.new(@products).serializable_hash
render json: ProductSerializer.new(@products).serializable_hash.to_json
end
# ...
end
Expand Down Expand Up @@ -163,7 +163,7 @@ class Api::V1::ProductsController < ApplicationController
}
}

render json: ProductSerializer.new(@products, options).serializable_hash
render json: ProductSerializer.new(@products, options).serializable_hash.to_json
end
end
----
Expand Down Expand Up @@ -249,7 +249,7 @@ class Api::V1::OrdersController < ApplicationController
}
}

render json: OrderSerializer.new(@orders, options).serializable_hash
render json: OrderSerializer.new(@orders, options).serializable_hash.to_json
end
# ...
end
Expand Down Expand Up @@ -383,7 +383,7 @@ class Api::V1::OrdersController < ApplicationController

options = get_links_serializer_options('api_v1_orders_path', @orders)

render json: OrderSerializer.new(@orders, options).serializable_hash
render json: OrderSerializer.new(@orders, options).serializable_hash.to_json
end
# ...
end
Expand All @@ -403,7 +403,7 @@ class Api::V1::ProductsController < ApplicationController

options = get_links_serializer_options('api_v1_products_path', @products)

render json: ProductSerializer.new(@products, options).serializable_hash
render json: ProductSerializer.new(@products, options).serializable_hash.to_json
end
# ...
end
Expand All @@ -427,7 +427,7 @@ $ git commit -am "Factorize pagination"

== API Caching

There is currently an implementation to do caching with the gem `fast_jsonapi` which is really easy to handle. Although in older versions of the gem, this implementation can change, it does the job.
There is currently an implementation to do caching with the gem `jsonapi-serializer` which is really easy to handle. Although in older versions of the gem, this implementation can change, it does the job.

If we request the product list, we will notice that the response time takes about 174 milliseconds using cURL:

Expand All @@ -446,7 +446,7 @@ By adding only one line to the `ProductSerializer` class, we will see a signific
----
class OrderSerializer
# ...
cache_options enabled: true, cache_length: 12.hours
cache_options store: Rails.cache, namespace: 'jsonapi-serializer', expires_in: 1.hour
end
----

Expand All @@ -455,7 +455,7 @@ end
----
class ProductSerializer
# ...
cache_options enabled: true, cache_length: 12.hours
cache_options store: Rails.cache, namespace: 'jsonapi-serializer', expires_in: 1.hour
end
----

Expand All @@ -464,7 +464,7 @@ end
----
class UserSerializer
# ...
cache_options enabled: true, cache_length: 12.hours
cache_options store: Rails.cache, namespace: 'jsonapi-serializer', expires_in: 1.hour
end
----

Expand Down Expand Up @@ -538,7 +538,7 @@ class Api::V1::ProductsController < ApplicationController
options = get_links_serializer_options('api_v1_products_path', @products)
options[:include] = [:user]

render json: ProductSerializer.new(@products, options).serializable_hash
render json: ProductSerializer.new(@products, options).serializable_hash.to_json
end
# ...
end
Expand Down Expand Up @@ -642,7 +642,7 @@ class Api::V1::ProductsController < ApplicationController
options = get_links_serializer_options('api_v1_products_path', @products)
options[:include] = [:user]

render json: ProductSerializer.new(@products, options).serializable_hash
render json: ProductSerializer.new(@products, options).serializable_hash.to_json
end
# ...
end
Expand Down
3 changes: 2 additions & 1 deletion rails6/es/chapter00-before.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Todo el código fuente de este libro está en formato https://asciidoctor.org/[A

Este libro está bajo la http://opensource.org/licenses/MIT[licencia MIT]. Todo el código fuente del libro está en el formato https://fr.wikipedia.org/wiki/Markdown[Markdown] disponible en https://github.com/madeindjs/api_on_rails[GitHub]

.Licencia MIT
.Licencia MIT
****
Copyright 2019 Alexandre Rousseau

Expand All @@ -43,4 +43,5 @@ Un gran "gracias" a todos los contribuidores de GitHub quienes mantienen este li
* https://github.com/cuilei5205189[cuilei5205189]
* https://github.com/franklinjosmell[franklinjosmell]
* https://github.com/notapatch[notapatch]
* https://github.com/promisepreston[promisepreston]
* https://github.com/tacataca[tacataca]
1 change: 1 addition & 0 deletions rails6/fr/chapter00-before.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@ Un grand merci à tous les contributeurs de Github qui rendent ce livre vivant.
* https://github.com/cuilei5205189[cuilei5205189]
* https://github.com/franklinjosmell[franklinjosmell]
* https://github.com/notapatch[notapatch]
* https://github.com/promisepreston[promisepreston]
* https://github.com/tacataca[tacataca]