Skip to content

Instantly share code, notes, and snippets.

@kmcd
Created April 26, 2010 16:19
Show Gist options
  • Save kmcd/379541 to your computer and use it in GitHub Desktop.
Save kmcd/379541 to your computer and use it in GitHub Desktop.
require 'socket'
require 'psych'
class StreamClient
def initialize user, pass
@ba = ["#{user}:#{pass}"].pack('m').chomp
end
def listen listener
socket = TCPSocket.new 'stream.twitter.com', 80
socket.write "GET /1/statuses/sample.json HTTP/1.1\r\n"
socket.write "Host: stream.twitter.com\r\n"
socket.write "Authorization: Basic #{@ba}\r\n"
socket.write "\r\n"
# Read the headers
while((line = socket.readline) != "\r\n"); puts line if $DEBUG; end
reader, writer = IO.pipe
producer = Thread.new(socket, writer) do |s, io|
loop do
io.write "---\n"
io.write s.read s.readline.strip.to_i 16
io.write "...\n"
s.read 2 # strip the blank line
end
end
parser = Psych::Parser.new listener
parser.parse reader
producer.join
end
end
class Listener < Psych::Handler
def initialize
@was_text = false
end
def scalar value, anchor, tag, plain, quoted, style
puts value if @was_text
@was_text = value == 'text'
end
end
StreamClient.new(ARGV[0], ARGV[1]).listen Listener.new
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment