From 419a5aa34ce1387cf760334e3be478cfe9f9d3b0 Mon Sep 17 00:00:00 2001 From: Promise Chukwuenyem Date: Sat, 19 Dec 2020 12:26:05 +0100 Subject: [PATCH 1/2] rename chapter04/chapter05 from athentification to authentication --- rails5/en/chapter00-before.adoc | 1 + ...er05-athentification.adoc => chapter05-authentication.adoc} | 0 rails5/fr/chapter00-before.adoc | 1 + ...er05-athentification.adoc => chapter05-authentication.adoc} | 0 rails6/en/chapter00-before.adoc | 1 + ...er04-athentification.adoc => chapter04-authentication.adoc} | 0 rails6/es/chapter00-before.adoc | 3 ++- ...er04-athentification.adoc => chapter04-authentication.adoc} | 0 rails6/fr/chapter00-before.adoc | 1 + ...er04-athentification.adoc => chapter04-authentication.adoc} | 0 10 files changed, 6 insertions(+), 1 deletion(-) rename rails5/en/{chapter05-athentification.adoc => chapter05-authentication.adoc} (100%) rename rails5/fr/{chapter05-athentification.adoc => chapter05-authentication.adoc} (100%) rename rails6/en/{chapter04-athentification.adoc => chapter04-authentication.adoc} (100%) rename rails6/es/{chapter04-athentification.adoc => chapter04-authentication.adoc} (100%) rename rails6/fr/{chapter04-athentification.adoc => chapter04-authentication.adoc} (100%) diff --git a/rails5/en/chapter00-before.adoc b/rails5/en/chapter00-before.adoc index dd84d00..1549838 100644 --- a/rails5/en/chapter00-before.adoc +++ b/rails5/en/chapter00-before.adoc @@ -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] diff --git a/rails5/en/chapter05-athentification.adoc b/rails5/en/chapter05-authentication.adoc similarity index 100% rename from rails5/en/chapter05-athentification.adoc rename to rails5/en/chapter05-authentication.adoc diff --git a/rails5/fr/chapter00-before.adoc b/rails5/fr/chapter00-before.adoc index 89df69f..3346969 100644 --- a/rails5/fr/chapter00-before.adoc +++ b/rails5/fr/chapter00-before.adoc @@ -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] diff --git a/rails5/fr/chapter05-athentification.adoc b/rails5/fr/chapter05-authentication.adoc similarity index 100% rename from rails5/fr/chapter05-athentification.adoc rename to rails5/fr/chapter05-authentication.adoc diff --git a/rails6/en/chapter00-before.adoc b/rails6/en/chapter00-before.adoc index 1c88505..df721f6 100644 --- a/rails6/en/chapter00-before.adoc +++ b/rails6/en/chapter00-before.adoc @@ -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] diff --git a/rails6/en/chapter04-athentification.adoc b/rails6/en/chapter04-authentication.adoc similarity index 100% rename from rails6/en/chapter04-athentification.adoc rename to rails6/en/chapter04-authentication.adoc diff --git a/rails6/es/chapter00-before.adoc b/rails6/es/chapter00-before.adoc index 638f50a..233d6d6 100644 --- a/rails6/es/chapter00-before.adoc +++ b/rails6/es/chapter00-before.adoc @@ -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 @@ -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] diff --git a/rails6/es/chapter04-athentification.adoc b/rails6/es/chapter04-authentication.adoc similarity index 100% rename from rails6/es/chapter04-athentification.adoc rename to rails6/es/chapter04-authentication.adoc diff --git a/rails6/fr/chapter00-before.adoc b/rails6/fr/chapter00-before.adoc index 6d7fcc8..3cd9276 100644 --- a/rails6/fr/chapter00-before.adoc +++ b/rails6/fr/chapter00-before.adoc @@ -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] diff --git a/rails6/fr/chapter04-athentification.adoc b/rails6/fr/chapter04-authentication.adoc similarity index 100% rename from rails6/fr/chapter04-athentification.adoc rename to rails6/fr/chapter04-authentication.adoc From c27e3584ea532ede9281675b75d1ddd938e72606 Mon Sep 17 00:00:00 2001 From: Promise Chukwuenyem Date: Sat, 19 Dec 2020 13:35:32 +0100 Subject: [PATCH 2/2] Update fast_jsonapi to jsonapi-serializer for rails6 en --- rails6/en/chapter06-improve-json.adoc | 38 ++++++++++++------------- rails6/en/chapter07-placing-orders.adoc | 6 ++-- rails6/en/chapter09-optimization.adoc | 22 +++++++------- 3 files changed, 33 insertions(+), 33 deletions(-) diff --git a/rails6/en/chapter06-improve-json.adoc b/rails6/en/chapter06-improve-json.adoc index e394380..8cf2bbb 100644 --- a/rails6/en/chapter06-improve-json.adoc +++ b/rails6/en/chapter06-improve-json.adoc @@ -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: @@ -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 ---- @@ -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 @@ -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 @@ -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 ---- @@ -208,17 +208,17 @@ 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 @@ -226,7 +226,7 @@ class Api::V1::ProductsController < ApplicationController 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 @@ -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 @@ -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 @@ -607,7 +607,7 @@ _serializer_: .app/serializers/user_serializer.rb ---- class UserSerializer - include FastJsonapi::ObjectSerializer + include JSONAPI::Serializer attributes :email has_many :products end @@ -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 @@ -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 @@ -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. diff --git a/rails6/en/chapter07-placing-orders.adoc b/rails6/en/chapter07-placing-orders.adoc index 1a2e2c9..62111a4 100644 --- a/rails6/en/chapter07-placing-orders.adoc +++ b/rails6/en/chapter07-placing-orders.adoc @@ -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 @@ -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 ---- @@ -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 diff --git a/rails6/en/chapter09-optimization.adoc b/rails6/en/chapter09-optimization.adoc index f32fd01..70e31bb 100644 --- a/rails6/en/chapter09-optimization.adoc +++ b/rails6/en/chapter09-optimization.adoc @@ -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 @@ -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 ---- @@ -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 @@ -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 @@ -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 @@ -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: @@ -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 ---- @@ -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 ---- @@ -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 ---- @@ -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 @@ -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