Rails API Tutorial Episode 07: JSONAPI.rb gem

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone this is mohammed from uh robbie  mark today i will be uh explaining how to create   a rails api using json api that's  rp let's uh first check out why   we should use this uh this gem uh first of all  if we want to follow the json api specifications   uh second if we want to use serializers  uh like active model serializers   it comes with the ready uh modules and mixings  so you um you just have to include them in the   controller and use many functionalities that you  will see in this tutorial it's also come with the   includes and sparse fields dynamically which  means that you can specify which include end   fields in the uh in the url parameters it supports  filtering and sorting pagination error handling   caching and the serialization this is maybe one of  my uh favorites episode because json api does rb   provides uh almost all what you need in an  api and by the way this is the gem that we are   using in our project in my job and it performs  really really good so let's go and discover it   uh first let's uh add the json api  that's rb in our gem file so gem json api   that's rp and run the bundle install once it's  done let's check in the uh in the github page it   says that you have to enable the uh the supports  for rails so let's add this in a initializer   so we go to uh config initializers and  create a file called json api desrt   and pass this code here this application it has  a books table and the outsource table so let's go   ahead and create the serializers related to this  tables so it's also come with a generator so we   can do rails generate serializer uh book then  title esbn language code and the norm of pages let's create another one rel generates  serializer outer with name attribute   great so let's uh go to the serializers file  it looks like the active model serializer gem   but here you include the json apis realizer  and then we have like this attribute title espn   language code no pages and we can also define  uh has many relationships so book will have   many outers let's go to the outer  serializer so the same we have we have the uh include the json apis realizer and  the uh the the attribute uh name let's   uh let's run our server and give it a try so http uh localhost port 3000 slash books  json but first let's go to the   books controller and make a small change here  json api instead of json so let's give it a try and yeah great so yeah here we have  the response following the json api   specifications so here we have like the  the first book with attribute and then   id and then relationships and the and the type  so the same for the uh for the others now if   we want to include the author's data as well and  maybe we want to uh to return only some specific   fields uh here so we can pass it in  the url so we can do like include   for example outers and fields of book  let's fetch only the title and the language language code okay and then pages as well but to  make it work we have to include the json fetching   in our controller so we have just  to come here and include json api fetching and here we have to override the json api include  method so we have to specify which includes do we   support so in our case it's  authors and we should also do uh super & to um um yeah to consider the *TO CONSIDER THE INCLUDES WE DO PASS IN THE URL AND ALLOW ONLY THE ONES WE LISTED HERE* uh yeah let's give it a try and yeah we see that they included the  section here which uh include our associations   which are the authors and here we  we also fetch only three attributes   instead of all the attributes so it looks  great here as you see that we didn't   include the relationship so we can  also add it so if we add outers here we got uh this uh uh included as well great so  yeah here we see that there is a implice one uh   queries i won't cover performance now but we can  do a later and next episode and in the regard and   filtering the json api does rb use user ransack  for for filtering so i i forget to mention that   it's also used the json apis realizer you can you  can have a look at this gem it's a fork of fast   json api which was created by netflix it's  not maintained anymore but it says that   the performance of this uh gem is is really really  uh much better than the uh active model serializer   so according to their benchmarks  it says that 25 times faster   but yeah we have now uh a fork for that and and  have a look at it it's really great i will cover   uh some tricks of this gym in the next episode  so uh so yeah and regarding the filtering so it's   used ransack so for that we can include the for  example some filters so if we want to a filter by for example title to contain i don't know uh lord this will return books whose  title contains this uh this word   but first we have to include the json api  filtering so we do include json api fill string and in the index action let's call the  json api filter method and first parameter we   return the list of books and in  the second parameter we specify   which attributes are allowed to filter by  so let's say we want to filter by title and   maybe author's name and this also allows other  arguments so if we want to add some allowed   scopes or whatever but we'll keep it like this for  now so then we add a block where we call the uh   the filtered records filtered filter red and here  we render uh our first result render json api filtered dots results yeah let's give it a try oops filtering oh yeah it's filtering nice so let's see uh the books  oops no we still return all the um books yeah let's check uh the uh  url oh yeah this should be and oh yes here we have to uh pass an array of the  allowed attributes let's give it another try   yeah great so here we uh return on the uh  uh one book we whose title uh contain lord   we can also change this filter to authors name contain equal to chris let's include the authors here and the names who which contain chris is four and  five we have to display books whose outers is uh   one of them three or four so here it's  correct it's four here is five and here is   five and four yeah it's uh it works so um yeah  this is for filtering also we can add the sorting   here we can add the sort by title of the books  so we can sort them alphabetically so so here   we have the first one is the lord and the one  is title let's uh uh sorts them in the other uh   in the other direction so to be like  descending so yeah here we found the uh   this book first and then the uh the other ones   and for pagination we also can specify  that in the uh url let's uh work on this uh url if you want for example to show only  three per page so we have to specify that   params it should be page size equal to 3 for instance before that let's uh  include that in the uh controller so we do include   json api pagination and here we should  call another method json api paginate   and here we pass this filtered results  and we open a block with the paginated   results and this is what we really want to  render paginated and let's give it a try   and yeah here we return only three books and we  also have the pagination links included so current   page and last page next page and this one um yeah  we also can add the uh page number so page number   equal to two by default is one so we have uh  more books the third page it should be empty yeah   so yeah this is for pagination now i  will explain the uh this realization   so if we want to um to create a book our  controller understand only the book parameter   which is used by our scaffold but if we want  to adapt this to uh to understand like json   api specification so we have to do uh a change in  in our controller so let's first include the json api this realization and change this books params to use  json api this sir yellow lies method   which um uh receive params as first arguments  and then the second arguments you uh you pass   a hash of options so here we want to only  include the title pages and language code and outers you can also uh pass accepts  for example with the with the   n1 unwanted attributes you can also specify a  polymorphic uh attribute here so uh in order to uh   to deserialize uh polymorphic relationships so  we don't have that so we will we will keep only   uh this one so we'll use the um uh the book params  and the creates action here we render the json api   formats and india the rest looks  looks good let's give it a try awesome it's uh it's created we  received like 201 and here we have   our book with the new id 6. so we  can do the same for the updates updates action so we do here json api now  we have to uh specify the method as puts   and then books let's update the um  the ones that we have created six   uh here we do title two uh updated we don't  want to update these attributes and these uh   either let's keep this very simple let's send it  so here yeah great it says that the statues is 200   india here we have like the uh book uh six has uh  changed its title to title to update with the same   authors great our uh api now still return like the  standard errors response so to use the json api   errors we have first to to include that in  the controller so include the json api errors and we have to change this json to json  api as we did before to test that let's uh   create some validation in the book model so  we want to validate the title to be present   so here if we want to update uh this book to  an empty title it shows the rails an error it looks like there is an error here yeah after  some debugging i found out that we have to call   uh json errors here and here the  same so let's give it another try and yeah here we have uh json api response  for errors as well yeah this is for   this episode i think i covered almost everything  that you might need in in in your api i hope you   found this series of tutorials useful please  write me your opinion about my content that   will help me a lot to determine my future focus uh  yeah have fun coding and see you in the next one
Info
Channel: Ruby au Maroc
Views: 482
Rating: undefined out of 5
Keywords: rails, rails 6, api, rest, ams, active_model_serializers, rabl, jbuilder, rails api, rails api tutorial, rails 7, ruby on rails, ruby on rails tutorial jsonapi, jsonapi spec, jsonapi specification, jsonapi serializer, jsonapi filter, jsonapi rails, jsonapi pagination, jsonapi rb pagination, jsonapi.rb gem, jsonapi rb include, jsonapi rb, jsonapi.rb, jsonapi-rb, deserialization, ransack, serialization, jsonapi rb sorting, jsonapi rb filtering
Id: dx0G-Ne0QGk
Channel Id: undefined
Length: 19min 27sec (1167 seconds)
Published: Sun Nov 28 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.