Class: Lita::Adapter
- Inherits:
-
Object
- Object
- Lita::Adapter
- Extended by:
- Configurable, Namespace
- Defined in:
- lib/lita/adapter.rb
Overview
Adapters are the glue between Lita’s API and a chat service.
Direct Known Subclasses
Constant Summary collapse
- REQUIRED_METHODS =
The names of methods that should be implemented by an adapter.
i( chat_service join part roster run set_topic shut_down ).freeze
Class Attribute Summary collapse
-
.required_configs ⇒ Array
readonly
deprecated
Deprecated.
Will be removed in Lita 5.0. Use #configuration_builder instead.
Instance Attribute Summary collapse
-
#robot ⇒ Lita::Robot
readonly
The instance of Robot.
Attributes included from Configurable
Class Method Summary collapse
-
.require_config(*keys) ⇒ void
(also: require_configs)
deprecated
Deprecated.
Will be removed in Lita 5.0. Use #config instead.
-
.t ⇒ String
Returns the translation for a key, automatically namespaced to the adapter.
-
.translate(key, hash = {}) ⇒ String
Returns the translation for a key, automatically namespaced to the adapter.
Instance Method Summary collapse
-
#chat_service ⇒ Object?
abstract
May return an object exposing chat-service-specific APIs.
-
#config ⇒ Lita::Configuration
The adapter’s configuration object.
-
#initialize(robot) ⇒ Adapter
constructor
A new instance of Adapter.
-
#join(room_id) ⇒ void
abstract
Joins the room with the specified ID.
-
#log ⇒ Lita::Logger
The Lita logger.
-
#mention_format(name) ⇒ String
Formats a name for “mentioning” a user in a group chat.
-
#part(room_id) ⇒ void
abstract
Parts from the room with the specified ID.
-
#roster(room) ⇒ Array<Lita::User>
abstract
Get a list of users that are online in the given room.
-
#run ⇒ void
abstract
The main loop.
-
#send_messages(target, strings) ⇒ void
abstract
Sends one or more messages to a user or room.
-
#set_topic(target, topic) ⇒ void
abstract
Sets the topic for a room.
-
#shut_down ⇒ void
abstract
Performs any clean up necessary when disconnecting from the chat service.
- #translate(*args) ⇒ Object (also: #t)
Methods included from Namespace
Methods included from Configurable
Constructor Details
#initialize(robot) ⇒ Adapter
Returns a new instance of Adapter.
57 58 59 60 |
# File 'lib/lita/adapter.rb', line 57 def initialize(robot) @robot = robot ensure_required_configs end |
Class Attribute Details
.required_configs ⇒ Array (readonly)
Will be removed in Lita 5.0. Use #configuration_builder instead.
A list of configuration keys that are required for the adapter to boot.
29 30 31 32 |
# File 'lib/lita/adapter.rb', line 29 def required_configs Lita.logger.warn(I18n.t("lita.adapter.required_configs_deprecated")) @required_configs end |
Instance Attribute Details
#robot ⇒ Lita::Robot (readonly)
The instance of Robot.
22 23 24 |
# File 'lib/lita/adapter.rb', line 22 def robot @robot end |
Class Method Details
.require_config(*keys) ⇒ void Also known as: require_configs
Will be removed in Lita 5.0. Use #config instead.
This method returns an undefined value.
Defines configuration keys that are requried for the adapter to boot.
38 39 40 41 |
# File 'lib/lita/adapter.rb', line 38 def require_config(*keys) @required_configs ||= [] @required_configs.concat(keys.flatten.map(&:to_sym)) end |
.t ⇒ String
Returns the translation for a key, automatically namespaced to the adapter.
53 54 55 |
# File 'lib/lita/adapter.rb', line 53 def translate(key, hash = {}) I18n.translate("lita.adapters.#{namespace}.#{key}", **hash) end |
.translate(key, hash = {}) ⇒ String
Returns the translation for a key, automatically namespaced to the adapter.
49 50 51 |
# File 'lib/lita/adapter.rb', line 49 def translate(key, hash = {}) I18n.translate("lita.adapters.#{namespace}.#{key}", **hash) end |
Instance Method Details
#chat_service ⇒ Object?
This should be implemented by the adapter.
May return an object exposing chat-service-specific APIs.
|
# File 'lib/lita/adapter.rb', line 69
|
#config ⇒ Lita::Configuration
The adapter’s configuration object.
65 66 67 |
# File 'lib/lita/adapter.rb', line 65 def config robot.config.adapters.public_send(self.class.namespace) end |
#join(room_id) ⇒ void
This should be implemented by the adapter.
This method returns an undefined value.
Joins the room with the specified ID.
|
# File 'lib/lita/adapter.rb', line 75
|
#log ⇒ Lita::Logger
The Lita logger.
130 131 132 |
# File 'lib/lita/adapter.rb', line 130 def log Lita.logger end |
#mention_format(name) ⇒ String
Formats a name for “mentioning” a user in a group chat. Override this method in child classes to customize the mention format for the chat service.
140 141 142 |
# File 'lib/lita/adapter.rb', line 140 def mention_format(name) "#{name}:" end |
#part(room_id) ⇒ void
This should be implemented by the adapter.
This method returns an undefined value.
Parts from the room with the specified ID.
|
# File 'lib/lita/adapter.rb', line 82
|
#roster(room) ⇒ Array<Lita::User>
This should be implemented by the adapter.
Get a list of users that are online in the given room.
|
# File 'lib/lita/adapter.rb', line 89
|
#run ⇒ void
This should be implemented by the adapter.
This method returns an undefined value.
The main loop. Should connect to the chat service, listen for incoming messages, create Message objects from them, and dispatch them to the robot by calling Robot#receive.
|
# File 'lib/lita/adapter.rb', line 96
|
#send_messages(target, strings) ⇒ void
This should be implemented by the adapter.
This method returns an undefined value.
Sends one or more messages to a user or room.
|
# File 'lib/lita/adapter.rb', line 103
|
#set_topic(target, topic) ⇒ void
This should be implemented by the adapter.
This method returns an undefined value.
Sets the topic for a room.
|
# File 'lib/lita/adapter.rb', line 110
|
#shut_down ⇒ void
This should be implemented by the adapter.
This method returns an undefined value.
Performs any clean up necessary when disconnecting from the chat service.
121 122 123 124 125 |
# File 'lib/lita/adapter.rb', line 121 REQUIRED_METHODS.each do |method| define_method(method) do |*_args| Lita.logger.warn(I18n.t("lita.adapter.method_not_implemented", method: method)) end end |
#translate(*args) ⇒ Object Also known as: t
145 146 147 |
# File 'lib/lita/adapter.rb', line 145 def translate(*args) self.class.translate(*args) end |