Class: Lita::Alias::ChatHandler

Inherits:
Handler
  • Object
show all
Defined in:
lib/lita/alias/chat_handler.rb

Overview

Chat handler for lita-alias

Instance Method Summary collapse

Instance Method Details

#add(response) ⇒ Object

Route Handlers



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/lita/alias/chat_handler.rb', line 46

def add(response)
  name = response.match_data[1]
  command = response.match_data[2]

  # TODO: support multiple commands
  ac = AliasedCommand.new(name, command)
  add_alias_route(ac)
  alias_store.add(ac)

  response.reply "Added alias '#{name}' for '#{command}'"
rescue AliasStore::AliasAddException => e
  response.reply(e.message)
end

#alias_storeObject

The repository where aliases are persisted

Returns:

  • AliasStore



91
92
93
# File 'lib/lita/alias/chat_handler.rb', line 91

def alias_store
  @alias_store ||= AliasStore.new(redis)
end

#delete(response) ⇒ Object



80
81
82
83
84
85
86
87
# File 'lib/lita/alias/chat_handler.rb', line 80

def delete(response)
  name = response.match_data[1]
  ac = alias_store.delete(name)
  delete_alias_route(ac)
  response.reply("Deleted alias '#{ac.name}' => '#{ac.command}'")
rescue AliasStore::AliasNotFoundException
  response.reply("Alias '#{name}' does not exist")
end

#foo(response) ⇒ Object



104
105
106
# File 'lib/lita/alias/chat_handler.rb', line 104

def foo(response)
  response.reply 'foo'
end

#list(response) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/lita/alias/chat_handler.rb', line 66

def list(response)
  aliases = alias_store.list

  if aliases.empty?
    message = 'No aliases have been saved'
  else
    message = aliases.map do |ac|
      "#{ac.name} => #{ac.command}"
    end
  end

  response.reply message
end

#load_aliases(payload) ⇒ Object

Event Handlers



36
37
38
39
40
41
# File 'lib/lita/alias/chat_handler.rb', line 36

def load_aliases(payload)
  log.debug("Loading aliases")
  alias_store.list.each do |ac|
    add_alias_route(ac)
  end
end

#test_alias(response) ⇒ Object



95
96
97
98
99
100
101
102
# File 'lib/lita/alias/chat_handler.rb', line 95

def test_alias(response)
  command = redis.hgetall(REDIS_ALIASES_KEY).first.last
  message = Lita::Message.new(robot, command, response.message.source)
  after(5) do
    response.reply "Sending #{command}"
    robot.receive(message)
  end
end

#trigger_alias(response) ⇒ Object



60
61
62
63
64
# File 'lib/lita/alias/chat_handler.rb', line 60

def trigger_alias(response)
  ac = alias_store.lookup(response.match_data[1])
  message = Lita::Message.new(robot, "#{robot.alias}#{ac.command}", response.message.source)
  robot.receive(message)
end