Skip to content

Instantly share code, notes, and snippets.

@sammylupt
Created October 2, 2013 13:49
Show Gist options
  • Save sammylupt/6794108 to your computer and use it in GitHub Desktop.
Save sammylupt/6794108 to your computer and use it in GitHub Desktop.
require 'json'
require 'rest_client'
reddit_hash = JSON.parse(RestClient.get('http://reddit.com/.json'))
posts = reddit_hash["data"]["children"]
#remove all posts which are NSFW
posts = posts.select { |post| post["data"]["over_18"] != true }
#build templating method
def template(post)
upvotes = post["data"]["ups"]
downvotes = post["data"]["downs"]
permalink = "http://www.reddit.com" + post["data"]["permalink"]
title = post["data"]["title"]
thumbnail = post["data"]["thumbnail"]
"<li><a href='#{permalink}'><h1>#{title}</h1><img src='#{thumbnail}'/><h4>Upvotes:</h4><p>#{upvotes}</p><h4>Downvotes:</h4><p>#{downvotes}</p></a></li>"
end
#run the template method on all posts
html_output = posts.map { |post| template(post) }
#add necessary HTML for beginning and end of file
write_to_file = '<html><head></head><body><ul>' + html_output.join + "</ul></body></html>"
#write to file
output = File.open("reddit-refactor.html", "w")
output << write_to_file
output.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment