Created
June 6, 2020 16:59
-
-
Save marcoranieri/ff8b9a550b2bac52e206f737b4b84028 to your computer and use it in GitHub Desktop.
Active Record
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "json" | |
require "rest-client" | |
def get_request_to(url) | |
response = RestClient.get(url) | |
JSON.parse(response) | |
end | |
def build_post(story_id) | |
one_story_url = "https://hacker-news.firebaseio.com/v0/item/#{story_id}.json" | |
story = get_request_to(one_story_url) | |
my_post = Post.new(title: story["title"], url: story["url"], votes: story["score"]) | |
my_post.save! | |
end | |
url = "https://hacker-news.firebaseio.com/v0/topstories.json" | |
top_stories = JSON.parse(RestClient.get(url)) | |
ten_stories = top_stories.first(10) | |
ten_stories.each { |story_id| build_post(story_id) } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment