Class: TwitterStream::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/twitterstream.rb

Instance Method Summary collapse

Constructor Details

#initialize(username, password) ⇒ Client

Returns a new instance of Client.



25
26
27
28
29
# File 'lib/twitterstream.rb', line 25

def initialize(username, password)
  @username = username
  @password = password
  self
end

Instance Method Details

#filter(params = nil) ⇒ Object



52
53
54
55
56
57
# File 'lib/twitterstream.rb', line 52

def filter(params=nil)
  uri = URI.parse("http://stream.twitter.com/1/statuses/filter.json")
  start_stream(uri, params) do |status|
    yield status
  end
end

#follow(follow, params = nil) ⇒ Object



68
69
70
71
72
73
74
75
# File 'lib/twitterstream.rb', line 68

def follow(follow, params=nil)
  uri = URI.parse("http://stream.twitter.com/1/statuses/filter.json")
  p = {'follow'=>follow}
  p.merge!(params) if params
  start_stream(uri, p) do |status|
    yield status
  end
end

#sample(params = nil) ⇒ Object



45
46
47
48
49
50
# File 'lib/twitterstream.rb', line 45

def sample(params=nil)
  uri = URI.parse("http://stream.twitter.com/1/statuses/sample.json")
  start_stream(uri, params) do |status|
    yield status
  end
end

#start_stream(uri, params = nil) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/twitterstream.rb', line 31

def start_stream(uri, params=nil)
  Net::HTTP.start(uri.host, uri.port) do |http|
    request = Net::HTTP::Post.new(uri.request_uri)
    request.set_form_data(params) if params
    request.basic_auth(@username, @password)
    http.request(request) do |response|
      response.each_line("\r\n") do |line|
        status = JSON.parse(line) rescue next
        yield status
      end
    end
  end
end

#track(track, params = nil) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/twitterstream.rb', line 59

def track(track, params=nil)
  uri = URI.parse("http://stream.twitter.com/1/statuses/filter.json")
  p = {'track'=>track}
  p.merge!(params) if params
  start_stream(uri, p) do |status|
    yield status
  end
end