Module: Lita::Handler::Common

Defined in:
lib/lita/handler/common.rb

Overview

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

Since:

  • 4.0.0

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#redisRedis::Namespace (readonly)

A Redis::Namespace scoped to the handler.

Returns:

  • (Redis::Namespace)

Since:

  • 4.0.0



56
57
58
# File 'lib/lita/handler/common.rb', line 56

def redis
  @redis
end

#robotLita::Robot (readonly)

The running Robot instance.

Returns:

Since:

  • 4.0.0



60
61
62
# File 'lib/lita/handler/common.rb', line 60

def robot
  @robot
end

Class Method Details

.included(klass) ⇒ Object

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

Since:

  • 4.0.0



7
8
9
10
11
12
# 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

Instance Method Details

#after(interval) {|timer| ... } ⇒ void

This method returns an undefined value.

Invokes the given block after the given number of seconds.

Parameters:

  • interval (Integer)

    The number of seconds to wait before invoking the block.

Yield Parameters:

Since:

  • 3.0.0



73
74
75
# File 'lib/lita/handler/common.rb', line 73

def after(interval, &block)
  Thread.new { Timer.new(interval: interval, &block).start }
end

#configLita::Configuration, Lita::Config

The handler’s configuration object.

Returns:

Since:

  • 3.2.0



80
81
82
83
84
# 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) {|timer| ... } ⇒ void

Note:

The block should call Timer#stop at a terminating condition to avoid infinite recursion.

This method returns an undefined value.

Invokes the given block repeatedly, waiting the given number of seconds between each invocation.

Parameters:

  • interval (Integer)

    The number of seconds to wait before each invocation of the block.

Yield Parameters:

Since:

  • 3.0.0



94
95
96
# 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 = {}) {|builder| ... } ⇒ Faraday::Connection

Creates a new Faraday::Connection for making HTTP requests.

Parameters:

  • options (Hash) (defaults to: {})

    A set of options passed on to Faraday.

Yields:

  • (builder)

    A Faraday builder object for adding middleware.

Returns:

  • (Faraday::Connection)

    The new connection object.

Since:

  • 4.0.0



102
103
104
105
106
107
108
109
110
# 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

#initialize(robot) ⇒ Object

Parameters:

Since:

  • 4.0.0



63
64
65
66
# File 'lib/lita/handler/common.rb', line 63

def initialize(robot)
  @robot = robot
  @redis = Redis::Namespace.new(redis_namespace, redis: Lita.redis)
end

#logLita::Logger

The Lita logger.

Returns:

Since:

  • 3.2.0



115
116
117
# File 'lib/lita/handler/common.rb', line 115

def log
  Lita.logger
end

#render_template(template_name, variables = {}) ⇒ String

Render an ERB template to a string, with any provided variables made available to the template.

Parameters:

  • template_name (String)

    The name of the ERB template file.

  • variables (Hash) (defaults to: {})

    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.

Returns:

  • (String)

    The rendered template.

Since:

  • 4.2.0



127
128
129
# 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 = {}) ⇒ String

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.

Parameters:

  • template_name (String)

    The name of the ERB template file.

  • helpers (Array<Module>)

    An array of modules whose methods should be added to the template evaluation context.

  • variables (Hash) (defaults to: {})

    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.

Returns:

  • (String)

    The rendered template.

Since:

  • 4.5.0



141
142
143
144
145
# 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

#translate(*args) ⇒ Object Also known as: t

See Also:

Since:

  • 4.0.0



148
149
150
# File 'lib/lita/handler/common.rb', line 148

def translate(*args)
  self.class.translate(*args)
end