Skip to content

Instantly share code, notes, and snippets.

@negamorgan
Last active August 29, 2015 13:56
Show Gist options
  • Save negamorgan/9093628 to your computer and use it in GitHub Desktop.
Save negamorgan/9093628 to your computer and use it in GitHub Desktop.
Create array of reddit front page posts; no type data included
require 'json'
require 'rest-client'
r = JSON.parse(RestClient.get('http://reddit.com/.json'))
p = r["data"]["children"]
posts_array = p.collect { |post| post["data"] }
# posts_array is an array of post hashes
# you can iterate over posts_array or do posts_array[0]["url"]
posts_array.each do |p|
puts "#{p['title']}: #{p['permalink']}"
end
# key glossary
posts_array[0]["title"] #=> post title
posts_array[0]["permalink"] #=> post permalink
posts_array[0]["url"] #=> post reference URL (link out)
posts_array[0]["ups"] #=> post upvote count
posts_array[0]["downs"] #=> post downvote count
posts_array[0]["thumbnail"] #=> post thumbnail
@dfenjves
Copy link

You are da bomb.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment