Skip to content

Instantly share code, notes, and snippets.

@marcoranieri
Created June 6, 2020 16:59
Show Gist options
  • Save marcoranieri/ff8b9a550b2bac52e206f737b4b84028 to your computer and use it in GitHub Desktop.
Save marcoranieri/ff8b9a550b2bac52e206f737b4b84028 to your computer and use it in GitHub Desktop.
Active Record
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