Class: Codebot::Channel

Inherits:
Serializable show all
Includes:
Sanitizers
Defined in:
lib/codebot/channel.rb

Overview

This class represents an IRC channel notifications can be delivered to.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Sanitizers

#valid!, #valid_boolean, #valid_channel_key, #valid_channel_name, #valid_endpoint, #valid_host, #valid_identifier, #valid_network, #valid_port, #valid_secret, #valid_string

Methods inherited from Serializable

deserialize_all, serialize_all

Constructor Details

#initialize(params) ⇒ Channel

Creates a new channel from the supplied hash.

Parameters:

  • params (Hash)

    A hash with symbolic keys representing the instance attributes of this channel. The keys :name and :network are required. Alternatively, the key :identifier, which should use the format network/name, can be specified.



30
31
32
# File 'lib/codebot/channel.rb', line 30

def initialize(params)
  update!(params)
end

Instance Attribute Details

#keyString?

Returns the key required for joining this channel.

Returns:

  • (String, nil)

    the key required for joining this channel



18
19
20
# File 'lib/codebot/channel.rb', line 18

def key
  @key
end

#nameString

Returns the name of this channel.

Returns:

  • (String)

    the name of this channel



12
13
14
# File 'lib/codebot/channel.rb', line 12

def name
  @name
end

#networkNetwork (readonly)

Returns the network this channel belongs to.

Returns:

  • (Network)

    the network this channel belongs to



15
16
17
# File 'lib/codebot/channel.rb', line 15

def network
  @network
end

#send_externalBoolean

Returns whether to send messages without joining this channel.

Returns:

  • (Boolean)

    whether to send messages without joining this channel



21
22
23
# File 'lib/codebot/channel.rb', line 21

def send_external
  @send_external
end

Class Method Details

.deserialize(identifier, data) ⇒ Hash

Deserializes a channel.

Parameters:

  • identifier (String)

    the channel identifier

  • data (Hash)

    the serialized data

Returns:

  • (Hash)

    the parameters to pass to the initializer



121
122
123
124
125
126
127
# File 'lib/codebot/channel.rb', line 121

def self.deserialize(identifier, data)
  {
    identifier: identifier,
    key: data['key'],
    send_external: data['send_external']
  }
end

.serialize_as_hash?true

Returns to indicate that data is serialized into a hash.

Returns:

  • (true)

    to indicate that data is serialized into a hash



130
131
132
# File 'lib/codebot/channel.rb', line 130

def self.serialize_as_hash?
  true
end

Instance Method Details

#identifierString

Returns the string used to identify this channel in configuration files.

Returns:

  • (String)

    the identifier



92
93
94
# File 'lib/codebot/channel.rb', line 92

def identifier
  "#{@network.name}/#{@name}"
end

#identifier_eql?(identifier) ⇒ Boolean

Checks whether the identifier of this channel is equal to another identifier.

Parameters:

  • identifier (String)

    the other identifier

Returns:

  • (Boolean)

    true if the names are equal, false otherwise



85
86
87
# File 'lib/codebot/channel.rb', line 85

def identifier_eql?(identifier)
  self.identifier.casecmp(identifier).zero?
end

#key?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/codebot/channel.rb', line 69

def key?
  !key.to_s.strip.empty?
end

#serialize(_conf) ⇒ Array, Hash

Serializes this channel.

Parameters:

  • _conf (Hash)

    the deserialized configuration

Returns:

  • (Array, Hash)

    the serialized object



109
110
111
112
113
114
# File 'lib/codebot/channel.rb', line 109

def serialize(_conf)
  [identifier, {
    'key' => key,
    'send_external' => send_external
  }]
end

#set_identifier(identifier, conf) ⇒ Object

Sets network and channel name based on the given identifier.

Parameters:

  • identifier (String)

    the identifier

  • conf (Hash)

    the configuration containing all networks



100
101
102
103
# File 'lib/codebot/channel.rb', line 100

def set_identifier(identifier, conf)
  network_name, self.name = identifier.split('/', 2) if identifier
  set_network(network_name, conf)
end

#set_network(network, conf) ⇒ Object

Sets the network for this channel.

Parameters:

  • network (String)

    the name of the network

  • conf (Hash)

    the configuration containing all networks



57
58
59
60
61
62
# File 'lib/codebot/channel.rb', line 57

def set_network(network, conf)
  @network = valid! network, valid_network(network, conf), :@network,
                    required: true,
                    required_error: 'channels must have a network',
                    invalid_error: 'invalid channel network %s'
end

#update!(params) ⇒ Object

Updates the channel from the supplied hash.

Parameters:

  • params (Hash)

    A hash with symbolic keys representing the instance attributes of this channel.



38
39
40
41
42
43
44
# File 'lib/codebot/channel.rb', line 38

def update!(params)
  set_identifier params[:identifier], params[:config] if params[:identifier]
  self.name          = params[:name]
  self.key           = params[:key]
  self.send_external = params[:send_external]
  set_network params[:network], params[:config]
end