Class: Ruboty::Handlers::TwitterTrack
- Inherits:
-
Base
- Object
- Base
- Ruboty::Handlers::TwitterTrack
- Defined in:
- lib/ruboty/handlers/twitter_track.rb
Instance Method Summary collapse
- #cache ⇒ Object
-
#initialize(*args) ⇒ TwitterTrack
constructor
A new instance of TwitterTrack.
- #track(message) ⇒ Object
- #tracking(message) ⇒ Object
- #untrack(message) ⇒ Object
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
#cache ⇒ Object
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() cache[:message] = .original.except(:robot) [: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) .reply("Tracked '#{[:term]}'.") rescue Twitter::Error::Forbidden .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() if cache[:terms].empty? .reply("Tracking no terms.") else response = cache[:terms].map { |key, words| "#{key}: #{words.join(' ')}" } .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() cache[:message] = .original.except(:robot) key = [:id].to_i words = cache[:terms].delete(key) unless words .reply("'#{key}' not found.") return end begin @stream.restart(cache[:message], cache[:terms].values) .reply("Untracked '#{key}: #{words.join(' ')}'.") rescue Twitter::Error::Forbidden .reply("Unable to verify your credentials.") end end |