Skip to content

Instantly share code, notes, and snippets.

@closer
Created April 24, 2010 12:59
Show Gist options
  • Select an option

  • Save closer/377633 to your computer and use it in GitHub Desktop.

Select an option

Save closer/377633 to your computer and use it in GitHub Desktop.
require 'net/http'
require 'uri'
require 'rubygems'
require 'json'
module Twitter
class UserStreams
BASE_URI = URI.parse('http://chirpstream.twitter.com/')
def initialize username, password
@username = username
@password = password
end
def start
raise "No block given." unless block_given?
begin
http.request(request) do |response|
puts "connected..."
response.read_body do |chunk|
status = JSON.parse(chunk) rescue next
#next unless status['text']
yield status
end
end
rescue TimeoutError => e
puts "timeout... reconect"
retry
end
self
end
private
def http
Net::HTTP.new(BASE_URI.host, BASE_URI.port)
end
def request
req = Net::HTTP::Post.new("/2b/user.json")
req.basic_auth(@username, @password)
req
end
end
module Streaming
USERNAME = ''
PASSWORD = ''
BASE_URI = URI.parse('http://stream.twitter.com/')
class API
class << self
def start point='sample', options={}, &block
raise "No block given." unless block_given?
new(options.merge({:point => point})).start &block
end
end
def initialize opts={}
options=opts
end
def options= opts={}
@username = opts[:username] || USERNAME
@password = opts[:password] || PASSWORD
@point = opts[:point] || 'sample'
@query = opts[:query] || nil
end
def start
raise "No block given." unless block_given?
http.request(request) do |response|
response.read_body do |chunk|
status = JSON.parse(chunk) rescue next
next unless status['text']
yield status
end
end
self
end
private
def http
Net::HTTP.new(BASE_URI.host, BASE_URI.port)
end
def request
req = Net::HTTP::Post.new("/1/statuses/#{@point}.json")
req.basic_auth(@username, @password)
req.body = URI.encode(@query) if @query
req
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment