Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

jaredsurya
jaredsurya

Posted on

     

Seeding to Rails from an API

Intro

I am currently a student finishing up phase 4 of my cirriculum at Flatiron School. For this phase I had to reflect on what I had learned, and what I could share with you. While making the full-stack web application (React front-end, Rails back-end), I encountered many problems which I solved. I thought I would convey here one of those, and attempt to create a mini walkthrough guide in case you're attempting to do the same thing.

While I was building my project - a single page application - there were a number of problems that I had to overcome. The unique coding situation that I found myself in whilst planning involved seeding. I had to get a specific quantity of data, which existed on the internet, into my SQL database. I discovered an API which hosted all the relevant info I needed, and I wanted to fetch it using (here-to-fore unknown) code in my railsseeds.rb file.

I got my data fromsuperheroapi.com. The site had far more info than what I needed, so I had to figure out how to selectively import data via code in myseeds.rb file. I began searching around and below you will find my discoveries. None of this stuff was easy, but with some crafty deductive logic it all came together into just what my project needs.


The Good Stuff

I new I wanted to make a webpage about superheroes and I was glad to find out they had an API for just that. As I sifted through the data, I found out it was quite comprehensive - FULL of info:

Raw data

I only wanted a fraction of that (like 5 key-value pairs), so I had to figure out how to selectively take what I needed and store it, through Rails, in my database. This led me to needing to download and use therest-client ruby gem, which I'll detail for you below.

REST-client

rest-client has been described as

"A simple HTTP and REST client for Ruby, inspired by the Sinatra microframework style of specifying actions: get, put, post, delete."

andthis Github link contains most of the information needed to get started using it. As I learned fromrubygems.org, all you have to do to get started is run the following command in your terminal. Be sure you are in the appropriate directory, such as the parent directory of your rails project.

gem install rest-client
Enter fullscreen modeExit fullscreen mode

Once you have installed the gem, navigate to yourseeds.rb file (within your text editor) and at the very top of the file, typerequire 'rest-client'. This will enable your file to use the commands from the rest-client library.

CODING the logic

superhero seed logic

In myheroesdata method, on line 9 of my code above, I get down to the business of extracting the information from the external API. By using therest-client gem, I was easily able to connect with the resource and make use of the information that was delivered. MyHero table needed to be populated with heroes. In order to do so, I simply made a GET requst to my endpoint. The syntax for this is:

RestClient.get(url, headers={})
Enter fullscreen modeExit fullscreen mode

On line 10 of my code I callrest-client in this way with aGET method and pass in the URL. This delivers the appropriate hero I need, and it needs formatting. The hero that's returned here has way more information than what I can use. I used constructive logic to take away just the components I wanted (key, value pairs), piece by piece. I just took what was useful and left the rest.

To begin with, I parsed the data into a useablehero_array. Then, I used the nested data to populate a hash which I callfinal_hero on line 21. On line 28 I submited that hash and effectively created the first instance of aHero on my heroes table. I need more than one hero, so I make sure the code loops through theheroesdata method many times. I chose to make 35 heros because that fit the needs of my app, but you could conceivably choose any number you want. With adequateHero instances created, I worked on my front end (react-based) to display the data for use (pictured below).

heroes displayed

Conclusion

Through usingrest-api gem and some crafty reasoning, I was able to reduce the large quantity of information to a hash that is much smaller:

final_hero = {    name: hero_array["name"],     full_name: full_name,     power_level: power_level,    publisher: hero_array["biography"]["publisher"],    image: hero_array["image"]["url"]  }
Enter fullscreen modeExit fullscreen mode

With only 5 key-value pairs (now manageable and precise), the app was thus crafted and everything fit nicely.
It was challenging but fun to create each of these unique features.rest-client was very helpful for when I needed to seed data using an API, and I hope it works for you too. It was relatively easy to figure out, and I know you will find it useful too. I am grateful for what hard work it must have taken to create such a useful ruby gem,rest-api.

Thank you for reading & happy coding!

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

A software engineer who is always learning. I enjoy colllaborative efforts where I can put my skills to use. I aim to create many great things. As the learning process goes on, I will post about it!
  • Location
    Ithaca, NY
  • Education
    Flatiron School
  • Work
    Freelance Software Engineer
  • Joined

More fromjaredsurya

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp