Class: Lita::Handlers::Regexcellent

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

Defined Under Namespace

Classes: InvalidTimeFormatError

Constant Summary collapse

DEFAULT_RANGE =
{
  since: "1 week ago",
  until: "now"
}
MESSAGES =
{
  found: "Found %{count} results for `/%{regex_string}/` since *%{oldest}* until *%{latest}*.",
  invalid_time_format: "Couldn't understand `since:%{oldest} until:%{latest}`."
}

Instance Method Summary collapse

Instance Method Details

#count(response) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/lita/handlers/regexcellent.rb', line 25

def count(response)
  data = {
    oldest: time_string_for('since', response) || DEFAULT_RANGE[:since],
    latest: time_string_for('until', response) || DEFAULT_RANGE[:until],
    regex_string: response.matches.first.first.tr('/', '')
  }

  begin
    messages = fetch_slack_message_history(response.room.id, data[:oldest], data[:latest])
    regex = Regexp.new data[:regex_string]
    reply = MESSAGES[:found] % data.merge({
      count: count_messages(messages, regex)
    })
  rescue InvalidTimeFormatError
    reply = MESSAGES[:invalid_time_format] % {
      oldest: data[:oldest].tr(" ", "_"),
      latest: data[:latest].tr(" ", "_")
    }
  end
  response.reply(reply)
end