module Lita::Handler::Common
Methods included in any class that includes at least one type of router. @since 4.0.0
Attributes
A Redis::Namespace scoped to the handler. @return [Redis::Namespace]
The running {Lita::Robot} instance. @return [Lita::Robot]
Public Class Methods
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
@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
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
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
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
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
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
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
@see .translate
# File lib/lita/handler/common.rb, line 148 def translate(*args) self.class.translate(*args) end
Private Instance Methods
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 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
The handler's namespace for Redis.
# File lib/lita/handler/common.rb, line 171 def redis_namespace "handlers:#{self.class.namespace}" end