Class: Streamer

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Streamer

Returns a new instance of Streamer.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/oii_twitter_goodies/lib/streamer.rb', line 4

def initialize(opts={})
  opts = Hashie::Mash[opts]
  @stream_type = opts[:stream_type] || "track"
  @stream_conditions = opts[:stream_conditions] || "lol"
  @oauth_token = opts[:oauth_token]
  @oauth_token_secret = opts[:oauth_token_secret]
  @consumer_key = opts[:consumer_key]
  @consumer_secret = opts[:consumer_secret]
  TweetStream.configure do |config|
    config.consumer_key = @consumer_key
    config.consumer_secret = @consumer_secret
    config.oauth_token = @oauth_token
    config.oauth_token_secret = @oauth_token_secret
  end
  @client = TweetStream::Client.new
  self.send("prepare_#{@stream_type}_variables")
end

Instance Attribute Details

#consumer_keyObject

Returns the value of attribute consumer_key.



2
3
4
# File 'lib/oii_twitter_goodies/lib/streamer.rb', line 2

def consumer_key
  @consumer_key
end

#consumer_secretObject

Returns the value of attribute consumer_secret.



2
3
4
# File 'lib/oii_twitter_goodies/lib/streamer.rb', line 2

def consumer_secret
  @consumer_secret
end

#oauth_tokenObject

Returns the value of attribute oauth_token.



2
3
4
# File 'lib/oii_twitter_goodies/lib/streamer.rb', line 2

def oauth_token
  @oauth_token
end

#oauth_token_secretObject

Returns the value of attribute oauth_token_secret.



2
3
4
# File 'lib/oii_twitter_goodies/lib/streamer.rb', line 2

def oauth_token_secret
  @oauth_token_secret
end

#stream_conditionsObject

Returns the value of attribute stream_conditions.



2
3
4
# File 'lib/oii_twitter_goodies/lib/streamer.rb', line 2

def stream_conditions
  @stream_conditions
end

#stream_typeObject

Returns the value of attribute stream_type.



2
3
4
# File 'lib/oii_twitter_goodies/lib/streamer.rb', line 2

def stream_type
  @stream_type
end

Instance Method Details

#prepare_follow_variablesObject



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/oii_twitter_goodies/lib/streamer.rb', line 22

def prepare_follow_variables
  raise "Stream Conditions can't be nil!" if @stream_conditions.nil?
  raise "Stream Conditions must be a comma-separated string or array!" if ![String, Array].include?(@stream_conditions.class)
  @stream_conditions = @stream_conditions.split(",") if @stream_conditions.class == String
  sc_count = @stream_conditions.count
  @stream_conditions = @stream_conditions.collect(&:to_i)
  @stream_conditions = @stream_conditions.select{|sc| sc != 0}
  raise "#{sc_count-@stream_conditions.length} values were dropped from stream conditions as they were not integers. You must specify integers (representing valid user IDs on Twitter) for this to work" if sc_count != @stream_conditions.length
  raise "Stream conditions are empty!" if @stream_conditions.length == 0
  @stream_conditions
end

#prepare_locations_variablesObject



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/oii_twitter_goodies/lib/streamer.rb', line 42

def prepare_locations_variables
  new_value = []
  @stream_conditions.split(",").each_slice(4) do |lat_lon_pair|
    boundings = lat_lon_pair.collect{|b| b.to_f}
    raise "Must input two pairs of numbers, separated by commas." if boundings.length!=4
    raise "Latitudes are out of range (max 90 degrees)" if boundings[1].abs>90 || boundings[3].abs>90
    raise "Longitudes are out of range (max 180 degrees)" if boundings[0].abs>180 || boundings[2].abs>180
    new_value << boundings
  end
  @stream_conditions = new_value.flatten.collect(&:to_f )
  
end

#prepare_track_variablesObject



34
35
36
37
38
39
40
# File 'lib/oii_twitter_goodies/lib/streamer.rb', line 34

def prepare_track_variables
  raise "Stream Conditions can't be nil!" if @stream_conditions.nil?
  raise "Stream Conditions must be a comma-separated string or array!" if ![String, Array].include?(@stream_conditions.class)
  raise "Stream conditions are empty!" if @stream_conditions.length == 0
  @stream_conditions = @stream_conditions.split(",") if @stream_conditions.class == String
  @stream_conditions
end

#stream(&block) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/oii_twitter_goodies/lib/streamer.rb', line 55

def stream(&block)
  EM.run do
    @client.on_error {|msg| puts msg }
    @client.on_limit {|skip_count| puts "limit reached: #{skip_count} skipped"}
    @client.send(@stream_type, @stream_conditions) do |status|
      block.call status
    end
  end
end