Class: Lita::Handlers::WhoHas

Inherits:
Handler
  • Object
show all
Includes:
Utils::LitaWhoHas::Persistence
Defined in:
lib/lita/handlers/who_has.rb

Overview

The main handler for the WhoHas plugin

Instance Method Summary collapse

Instance Method Details

#claim_thing(response) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/lita/handlers/who_has.rb', line 60

def claim_thing(response)
  thing_id = response.matches.first.first
  case current_user(thing_id)
  when nil
    redis.hset(key(thing_id), 'user', response.user.name)
    response.reply t('claim_thing.success')
  when ''
    redis.hset(key(thing_id), 'user', response.user.name)
    response.reply t('claim_thing.success')
  when response.user.name
    response.reply t(
      'claim_thing.failure.thing_in_use_by_user', thing_id: thing_id
    )
  else
    response.reply t(
      'claim_thing.failure.thing_in_use_by_other_user',
      thing_id: thing_id,
      user: current_user(thing_id)
    )
  end
end

#claim_used_thing(response) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/lita/handlers/who_has.rb', line 150

def claim_used_thing(response)
  thing_id, specified_user = response.matches.first
  case current_user(thing_id)
  when nil
    response.reply t(
      'claim_used_thing.failure.thing_unknown',
      thing_id: thing_id
    )
  when ''
    response.reply t(
      'claim_used_thing.failure.thing_not_in_use',
      thing_id: thing_id
    )
  when response.user.name
    response.reply t(
      'claim_used_thing.failure.thing_in_use_by_user',
      thing_id: thing_id
    )
  when specified_user
    redis.hset(key(thing_id), 'user', response.user.name)
    response.reply t('claim_used_thing.success')
  else
    response.reply t(
      'claim_used_thing.failure.thing_in_use_by_user_other',
      thing_id: thing_id,
      user: current_user(thing_id),
      specified_user: specified_user
    )
  end
end

#describe_thing(response) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/lita/handlers/who_has.rb', line 181

def describe_thing(response)
  thing_id, _, attribute, value = response.matches.first
  owner = current_user(thing_id)
  case owner
  when nil
    response.reply t(
      'describe_thing.failure.thing_unknown',
      thing_id: thing_id
    )
  when '', response.user.name
    if attribute && value
      describe(thing_id, attribute, value)
      response.reply t(
        'describe_thing.success', key: attribute, thing_id: thing_id
      )
    else
      response.reply t('describe_thing.description', thing_id: thing_id)
      response.reply('/code ' + full_description_json(thing_id))
    end
  else
    if attribute && value
      response.reply t(
        'describe_thing.failure.thing_in_use_by_other_user',
        thing_id: thing_id,
        user: owner
      )
    else
      response.reply t('describe_thing.description', thing_id: thing_id)
      response.reply('/code ' + full_description_json(thing_id))
    end
  end
end

#forget_thing(response) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/lita/handlers/who_has.rb', line 125

def forget_thing(response)
  thing_id = response.matches.first.first
  case current_user(thing_id)
  when nil
    response.reply t(
      'forget_thing.failure.thing_unknown',
      thing_id: thing_id
    )
  when ''
    redis.del(key(thing_id))
    response.reply t('forget_thing.success')
  when response.user.name
    response.reply t(
      'forget_thing.failure.thing_in_use_by_user',
      thing_id: thing_id
    )
  else
    response.reply t(
      'forget_thing.failure.thing_in_use_by_other_user',
      thing_id: thing_id,
      user: current_user(thing_id)
    )
  end
end

#list_things(response) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/lita/handlers/who_has.rb', line 107

def list_things(response)
  list_regex = response.matches.first.last
  list_regex ||= /.*/
  lines = []
  redis.keys(key('*')).sort.each do |key|
    thing_id = key.split(':').last
    user = redis.hget(key, 'user')
    line = thing_id
    line += " (#{user})" unless user.empty?
    lines << line if thing_id.match(list_regex)
  end
  if lines.any?
    response.reply(lines.join("\n"))
  else
    response.reply t('list_things.failure.no_things')
  end
end

#release_thing(response) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/lita/handlers/who_has.rb', line 82

def release_thing(response)
  thing_id = response.matches.first.first
  case current_user(thing_id)
  when nil
    response.reply t(
      'release_thing.failure.thing_unknown',
      thing_id: thing_id
    )
  when ''
    response.reply t(
      'release_thing.failure.thing_not_in_use_by_user',
      thing_id: thing_id
    )
  when response.user.name
    redis.hset(key(thing_id), 'user', nil)
    response.reply t('release_thing.success')
  else
    response.reply t(
      'release_thing.failure.thing_in_use_by_other_user',
      thing_id: thing_id,
      user: current_user(thing_id)
    )
  end
end