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.
Defined Under Namespace
Modules: ClassMethods
Instance Attribute Summary collapse
-
#redis ⇒ Redis::Namespace
readonly
A Redis::Namespace scoped to the handler.
-
#robot ⇒ Lita::Robot
readonly
The running Robot instance.
Class Method Summary collapse
-
.included(klass) ⇒ Object
Adds common functionality to the class and initializes the handler’s configuration builder.
Instance Method Summary collapse
-
#after(interval) {|timer| ... } ⇒ void
Invokes the given block after the given number of seconds.
-
#config ⇒ Lita::Configuration, Lita::Config
The handler’s configuration object.
-
#every(interval) {|timer| ... } ⇒ void
Invokes the given block repeatedly, waiting the given number of seconds between each invocation.
-
#http(options = {}) {|builder| ... } ⇒ Faraday::Connection
Creates a new
Faraday::Connection
for making HTTP requests. - #initialize(robot) ⇒ Object
-
#log ⇒ Lita::Logger
The Lita logger.
-
#render_template(template_name, variables = {}) ⇒ String
Render an ERB template to a string, with any provided variables made available to the template.
-
#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.
- #translate(*args) ⇒ Object (also: #t)
Instance Attribute Details
#redis ⇒ Redis::Namespace (readonly)
A Redis::Namespace scoped to the handler.
56 57 58 |
# File 'lib/lita/handler/common.rb', line 56 def redis @redis end |
#robot ⇒ Lita::Robot (readonly)
The running Robot instance.
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.
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.
73 74 75 |
# File 'lib/lita/handler/common.rb', line 73 def after(interval, &block) Thread.new { Timer.new(interval: interval, &block).start } end |
#config ⇒ Lita::Configuration, Lita::Config
The handler’s configuration object.
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
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.
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.
102 103 104 105 106 107 108 109 110 |
# File 'lib/lita/handler/common.rb', line 102 def http( = {}, &block) = .merge() if block Faraday::Connection.new(nil, , &block) else Faraday::Connection.new(nil, ) end end |
#initialize(robot) ⇒ Object
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 |
#log ⇒ Lita::Logger
The Lita logger.
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.
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.
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
148 149 150 |
# File 'lib/lita/handler/common.rb', line 148 def translate(*args) self.class.translate(*args) end |