Skip to content

Commit 4ecab55

Browse files
promiseprestonPromise Chukwuenyem
andauthored
Jsonapi serializer (#57)
* rename chapter04/chapter05 from athentification to authentication * Update fast_jsonapi to jsonapi-serializer for rails6 en Co-authored-by: Promise Chukwuenyem <[email protected]>
1 parent 4fc64ec commit 4ecab55

File tree

3 files changed

+33
-33
lines changed

3 files changed

+33
-33
lines changed

rails6/en/chapter06-improve-json.adoc

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -58,20 +58,20 @@ The content of the `data` key is also quite strict:
5858
* properties of the objects must be placed in an `attributes` key
5959
* links of the objects must be placed in a `relationships` key
6060

61-
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.
61+
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.
6262

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

6565
[source,bash]
6666
----
67-
$ bundle add fast_jsonapi
67+
$ bundle add jsonapi-serializer
6868
----
6969

7070
You should be ready to continue with this tutorial.
7171

7272
== Serialize user
7373

74-
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.
74+
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.
7575

7676
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:
7777

@@ -87,7 +87,7 @@ This has created a file called `user_serializer.rb` under the `app/serializers`
8787
.app/serializers/user_serializer.rb
8888
----
8989
class UserSerializer
90-
include FastJsonapi::ObjectSerializer
90+
include JSONAPI::Serializer
9191
attributes :email
9292
end
9393
----
@@ -111,12 +111,12 @@ There you go. As you can see this is easy. Now we can use our new _serializer_ i
111111
class Api::V1::UsersController < ApplicationController
112112
# ...
113113
def show
114-
render json: UserSerializer.new(@user).serializable_hash
114+
render json: UserSerializer.new(@user).serializable_hash.to_json
115115
end
116116
117117
def update
118118
if @user.update(user_params)
119-
render json: UserSerializer.new(@user).serializable_hash
119+
render json: UserSerializer.new(@user).serializable_hash.to_json
120120
else
121121
# ...
122122
end
@@ -125,7 +125,7 @@ class Api::V1::UsersController < ApplicationController
125125
def create
126126
# ...
127127
if @user.save
128-
render json: UserSerializer.new(@user).serializable_hash, status: :created
128+
render json: UserSerializer.new(@user).serializable_hash.to_json, status: :created
129129
else
130130
# ...
131131
end
@@ -194,7 +194,7 @@ Now let's add attributes to serialize the product:
194194
.app/serializers/product_serializer.rb
195195
----
196196
class ProductSerializer
197-
include FastJsonapi::ObjectSerializer
197+
include JSONAPI::Serializer
198198
attributes :title, :price, :published
199199
end
200200
----
@@ -208,25 +208,25 @@ class Api::V1::ProductsController < ApplicationController
208208
# ...
209209
def index
210210
@products = Product.all
211-
render json: ProductSerializer.new(@products).serializable_hash
211+
render json: ProductSerializer.new(@products).serializable_hash.to_json
212212
end
213213
214214
def show
215-
render json: ProductSerializer.new(@product).serializable_hash
215+
render json: ProductSerializer.new(@product).serializable_hash.to_json
216216
end
217217
218218
def create
219219
product = current_user.products.build(product_params)
220220
if product.save
221-
render json: ProductSerializer.new(product).serializable_hash, status: :created
221+
render json: ProductSerializer.new(product).serializable_hash.to_json, status: :created
222222
else
223223
# ...
224224
end
225225
end
226226
227227
def update
228228
if @product.update(product_params)
229-
render json: ProductSerializer.new(@product).serializable_hash
229+
render json: ProductSerializer.new(@product).serializable_hash.to_json
230230
else
231231
# ...
232232
end
@@ -493,7 +493,7 @@ To pass this test we will start by including the relationship in the _serializer
493493
.app/serializers/product_serializer.rb
494494
----
495495
class ProductSerializer
496-
include FastJsonapi::ObjectSerializer
496+
include JSONAPI::Serializer
497497
attributes :title, :price, :published
498498
belongs_to :user
499499
end
@@ -533,7 +533,7 @@ class Api::V1::ProductsController < ApplicationController
533533
# ...
534534
def show
535535
options = { include: [:user] }
536-
render json: ProductSerializer.new(@product, options).serializable_hash
536+
render json: ProductSerializer.new(@product, options).serializable_hash.to_json
537537
end
538538
# ...
539539
end
@@ -607,7 +607,7 @@ _serializer_:
607607
.app/serializers/user_serializer.rb
608608
----
609609
class UserSerializer
610-
include FastJsonapi::ObjectSerializer
610+
include JSONAPI::Serializer
611611
attributes :email
612612
has_many :products
613613
end
@@ -622,7 +622,7 @@ class Api::V1::UsersController < ApplicationController
622622
# ...
623623
def show
624624
options = { include: [:products] }
625-
render json: UserSerializer.new(@user, options).serializable_hash
625+
render json: UserSerializer.new(@user, options).serializable_hash.to_json
626626
end
627627
# ...
628628
end
@@ -948,7 +948,7 @@ class Api::V1::ProductsController < ApplicationController
948948
# ...
949949
def index
950950
@products = Product.search(params)
951-
render json: ProductSerializer.new(@products).serializable_hash
951+
render json: ProductSerializer.new(@products).serializable_hash.to_json
952952
end
953953
# ...
954954
end
@@ -980,4 +980,4 @@ $ git merge chapter06
980980

981981
== Conclusion
982982

983-
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.
983+
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.

rails6/en/chapter07-placing-orders.adoc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ And let's add relationships:
246246
[source,ruby]
247247
----
248248
class OrderSerializer
249-
include FastJsonapi::ObjectSerializer
249+
include JSONAPI::Serializer
250250
belongs_to :user
251251
has_many :products
252252
end
@@ -261,7 +261,7 @@ class Api::V1::OrdersController < ApplicationController
261261
before_action :check_login, only: %i[index]
262262
263263
def index
264-
render json: OrderSerializer.new(current_user.orders).serializable_hash
264+
render json: OrderSerializer.new(current_user.orders).serializable_hash.to_json
265265
end
266266
end
267267
----
@@ -335,7 +335,7 @@ class Api::V1::OrdersController < ApplicationController
335335
336336
if order
337337
options = { include: [:products] }
338-
render json: OrderSerializer.new(order, options).serializable_hash
338+
render json: OrderSerializer.new(order, options).serializable_hash.to_json
339339
else
340340
head 404
341341
end

rails6/en/chapter09-optimization.adoc

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class Api::V1::ProductsController < ApplicationController
6262
.per(params[:per_page])
6363
.search(params)
6464
65-
render json: ProductSerializer.new(@products).serializable_hash
65+
render json: ProductSerializer.new(@products).serializable_hash.to_json
6666
end
6767
# ...
6868
end
@@ -163,7 +163,7 @@ class Api::V1::ProductsController < ApplicationController
163163
}
164164
}
165165
166-
render json: ProductSerializer.new(@products, options).serializable_hash
166+
render json: ProductSerializer.new(@products, options).serializable_hash.to_json
167167
end
168168
end
169169
----
@@ -249,7 +249,7 @@ class Api::V1::OrdersController < ApplicationController
249249
}
250250
}
251251
252-
render json: OrderSerializer.new(@orders, options).serializable_hash
252+
render json: OrderSerializer.new(@orders, options).serializable_hash.to_json
253253
end
254254
# ...
255255
end
@@ -383,7 +383,7 @@ class Api::V1::OrdersController < ApplicationController
383383
384384
options = get_links_serializer_options('api_v1_orders_path', @orders)
385385
386-
render json: OrderSerializer.new(@orders, options).serializable_hash
386+
render json: OrderSerializer.new(@orders, options).serializable_hash.to_json
387387
end
388388
# ...
389389
end
@@ -403,7 +403,7 @@ class Api::V1::ProductsController < ApplicationController
403403
404404
options = get_links_serializer_options('api_v1_products_path', @products)
405405
406-
render json: ProductSerializer.new(@products, options).serializable_hash
406+
render json: ProductSerializer.new(@products, options).serializable_hash.to_json
407407
end
408408
# ...
409409
end
@@ -427,7 +427,7 @@ $ git commit -am "Factorize pagination"
427427

428428
== API Caching
429429

430-
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.
430+
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.
431431

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

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

@@ -455,7 +455,7 @@ end
455455
----
456456
class ProductSerializer
457457
# ...
458-
cache_options enabled: true, cache_length: 12.hours
458+
cache_options store: Rails.cache, namespace: 'jsonapi-serializer', expires_in: 1.hour
459459
end
460460
----
461461

@@ -464,7 +464,7 @@ end
464464
----
465465
class UserSerializer
466466
# ...
467-
cache_options enabled: true, cache_length: 12.hours
467+
cache_options store: Rails.cache, namespace: 'jsonapi-serializer', expires_in: 1.hour
468468
end
469469
----
470470

@@ -538,7 +538,7 @@ class Api::V1::ProductsController < ApplicationController
538538
options = get_links_serializer_options('api_v1_products_path', @products)
539539
options[:include] = [:user]
540540
541-
render json: ProductSerializer.new(@products, options).serializable_hash
541+
render json: ProductSerializer.new(@products, options).serializable_hash.to_json
542542
end
543543
# ...
544544
end
@@ -642,7 +642,7 @@ class Api::V1::ProductsController < ApplicationController
642642
options = get_links_serializer_options('api_v1_products_path', @products)
643643
options[:include] = [:user]
644644
645-
render json: ProductSerializer.new(@products, options).serializable_hash
645+
render json: ProductSerializer.new(@products, options).serializable_hash.to_json
646646
end
647647
# ...
648648
end

0 commit comments

Comments
 (0)