Skip to content

Instantly share code, notes, and snippets.

@astevens astevens/hn.rb
Created Oct 8, 2014

Embed
What would you like to do?
Hacker News API demo
#! /usr/bin/env ruby
require 'rest-firebase'
f = RestFirebase.new :site => 'https://hacker-news.firebaseio.com',
:log_method => method(:puts),
:auth => false
@top_stories = []
topstories_service = f.event_source('v0/topstories').onmessage do |event, data|
puts "Recived #{event}"
if event == 'put'
stories = data['data']
new_stories = stories.select{|s| !@top_stories.include? s}
puts "New top stories: " + new_stories.join(', ') if new_stories.length > 0
@top_stories = stories
end
end
update_service = f.event_source('v0/updates').onmessage do |event, data|
data['data']['items'].map{|i| f.get("v0/item/#{i}")}.each do |result|
case result['type']
when 'story'
puts "Updated story: #{result['title']} #{result['url']}"
when 'comment'
puts "Updated comment by #{result['by']}: #{result['text']}"
end
end
end
begin
topstories_service.start
update_service.start
sleep(1) while true
rescue SystemExit, Interrupt
puts "\nClosing connection..."
topstories_service.close
update_service.close
exit(0)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.