Class: EurekaBot::Controller

Inherits:
Object
  • Object
show all
Extended by:
ActiveSupport::Autoload
Includes:
ActiveSupport::Callbacks, Instrumentation
Defined in:
lib/eureka_bot/controller.rb

Defined Under Namespace

Classes: Response, UnknownAction

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Instrumentation

#instrument, prefix

Constructor Details

#initialize(params: {}, message: nil, response: nil, logger: EurekaBot.logger) ⇒ Controller

cattr_accessor :exception_handler



13
14
15
16
17
18
19
# File 'lib/eureka_bot/controller.rb', line 13

def initialize(params: {}, message: nil, response: nil, logger: EurekaBot.logger)
  @params   = params
  @message  = message
  @logger   = logger
  @response = response || response_class.new(logger: logger)
  @response.controller = self
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



10
11
12
# File 'lib/eureka_bot/controller.rb', line 10

def logger
  @logger
end

#messageObject (readonly)

Returns the value of attribute message.



10
11
12
# File 'lib/eureka_bot/controller.rb', line 10

def message
  @message
end

#paramsObject (readonly)

Returns the value of attribute params.



10
11
12
# File 'lib/eureka_bot/controller.rb', line 10

def params
  @params
end

#responseObject (readonly)

Returns the value of attribute response.



10
11
12
# File 'lib/eureka_bot/controller.rb', line 10

def response
  @response
end

Class Method Details

.has_action?(action) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
65
# File 'lib/eureka_bot/controller.rb', line 62

def self.has_action?(action)
  return false unless action.present?
  public_instance_methods(false).include?(action.to_sym)
end

Instance Method Details

#answer(**params) ⇒ Object



34
35
36
37
38
39
# File 'lib/eureka_bot/controller.rb', line 34

def answer(**params)
  instrument 'controller.answer', params do
    response << params
    nil
  end
end

#execute(action) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/eureka_bot/controller.rb', line 21

def execute(action)
  if respond_to?(action, include_all: false)
    instrument 'controller.execute' do
      run_callbacks :action do
        public_send(action)
      end
    end
  else
    raise UnknownAction.new("Action #{action} is not defined in #{self.class}")
  end
  self
end

#redirect(controller, action) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/eureka_bot/controller.rb', line 41

def redirect(controller, action)
  instance = controller.new(
      params:   params,
      message:  message,
      response: response,
      logger:   logger
  )
  instrument 'controller.redirect', {from: self.class.to_s, to: {controller: controller.to_s, action: action}} do
    instance.public_send(action)
  end
end

#response_classObject



53
54
55
# File 'lib/eureka_bot/controller.rb', line 53

def response_class
  EurekaBot::Controller::Response
end

#trace_error(e, params = {}) ⇒ Object



57
58
59
60
# File 'lib/eureka_bot/controller.rb', line 57

def trace_error(e, params={})
  logger.error(e.http_body) if e.respond_to?(:http_body)
  EurekaBot.exception_handler(e, self.class, params)
end