Skip to content

Instantly share code, notes, and snippets.

@julien51
Created January 25, 2009 01:57
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 julien51/51639 to your computer and use it in GitHub Desktop.
Save julien51/51639 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'eventmachine'
require 'open-uri'
require 'libxml'
class XmlHandler
include LibXML::XML::SaxParser::Callbacks
def initialize()
@parser = LibXML::XML::SaxParser.new(LibXML::XML::Parser::Context.new)
@parser.callbacks = self
end
def parse(data)
puts data
LibXML::XML::SaxParser.string(data)
@parser.parse
end
def on_error(error)
puts "#{error}"
end
def on_start_element(element, attributes = {})
end
def on_end_element(element)
end
def on_characters(text)
end
def method_missing(method_name, *attributes, &block)
puts method_name + " missing"
end
end
class AtomStreamHandler < EventMachine::Connection
def initialize(params)
@uri = params[:uri]
@callback = params[:callback]
@elem = nil
@xml_handler = XmlHandler.new()
@started = false
super
end
def post_init
send_data("GET #{@uri.request_uri} HTTP/1.0\r\n" +
"Host: #{@uri.host}\r\n" +
"\r\n")
end
def receive_data(data)
data.each_line do |line|
@xml_handler.parse(line) if @started
@started = true if line =~ /\A\r\n/
end
end
end
EventMachine::run do
@uri = URI::parse("http://updates.sixapart.com/atom-stream.xml")
EventMachine::connect @uri.host, @uri.port, AtomStreamHandler, {:uri => @uri}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment