Class: Lita::Adapter

Inherits:
Object
  • Object
show all
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

Lita::Adapters::Shell, Lita::Adapters::Test

Constant Summary collapse

REQUIRED_METHODS =

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

Since:

  • 4.4.0

%i(
  chat_service
  join
  part
  roster
  run
  send_messages
  set_topic
  shut_down
).freeze

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes included from Configurable

#configuration_builder

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Configurable

inherited

Methods included from Namespace

namespace

Constructor Details

#initialize(robot) ⇒ Adapter

Returns a new instance of Adapter.

Parameters:


57
58
59
60
# File 'lib/lita/adapter.rb', line 57

def initialize(robot)
  @robot = robot
  ensure_required_configs
end

Class Attribute Details

.required_configsArray (readonly)

Deprecated.

Will be removed in Lita 5.0. Use #configuration_builder instead.

A list of configuration keys that are required for the adapter to boot.

Returns:

  • (Array)

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

#robotLita::Robot (readonly)

The instance of Robot.

Returns:


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

Deprecated.

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.

Parameters:

  • keys (String, Symbol)

    The required keys.


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

.tString

Returns the translation for a key, automatically namespaced to the adapter.

Parameters:

  • key (String)

    The key of the translation.

  • hash (Hash)

    An optional hash of values to be interpolated in the string.

Returns:

  • (String)

    The translated string.


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.

Parameters:

  • key (String)

    The key of the translation.

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

    An optional hash of values to be interpolated in the string.

Returns:

  • (String)

    The translated string.


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_serviceObject?

This method is abstract.

This should be implemented by the adapter.

May return an object exposing chat-service-specific APIs.

Returns:

  • (Object, nil)

    The chat service API object, if any.

Since:

  • 4.6.0


# File 'lib/lita/adapter.rb', line 69

#configLita::Configuration

The adapter's configuration object.

Returns:

Since:

  • 4.0.0


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 method is abstract.

This should be implemented by the adapter.

This method returns an undefined value.

Joins the room with the specified ID.

Parameters:

  • room_id (String)

    The ID of the room.

Since:

  • 3.0.0


# File 'lib/lita/adapter.rb', line 75

#logLita::Logger

The Lita logger.

Returns:

Since:

  • 4.0.2


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.

Parameters:

  • name (String)

    The name to format as a mention name.

Returns:

  • (String)

    The formatted mention name.

Since:

  • 3.1.0


140
141
142
# File 'lib/lita/adapter.rb', line 140

def mention_format(name)
  "#{name}:"
end

#part(room_id) ⇒ void

This method is abstract.

This should be implemented by the adapter.

This method returns an undefined value.

Parts from the room with the specified ID.

Parameters:

  • room_id (String)

    The ID of the room.

Since:

  • 3.0.0


# File 'lib/lita/adapter.rb', line 82

#roster(room) ⇒ Array<Lita::User>

This method is abstract.

This should be implemented by the adapter.

Get a list of users that are online in the given room.

Parameters:

  • room (Lita::Room)

    The room to return a roster for.

Returns:

Since:

  • 4.4.0


# File 'lib/lita/adapter.rb', line 89

#runvoid

This method is abstract.

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 method is abstract.

This should be implemented by the adapter.

This method returns an undefined value.

Sends one or more messages to a user or room.

Parameters:

  • target (Lita::Source)

    The user or room to send messages to.

  • strings (Array<String>)

    An array of messages to send.


# File 'lib/lita/adapter.rb', line 103

#set_topic(target, topic) ⇒ void

This method is abstract.

This should be implemented by the adapter.

This method returns an undefined value.

Sets the topic for a room.

Parameters:

  • target (Lita::Source)

    The room to change the topic for.

  • topic (String)

    The new topic.


# File 'lib/lita/adapter.rb', line 110

#shut_downvoid

This method is abstract.

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

See Also:


145
146
147
# File 'lib/lita/adapter.rb', line 145

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