class Lita::Adapter

Adapters are the glue between Lita's API and a chat service.

Constants

REQUIRED_METHODS

The names of methods that should be implemented by an adapter. @since 4.4.0

Attributes

robot[R]

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

Public Class Methods

new(robot) click to toggle source

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

# File lib/lita/adapter.rb, line 57
def initialize(robot)
  @robot = robot
  ensure_required_configs
end
require_config(*keys) click to toggle source

Defines configuration keys that are requried for the adapter to boot. @param keys [String, Symbol] The required keys. @return [void] @deprecated Will be removed in Lita 5.0. Use {Lita::Adapter#config} instead.

# File lib/lita/adapter.rb, line 38
def require_config(*keys)
  @required_configs ||= []
  @required_configs.concat(keys.flatten.map(&:to_sym))
end
Also aliased as: require_configs
require_configs(*keys)
Alias for: require_config
required_configs() click to toggle source

@!attribute [r] ::required_configs A list of configuration keys that are required for the adapter to boot. @return [Array] @deprecated Will be removed in Lita 5.0. Use {Lita::Adapter#configuration_builder} instead.

# File lib/lita/adapter.rb, line 29
def required_configs
  Lita.logger.warn(I18n.t("lita.adapter.required_configs_deprecated"))
  @required_configs
end
t(key, hash = {})
Alias for: translate
translate(key, hash = {}) click to toggle source

Returns the translation for a key, automatically namespaced to the adapter. @param key [String] The key of the translation. @param hash [Hash] An optional hash of values to be interpolated in the string. @return [String] The translated string.

# File lib/lita/adapter.rb, line 49
def translate(key, hash = {})
  I18n.translate("lita.adapters.#{namespace}.#{key}", hash)
end
Also aliased as: t

Public Instance Methods

config() click to toggle source

The adapter's configuration object. @return [Lita::Configuration] The adapter's configuration object. @since 4.0.0

# File lib/lita/adapter.rb, line 65
def config
  robot.config.adapters.public_send(self.class.namespace)
end
log() click to toggle source

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

# File lib/lita/adapter.rb, line 130
def log
  Lita.logger
end
mention_format(name) click to toggle source

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. @param name [String] The name to format as a mention name. @return [String] The formatted mention name. @since 3.1.0

# File lib/lita/adapter.rb, line 140
def mention_format(name)
  "#{name}:"
end
t(*args)
Alias for: translate
translate(*args) click to toggle source

@see .translate

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

Private Instance Methods

adapter_config() click to toggle source

Returns the object used as the adapter's configuration.

# File lib/lita/adapter.rb, line 154
def adapter_config
  if Lita.version_3_compatibility_mode?
    Lita.config.adapter
  else
    robot.config.adapter
  end
end
ensure_required_configs() click to toggle source

Logs a fatal message and aborts if a required config key is not set.

# File lib/lita/adapter.rb, line 163
def ensure_required_configs
  return if required_configs.nil?

  Lita.logger.warn(I18n.t("lita.adapter.require_config_deprecated"))

  missing_keys = missing_config_keys

  unless missing_keys.empty?
    Lita.logger.fatal(I18n.t("lita.adapter.missing_configs", configs: missing_keys.join(", ")))
    abort
  end
end
missing_config_keys() click to toggle source

Finds all missing config keys.

# File lib/lita/adapter.rb, line 177
def missing_config_keys
  required_configs.select do |key|
    key unless adapter_config[key]
  end
end
required_configs() click to toggle source

Access the required configs without triggering the deprecation warning.

# File lib/lita/adapter.rb, line 184
def required_configs
  self.class.instance_variable_get(:@required_configs)
end