Reputation: 6116
So I'm using a gem file to pull links from the front page of reddit. The gem function returns a hash containing ALL the links on the front page and all their info (# of comments, karma, author, date, link, ups, downs, etc). Below is a begging of the hash containg the information for the FIRST link on my front page:
{"modhash"=>"ubekfji9tr491ba98806d33ec78693dc157579335eb1ab283b", "children"=>[{"kind"=>"t3", "data"=>{"domain"=>"i.imgur.com", "banned_by"=>nil, "media_embed"=>{}, "subreddit"=>"pics", "selftext_html"=>nil, "selftext"=>"", "likes"=>nil, "link_flair_text"=>nil, "id"=>"1dt29e", "clicked"=>false, "title"=>"Our plumber in Italy put himself out there to ask us for help on his English homework :) Hope he passes his finals!", "media"=>nil, "score"=>3700, "approved_by"=>nil, "over_18"=>false, "hidden"=>false, "thumbnail"=>"http://b.thumbs.redditmedia.com/Fdid4luCfXah0bQ7.jpg", "subreddit_id"=>"t5_2qh0u", "edited"=>false, "link_flair_css_class"=>nil, "author_flair_css_class"=>nil, "downs"=>17890, "saved"=>false, "is_self"=>false, "permalink"=>"/r/pics/comments/1dt29e/our_plumber_in_italy_put_himself_out_there_to_ask/", "name"=>"t3_1dt29e", "created"=>1367893951.0, "url"=>"https://i.sstatic.net/DWZRf.jpg", "author_flair_text"=>nil, "author"=>"Junpha", "created_utc"=>1367865151.0, "distinguished"=>nil, "num_comments"=>553, "num_reports"=>nil, "ups"=>21590}},
That ending ,
seperates the next link which looks like this:
{"kind"=>"t3", "data"=>{"domain"=>"i.imgur.com", "banned_by"=>nil, "media_embed"=>{}, "subreddit"=>"funny", "selftext_html"=>nil, "selftext"=>"", "likes"=>nil, "link_flair_text"=>nil, "id"=>"1dt4xv", "clicked"=>false, "title"=>"What a beautiful, majestic.... oops", "media"=>nil, "score"=>2848, "approved_by"=>nil, "over_18"=>false, "hidden"=>false, "thumbnail"=>"http://a.thumbs.redditmedia.com/vkstE1PFr9iyYpux.jpg", "subreddit_id"=>"t5_2qh33", "edited"=>false, "link_flair_css_class"=>nil, "author_flair_css_class"=>nil, "downs"=>8128, "saved"=>false, "is_self"=>false, "permalink"=>"/r/funny/comments/1dt4xv/what_a_beautiful_majestic_oops/", "name"=>"t3_1dt4xv", "created"=>1367895955.0, "url"=>"https://i.sstatic.net/jzIqQ.gif", "author_flair_text"=>nil, "author"=>"themisc", "created_utc"=>1367867155.0, "distinguished"=>nil, "num_comments"=>279, "num_reports"=>nil, "ups"=>10976}},
I basically need someway to parse each entry of this hash so that I can store all the relevant stuff into an array. Really, I just want to store the "title"
and "url"
parts into an array. I tried something like this:
@red.get_listing().each do |x['title']|
puts x
end
But i realized this is a hash of a hash..so I'm kinda lost as to how to parse this.
Upvotes: 1
Views: 9958
Reputation: 21574
hash = {"modhash"=>"ubekfji9tr491ba98806d33ec78693dc157579335eb1ab283b",
"children"=>[{"kind"=>"t3", "data"=>{"domain"=>"i.imgur.com", "banned_by"=>nil, "media_embed"=>{}, "subreddit"=>"pics", "selftext_html"=>nil, "selftext"=>"", "likes"=>nil, "link_flair_text"=>nil, "id"=>"1dt29e", "clicked"=>false, "title"=>"Our plumber in Italy put himself out there to ask us for help on his English homework :) Hope he passes his finals!", "media"=>nil, "score"=>3700, "approved_by"=>nil, "over_18"=>false, "hidden"=>false, "thumbnail"=>"http://b.thumbs.redditmedia.com/Fdid4luCfXah0bQ7.jpg", "subreddit_id"=>"t5_2qh0u", "edited"=>false, "link_flair_css_class"=>nil, "author_flair_css_class"=>nil, "downs"=>17890, "saved"=>false, "is_self"=>false, "permalink"=>"/r/pics/comments/1dt29e/our_plumber_in_italy_put_himself_out_there_to_ask/", "name"=>"t3_1dt29e", "created"=>1367893951.0, "url"=>"http://i.imgur.com/3xn1c8s.jpg", "author_flair_text"=>nil, "author"=>"Junpha", "created_utc"=>1367865151.0, "distinguished"=>nil, "num_comments"=>553, "num_reports"=>nil, "ups"=>21590}},{"kind"=>"t3", "data"=>{"domain"=>"i.imgur.com", "banned_by"=>nil, "media_embed"=>{}, "subreddit"=>"funny", "selftext_html"=>nil, "selftext"=>"", "likes"=>nil, "link_flair_text"=>nil, "id"=>"1dt4xv", "clicked"=>false, "title"=>"What a beautiful, majestic.... oops", "media"=>nil, "score"=>2848, "approved_by"=>nil, "over_18"=>false, "hidden"=>false, "thumbnail"=>"http://a.thumbs.redditmedia.com/vkstE1PFr9iyYpux.jpg", "subreddit_id"=>"t5_2qh33", "edited"=>false, "link_flair_css_class"=>nil, "author_flair_css_class"=>nil, "downs"=>8128, "saved"=>false, "is_self"=>false, "permalink"=>"/r/funny/comments/1dt4xv/what_a_beautiful_majestic_oops/", "name"=>"t3_1dt4xv", "created"=>1367895955.0, "url"=>"http://i.imgur.com/62q1jWN.gif", "author_flair_text"=>nil, "author"=>"themisc", "created_utc"=>1367867155.0, "distinguished"=>nil, "num_comments"=>279, "num_reports"=>nil, "ups"=>10976}}
]}
hash['children'].map { |child| child['data']['url'] }
hash['children'].map { |child| child['data']['title'] }
or if you want to use symbols (and are in rails)
hash.with_indifferent_access[:children].first[:data][:title]
Upvotes: 1
Reputation: 76240
You don't really need to parse them, just access its value:
hash[:data][:title]
hash[:data][:url]
Upvotes: 0
Reputation: 29880
hash[:children].each do |child|
puts child[:title]
puts child[:url]
end
Does that help?
Upvotes: 2