DEV Community

madelinemc
madelinemc

Posted on

Using Rest Client for Seeding Database

With the shortest requirement list yet, the sky was the limit for my final project for Flatiron School - it was almost too open ended but I decided to pay homage to my Art History minor and my love of the Met Museum. Besides the now retired (😢) tin buttons marking your visit(which I also paid tribute to in my button icons), the best part about a trip to the Met is that you never know what you might see as you wander around through her maze of halls and galleries. To recreate a little bit of the excitement of the unknown, I decided to present the user with a sampling of 5 works of art picked at random that will change upon every refresh.

The Met has a really robust API of their collection - over 470,000 distinct objects!
Screen Shot 2021-04-11 at 12.59.18 PM
Talk about a lot of data! I needed to figure out how to get at least a sampling of this data into my backend API. I found just what I was looking for in RestClient. In theory it is super easy to use and I will give a few quick steps below. I say in theory because I think it depends on how well set up the API is that you are pulling your data from. If you are wondering, the Met's was really easy sometimes and really hard others - some of the art objects were missing attributes that I wanted to pull so I had hand hold my seed file during some of the departments. For brevity sake, I will use one of the easier bit of data to pull - the Met's list of departments. (seeding the art objects themselves was another story all together!)

Step 1:

To get started, add the Rest Client Gem to your gem file, and require it in your application.rb file. Then bundle install in your terminal.

Gem file: gem ‘rest-client’
application.rb: require "rest-client"
terminal: $ bundle install

Step 2:

To set up your MVC file structure, you'll want to play around with what the data looks like coming from your chosen API. The Met gave several examples so I was able to pick the information I wanted to save about each department and artwork and then set up my models and database migrations accordingly.
Once models are generated, create and set up your database by running in your terminal window:
rails db:create
rails db:migrate

Step 3:

Next up is the seed file. You'll want to a make a method for collecting the data that will then be called when you run the seed file. The gist is: save the data from rest client to a variable, parse the data and save it as JSON, and use the data you want to instantiate new instances of your class.
This is what the Met's department query returns:
So my department model is simply,

Save the data from using RestClient to query the API database to a variable:

department_data = RestClient.get('https://collectionapi.metmuseum.org/public/collection/v1/departments')
Enter fullscreen mode Exit fullscreen mode

If the query returns a 200 success code, save the parsed data to another variable:

parsed_department_data = JSON.parse(department_data)
Enter fullscreen mode Exit fullscreen mode

Pull the data you want from the JSON and save to a variable. In my case, I am pulling the department objects creating an array:

department_array = parsed_department_data['departments']
Enter fullscreen mode Exit fullscreen mode

Iterate over the array to create new instances of your class with each item in the array:

department_array.each { |dept|
    department = Department.create(
        name: dept['displayName']
    )
}
Enter fullscreen mode Exit fullscreen mode

Step 4:

Call the method by writing the name of the method at the end of your file and in the terminal, run the file to seed your database.
Here is the full method and call in seeds.rb:

def get_department_data

    department_data = RestClient.get('https://collectionapi.metmuseum.org/public/collection/v1/departments')
    if department_data.code == 200
        parsed_department_data = JSON.parse(department_data)
        department_array = parsed_department_data['departments']
        department_array.each { |dept|
            department = Department.create(
                name: dept['displayName']
            )
        }
    end

end

get_department_data
Enter fullscreen mode Exit fullscreen mode

terminal call:
rails db:seed

Step 5:

Once all of your data has been saved in your database, it can be viewed in the browser at your localhost and can be accessed via a fetch request from your front end API.

Create an index route in your controller that renders JSON:

def index
        departments = Department.all
        render({json: departments, except: [:created_at, :updated_at] })
end
Enter fullscreen mode Exit fullscreen mode

Opening the route in the browser window looks like this:
http://localhost:3000/departments
Screen Shot 2021-04-11 at 1.43.39 PM

For a more complicated use case, I will write next about seeding the artwork objects associated with each department - stay tuned :) And please let me know if you have any suggestions about using Rest Client.
My seed file can be found here: ArtStart - Back-end

Top comments (0)