Class: Lita::Handlers::Pagerduty

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

Constant Summary collapse

COMMANDS_PATH =
File.read("#{File.dirname(__FILE__)}/commands.yml")
COMMANDS =
YAML.safe_load(COMMANDS_PATH)

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, message) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/lita/handlers/pagerduty.rb', line 23

def method_missing(method, message)
  super if COMMANDS.map { |i| i['method'] }.include? method
  response = Object.const_get(
    'Commands::' << method.to_s.split('_').map(&:capitalize).join
  ).send(:call, message, pagerduty, store)
  handle_response(message, response) if response
end

Instance Method Details

#handle_response(message, response) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/lita/handlers/pagerduty.rb', line 48

def handle_response(message, response)
  message.reply case response
                when String
                  response
                when Hash
                  t response[:message], response[:params]
                when Array
                  response.map { |item| t item[:message], item[:params] }
                          .join("\n")
                end
end

#pagerdutyObject



35
36
37
38
39
40
41
42
# File 'lib/lita/handlers/pagerduty.rb', line 35

def pagerduty
  @pagerduty ||= ::Pagerduty.new(
    http,
    config.api_key,
    config.email,
    config.teams
  )
end

#respond_to_missing?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/lita/handlers/pagerduty.rb', line 31

def respond_to_missing?(method, include_private = false)
  COMMANDS.map { |i| i['method'] }.include?(method) || super
end

#storeObject



44
45
46
# File 'lib/lita/handlers/pagerduty.rb', line 44

def store
  @store ||= Store.new(redis)
end