Class: TwitterStream

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

Constant Summary collapse

VERSION =
'0.2.1'
@@urls =
{
  'sample' => URI.parse("http://stream.twitter.com/1/statuses/sample.json"),
  'filter' => URI.parse("http://stream.twitter.com/1/statuses/filter.json"),
  'userstreams' => URI.parse('https://userstream.twitter.com/2/user.json?replies=all'),
}

Instance Method Summary collapse

Constructor Details

#initialize(params = { }) ⇒ TwitterStream

Returns a new instance of TwitterStream.



30
31
32
33
34
35
36
37
38
39
# File 'lib/twitterstream.rb', line 30

def initialize(params={ })
  if params[:username] && params[:password]
    @username = params[:username]
    @password = params[:password]
  else
    @consumer = OAuth::Consumer.new(params[:consumer_token], params[:consumer_secret])
    @access = OAuth::Token.new(params[:access_token], params[:access_secret])
  end
  self
end

Instance Method Details

#filter(params = nil) ⇒ Object

Raises:

  • (ArgumentError)


48
49
50
51
52
53
# File 'lib/twitterstream.rb', line 48

def filter(params=nil)
  raise ArgumentError, "params is not hash" unless params.nil? || params.kind_of?(Hash)
  start_stream('filter', params) do |status|
    yield status
  end
end

#follow(follow, params = nil) ⇒ Object

Raises:

  • (ArgumentError)


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

def follow(follow, params=nil)
  raise ArgumentError, "follow is not array or string" unless follow.kind_of?(Array) || follow.kind_of?(String)
  raise ArgumentError, "params is not hash" unless params.nil? || params.kind_of?(Hash)
  
  p = { 'follow' => follow.kind_of?(Array) ? follow.join(",") : follow }
  p.merge!(params) if params
  
  start_stream('filter', p) do |status|
    yield status
  end
end

#sample(params = nil) ⇒ Object

Raises:

  • (ArgumentError)


41
42
43
44
45
46
# File 'lib/twitterstream.rb', line 41

def sample(params=nil)
  raise ArgumentError, "params is not hash" unless params.nil? || params.kind_of?(Hash)
  start_stream('sample', params) do |status|
    yield status
  end
end

#track(track, params = nil) ⇒ Object

Raises:

  • (ArgumentError)


55
56
57
58
59
60
61
62
63
64
65
# File 'lib/twitterstream.rb', line 55

def track(track, params=nil)
  raise ArgumentError, "track is not array or string" unless track.kind_of?(Array) || track.kind_of?(String)
  raise ArgumentError, "params is not hash" unless params.nil? || params.kind_of?(Hash)
  
  p = { 'track' => track.kind_of?(Array) ? track.map{|x| raise ArgumentError, "track item is not string or integer!" unless x.kind_of?(String) || x.kind_of?(Integer); x.kind_of?(Integer) ? x.to_s : x }.join(",") : track }
  
  p.merge!('filter',params) if params
  start_stream('filter', p) do |status|
    yield status
  end
end

#userstreams(params = nil) ⇒ Object

Raises:

  • (ArgumentError)


79
80
81
82
83
84
# File 'lib/twitterstream.rb', line 79

def userstreams(params=nil)
  raise ArgumentError, "params is not hash" unless params.nil? || params.kind_of?(Hash)
  start_stream('userstreams', params) do |status|
    yield status
  end
end