Class: Lita::Handlers::CustomMeme

Inherits:
Handler
  • Object
show all
Defined in:
lib/lita/handlers/custom_meme.rb

Constant Summary collapse

REDIS_KEY =
"cmeme"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.default_config(config) ⇒ Object



32
33
34
# File 'lib/lita/handlers/custom_meme.rb', line 32

def self.default_config(config)
  config.command_only = false
end

Instance Method Details

#meme_add(response) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/lita/handlers/custom_meme.rb', line 60

def meme_add(response)
  name, image = response.matches.first

  name = normalize_key(name)
  redis.hset(REDIS_KEY, name, image)

  response.reply "Meme '#{name}' has been added."
end

#meme_delete(response) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/lita/handlers/custom_meme.rb', line 69

def meme_delete(response)
  key = normalize_key(response.matches.first.first)

  if redis.hdel(REDIS_KEY, key) >= 1
    response.reply "Deleted meme '#{key}'."
  else
    response.reply "Meme '#{key}' was not found."
  end
end

#meme_image(response) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/lita/handlers/custom_meme.rb', line 36

def meme_image(response)
  # If command only is desired, then bail if general message
  return if config.command_only && !response.message.command?

  output = []

  response.matches.each do |match|
    term = normalize_key(match[0])

    image = redis.hget(REDIS_KEY, term)

    if image
      output << image
    end

  end

  if output.size > 0
    response.reply *output
  elsif response.message.command?
    response.reply "Meme not found"
  end
end

#meme_list(response) ⇒ Object



79
80
81
82
83
84
85
86
87
# File 'lib/lita/handlers/custom_meme.rb', line 79

def meme_list(response)
  keys = redis.hkeys(REDIS_KEY)

  if keys.empty?
    response.reply "No memes have been added"
  else
    response.reply "Available memes: #{keys.sort.join(', ')}"
  end
end