Skip to content

Instantly share code, notes, and snippets.

@ahimmelstoss
Created October 11, 2013 18:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahimmelstoss/6940009 to your computer and use it in GitHub Desktop.
Save ahimmelstoss/6940009 to your computer and use it in GitHub Desktop.
require 'json'
require 'open-uri'
html_tag =
'<html>
<head>
</head>
<body>
<ul>
LI TAGS
</ul>
</body>
</html>'
li_tags = ''
li_tag =
'<li>
<a href="REDDIT URL">
<h1>POST TITLE</h1>
<img src="THUMBNAIL URL" />
<h4>Upvotes:</h4>
<p>NUMBER OF UPVOTES</p>
<h4>Downvotes:</h4>
<p>NUMBER OF DOWNVOTES</p>
</a>
</li>'
reddit_hash = JSON.parse(open('http://www.reddit.com/r/cats/search.json?q=bow+tie&restrict_sr=on&sort=relevance&t=all').read) #open string and send it to ruby gem json
#deconstructing this hash one level at a time and put it in its own array to make more intuitive
reddit_data = reddit_hash["data"]
reddit_children = reddit_data["children"]
reddit_children.each do |story|
story_data = story["data"]
story_title = story_data["title"]
story_url = story_data["url"]
story_thumbnail = story_data["thumbnail"]
story_over_18 = story_data["over_18"]
story_upvote = story_data["ups"]
story_downvote = story_data["downs"]
if !story_over_18 #over_18 is false, so !story_over_18 is true
my_li_tag = li_tag.sub("REDDIT URL", story_url)
my_li_tag = my_li_tag.sub("POST TITLE", story_title)
my_li_tag = my_li_tag.sub("THUMBNAIL URL", story_thumbnail)
my_li_tag = my_li_tag.sub("NUMBER OF UPVOTES", story_upvote.to_s)
my_li_tag = my_li_tag.sub("NUMBER OF DOWNVOTES", story_downvote.to_s)
li_tags << my_li_tag
end
end
full_html_page = html_tag.sub!("LI TAGS", li_tags)
reddit_page = File.open("Reddit.html", "w")
reddit_page << full_html_page
reddit_page.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment