module Lita::Handler::Common

Methods included in any class that includes at least one type of router. @since 4.0.0

Attributes

redis[R]

A Redis::Namespace scoped to the handler. @return [Redis::Namespace]

robot[R]

The running {Lita::Robot} instance. @return [Lita::Robot]

Public Class Methods

included(klass) click to toggle source

Adds common functionality to the class and initializes the handler's configuration builder.

# File lib/lita/handler/common.rb, line 7
def self.included(klass)
  klass.extend(ClassMethods)
  klass.extend(Namespace)
  klass.extend(Configurable)
  klass.configuration_builder = ConfigurationBuilder.new
end
new(robot) click to toggle source

@param robot [Lita::Robot] The currently running robot.

# File lib/lita/handler/common.rb, line 63
def initialize(robot)
  @robot = robot
  @redis = Redis::Namespace.new(redis_namespace, redis: Lita.redis)
end

Public Instance Methods

after(interval, &block) click to toggle source

Invokes the given block after the given number of seconds. @param interval [Integer] The number of seconds to wait before invoking the block. @yieldparam timer [Lita::Timer] The current {Lita::Timer} instance. @return [void] @since 3.0.0

# File lib/lita/handler/common.rb, line 73
def after(interval, &block)
  Thread.new { Timer.new(interval: interval, &block).start }
end
config() click to toggle source

The handler's configuration object. @return [Lita::Configuration, Lita::Config] The handler's configuration object. @since 3.2.0

# File lib/lita/handler/common.rb, line 80
def config
  if robot.config.handlers.respond_to?(self.class.namespace)
    robot.config.handlers.public_send(self.class.namespace)
  end
end
every(interval, &block) click to toggle source

Invokes the given block repeatedly, waiting the given number of seconds between each invocation. @param interval [Integer] The number of seconds to wait before each invocation of the block. @yieldparam timer [Lita::Timer] The current {Lita::Timer} instance. @return [void] @note The block should call {Lita::Timer#stop} at a terminating condition to avoid infinite

recursion.

@since 3.0.0

# File lib/lita/handler/common.rb, line 94
def every(interval, &block)
  Thread.new { Timer.new(interval: interval, recurring: true, &block).start }
end
http(options = {}, &block) click to toggle source

Creates a new Faraday::Connection for making HTTP requests. @param options [Hash] A set of options passed on to Faraday. @yield [builder] A Faraday builder object for adding middleware. @return [Faraday::Connection] The new connection object.

# File lib/lita/handler/common.rb, line 102
def http(options = {}, &block)
  options = default_faraday_options.merge(options)

  if block
    Faraday::Connection.new(nil, options, &block)
  else
    Faraday::Connection.new(nil, options)
  end
end
log() click to toggle source

The Lita logger. @return [Lita::Logger] The Lita logger. @since 3.2.0

# File lib/lita/handler/common.rb, line 115
def log
  Lita.logger
end
render_template(template_name, variables = {}) click to toggle source

Render an ERB template to a string, with any provided variables made available to the template. @param template_name [String] The name of the ERB template file. @param variables [Hash] An optional hash of variables to make available within the

template. Hash keys become instance variable names with the hash values as the instance
variables' values.

@return [String] The rendered template. @since 4.2.0

# File lib/lita/handler/common.rb, line 127
def render_template(template_name, variables = {})
  Template.from_file(file_for_template(template_name)).render(variables)
end
render_template_with_helpers(template_name, helpers, variables = {}) click to toggle source

Renders an ERB template to a string, like {#render_template}, but also takes an array of modules with helper methods to be made available to the template. @param template_name [String] The name of the ERB template file. @param helpers [Array<Module>] An array of modules whose methods should be added to the

template evaluation context.

@param variables [Hash] An optional hash of variables to make available within the

template. Hash keys become instance variable names with the hash values as the instance
variables' values.

@return [String] The rendered template. @since 4.5.0

# File lib/lita/handler/common.rb, line 141
def render_template_with_helpers(template_name, helpers, variables = {})
  template = Template.from_file(file_for_template(template_name))
  helpers.each { |helper| template.add_helper(helper) }
  template.render(variables)
end
t(*args)
Alias for: translate
translate(*args) click to toggle source

@see .translate

# File lib/lita/handler/common.rb, line 148
def translate(*args)
  self.class.translate(*args)
end
Also aliased as: t

Private Instance Methods

default_faraday_options() click to toggle source

Default options for new Faraday connections. Sets the user agent to the current version of Lita.

# File lib/lita/handler/common.rb, line 158
def default_faraday_options
  { headers: { "User-Agent" => "Lita v#{VERSION}" } }
end
file_for_template(template_name) click to toggle source
# File lib/lita/handler/common.rb, line 162
def file_for_template(template_name)
  TemplateResolver.new(
    self.class.template_root,
    template_name,
    robot.config.robot.adapter
  ).resolve
end
redis_namespace() click to toggle source

The handler's namespace for Redis.

# File lib/lita/handler/common.rb, line 171
def redis_namespace
  "handlers:#{self.class.namespace}"
end