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

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:



44
45
46
47
# File 'lib/lita/adapter.rb', line 44

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)


16
17
18
19
# File 'lib/lita/adapter.rb', line 16

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:



9
10
11
# File 'lib/lita/adapter.rb', line 9

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.



25
26
27
28
# File 'lib/lita/adapter.rb', line 25

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.



40
41
42
# File 'lib/lita/adapter.rb', line 40

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.



36
37
38
# File 'lib/lita/adapter.rb', line 36

def translate(key, hash = {})
  I18n.translate("lita.adapters.#{namespace}.#{key}", hash)
end

Instance Method Details

#configLita::Configuration

The adapter's configuration object.

Returns:

Since:

  • 4.0.0



52
53
54
# File 'lib/lita/adapter.rb', line 52

def config
  robot.config.adapters.public_send(self.class.namespace)
end

#joinvoid

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 56

#logLita::Logger

The Lita logger.

Returns:

Since:

  • 4.0.2



104
105
106
# File 'lib/lita/adapter.rb', line 104

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



114
115
116
# File 'lib/lita/adapter.rb', line 114

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

#partvoid

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 63

#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 70

#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 77

#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 84

#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.



95
96
97
98
99
# File 'lib/lita/adapter.rb', line 95

[:join, :part, :run, :send_messages, :set_topic, :shut_down].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:



119
120
121
# File 'lib/lita/adapter.rb', line 119

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