Seeding data using external APIs with “RestClient” Gem

Yuri Nguyen
4 min readMay 28, 2021

There are a good amount of Ruby gems out there to work with external APIs such as Net::HTTP, HTTParty, however, in this blog I am going to show you my approach using “RestClient” Gem with The Movie Database (TMDb) API example. So let’s get started!

Once you have access to the API Key from your desired API source, my 1st step is to make several requests to check how the information returned, what type of data you will be receiving so you can decide what attributes to give your table (in this example is Movie table).

Replace <<api_key>> with your actual key and test it out either on Google Chrome/Insomnia or POSTMAN.
And those are the data I get back. Looks like whatever we need is nested inside “results”

2nd step is to create a Model, Controller, Table & Routes with rails g resource command. (Don’t forget to run rails db:migrate after your table is created)

3rd step is to install these 3 following gems in your Gemfile.

4th step: in the root directory of your application create a file named: .env

What gem ‘json’ does is to parse a response that we get from our API into some workable format of data which is either a hash or an array. Gem ‘dotenv-rails’ provides an environment variable and what we can do is we can add environment variables which is the API Key to a .env file.

name your key whatever you want.

API key exposure CAN result in significant damage, both to a company and to the data it holds so it leads to my 5th step which I consider the most important step before you proceed any further is to add .env file to .gitignore. What gitignore does is when you push up all of your code to GitHub, anything listed in gitignore will NOT become public. Check out this link for more ways to HIDE YOUR API KEYs!!!

Next step: Copy and paste your request URL into seed file however replace <<api_key>> with the variable in .env file:

using interpolation to inject API key into your request. Then run rails db:seed to hit byebug after response.

response gives us a Response 200 which means it’s a successful request. Run response.body to return the body of the response. As you can see down below, it looks very much the same like the 2nd top image above. The only different is the data type we got is string. This is where gem ‘json’ comes in helping us turn this string data into array/hashes.

I mentioned earlier that all the attributes we need are nested inside results.

Almost done guys. The last step is to create Movie Object by iterrating to this movie_array used .each method:

Check out tmdb documentation on how to generate a fully working image URL. 3 pieces of data are required: a base_url:”https://image.tmdb.org/t/p/", a file size:"w500/" and a poster_path.

And Thats it we are done !!! Thank you so much if you stick till the last step. I hope you find this helpful and please leave a comment about any mistake I might make in this blog.

--

--

Yuri Nguyen

Student at Flatiron School. A traveler. A bubble tea aficionado.