Class: Ruboty::Handlers::TwitterTrack

Inherits:
Base
  • Object
show all
Defined in:
lib/ruboty/handlers/twitter_track.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ TwitterTrack

Returns a new instance of TwitterTrack.



16
17
18
19
20
21
# File 'lib/ruboty/handlers/twitter_track.rb', line 16

def initialize(*args)
  super

  @stream = Ruboty::TwitterTrack::Stream.new(robot)
  @stream.start(cache[:message], cache[:terms])
end

Instance Method Details

#cacheObject



67
68
69
70
71
72
73
74
# File 'lib/ruboty/handlers/twitter_track.rb', line 67

def cache
  unless robot.brain.data[Ruboty::TwitterTrack::NAMESPACE]
    status = { message: nil, terms: {} }
    robot.brain.data[Ruboty::TwitterTrack::NAMESPACE] = status
  end

  robot.brain.data[Ruboty::TwitterTrack::NAMESPACE]
end

#track(message) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ruboty/handlers/twitter_track.rb', line 23

def track(message)
  cache[:message] = message.original.except(:robot)

  message[:term].split(',').each do |term|
    key   = generate_id
    words = term.strip.split(/\s+/)
    cache[:terms][key] = words
  end

  begin
    @stream.restart(cache[:message], cache[:terms].values)
    message.reply("Tracked '#{message[:term]}'.")
  rescue Twitter::Error::Forbidden
    message.reply("Unable to verify your credentials.")
  end
end

#tracking(message) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/ruboty/handlers/twitter_track.rb', line 58

def tracking(message)
  if cache[:terms].empty?
    message.reply("Tracking no terms.")
  else
    response = cache[:terms].map { |key, words| "#{key}: #{words.join(' ')}" }
    message.reply(response.join("\n"), code:true)
  end
end

#untrack(message) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ruboty/handlers/twitter_track.rb', line 40

def untrack(message)
  cache[:message] = message.original.except(:robot)

  key   = message[:id].to_i
  words = cache[:terms].delete(key)
  unless words
    message.reply("'#{key}' not found.")
    return
  end

  begin
    @stream.restart(cache[:message], cache[:terms].values)
    message.reply("Untracked '#{key}: #{words.join(' ')}'.")
  rescue Twitter::Error::Forbidden
    message.reply("Unable to verify your credentials.")
  end
end