Class: TwitterStream

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

Constant Summary collapse

VERSION =
'0.1.1'
@@urls =
{
    'sample' => URI.parse("http://stream.twitter.com/1/statuses/sample.json"),
    'filter' => URI.parse("http://stream.twitter.com/1/statuses/filter.json")
}

Instance Method Summary collapse

Constructor Details

#initialize(username, password) ⇒ TwitterStream

Returns a new instance of TwitterStream.



28
29
30
31
32
# File 'lib/twitterstream.rb', line 28

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

Instance Method Details

#filter(params = nil) ⇒ Object

Raises:

  • (ArgumentError)


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

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)


60
61
62
63
64
65
66
67
68
69
70
# File 'lib/twitterstream.rb', line 60

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)


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

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)


48
49
50
51
52
53
54
55
56
57
58
# File 'lib/twitterstream.rb', line 48

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