Skip to content

Instantly share code, notes, and snippets.

@chelsea
Created May 24, 2013 07:05
Show Gist options
  • Save chelsea/5641777 to your computer and use it in GitHub Desktop.
Save chelsea/5641777 to your computer and use it in GitHub Desktop.
<li data-row="1" data-col="1" data-sizex="1" data-sizey="1">
<div data-id="random_aww" data-view="RandomAww"></div>
</li>
<li data-row="1" data-col="1" data-sizex="1" data-sizey="1">
<div data-id="animalsbeingjerks" data-view="RandomAww"></div>
</li>
require 'net/http'
require 'json'
placeholder = '/assets/nyantocat.gif'
reddits = { 'random_aww' => 'awww', 'animalsbeingjerks' => 'animalsbeingjerks' }
reddits.each do |title, reddit|
SCHEDULER.every '60s', first_in: 0 do |job|
http = Net::HTTP.new('www.reddit.com')
response = http.request(Net::HTTP::Get.new("/r/#{reddit}.json"))
json = JSON.parse(response.body)
if json['data']['children'].count <= 0
send_event(title, image: placeholder)
else
urls = json['data']['children'].map{|child| child['data']['url'] }
# Ensure we're linking directly to an image, not a gallery etc.
send_event(title, image: urls.select{|url| url.downcase.end_with?('png', 'gif', 'jpg', 'jpeg') }.sample(1).first)
end
end
end
@scottharvey
Copy link

Seems like a decent solution, since the server is just pushing data to the client it would be tricky to do anything else.

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