Class: Codebot::Integration

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

Overview

This class represents an integration that maps an endpoint to the corresponding IRC channels.

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) ⇒ Integration

Creates a new integration from the supplied hash.

Parameters:

  • params (Hash)

    A hash with symbolic keys representing the instance attributes of this integration. The key :name is required.



36
37
38
# File 'lib/codebot/integration.rb', line 36

def initialize(params)
  update!(params)
end

Instance Attribute Details

#channelsArray<Channel> (readonly)

Returns the channels notifications will be delivered to.

Returns:

  • (Array<Channel>)

    the channels notifications will be delivered to



25
26
27
# File 'lib/codebot/integration.rb', line 25

def channels
  @channels
end

#endpointString

Returns the endpoint mapped to this integration.

Returns:

  • (String)

    the endpoint mapped to this integration



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

def endpoint
  @endpoint
end

#gitlabObject

Returns the value of attribute gitlab.



27
28
29
# File 'lib/codebot/integration.rb', line 27

def gitlab
  @gitlab
end

#nameString

Returns the name of this integration.

Returns:

  • (String)

    the name of this integration



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

def name
  @name
end

#secretString

Returns the secret for verifying the authenticity of payloads delivered to the endpoint.

Returns:

  • (String)

    the secret for verifying the authenticity of payloads delivered to the endpoint



22
23
24
# File 'lib/codebot/integration.rb', line 22

def secret
  @secret
end

#shortener_secretObject

Returns the value of attribute shortener_secret.



29
30
31
# File 'lib/codebot/integration.rb', line 29

def shortener_secret
  @shortener_secret
end

#shortener_urlObject

Returns the value of attribute shortener_url.



28
29
30
# File 'lib/codebot/integration.rb', line 28

def shortener_url
  @shortener_url
end

Class Method Details

.deserialize(name, data) ⇒ Hash

Deserializes an integration.

Parameters:

  • name (String)

    the name of the integration

  • data (Hash)

    the serialized data

Returns:

  • (Hash)

    the parameters to pass to the initializer



178
179
180
181
182
183
184
185
186
187
188
# File 'lib/codebot/integration.rb', line 178

def self.deserialize(name, data)
  {
    name: name,
    endpoint: data['endpoint'],
    secret: data['secret'],
    gitlab: data['gitlab'],
    shortener_url: data['shortener_url'],
    shortener_secret: data['shortener_secret'],
    channels: data['channels']
  }
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



191
192
193
# File 'lib/codebot/integration.rb', line 191

def self.serialize_as_hash?
  true
end

Instance Method Details

#add_channels!(channels, conf) ⇒ Object

Note:

This method is not thread-safe and should only be called from an active transaction.

Adds the specified channels to this integration.

Parameters:

  • channels (Hash)

    the channel data to add

  • conf (Hash)

    the previously deserialized configuration

Raises:

  • (CommandError)

    if one of the channel identifiers already exists



61
62
63
64
65
66
67
68
69
# File 'lib/codebot/integration.rb', line 61

def add_channels!(channels, conf)
  channels.each_key do |identifier|
    if @channels.any? { |chan| chan.identifier_eql?(identifier) }
      raise CommandError, "channel #{identifier.inspect} already exists"
    end
  end
  new_channels = Channel.deserialize_all(channels, conf)
  @channels.push(*new_channels)
end

#check_channel_networks!(conf) ⇒ Object

Compares the channels against the specified configuration, dropping any channels belonging to networks that no longer exist.

Parameters:

  • conf (Config)

    the configuration



167
168
169
170
171
# File 'lib/codebot/integration.rb', line 167

def check_channel_networks!(conf)
  @channels.select! do |channel|
    conf[:networks].include? channel.network
  end
end

#delete_channels!(identifiers) ⇒ Object

Note:

This method is not thread-safe and should only be called from an active transaction.

Deletes the specified channels from this integration.

Parameters:

  • identifiers (Array<String>)

    the channel identifiers to remove

Raises:

  • (CommandError)

    if one of the channel identifiers does not exist



77
78
79
80
81
82
83
84
85
86
# File 'lib/codebot/integration.rb', line 77

def delete_channels!(identifiers)
  identifiers.each do |identifier|
    channel = @channels.find { |chan| chan.identifier_eql? identifier }
    if channel.nil?
      raise CommandError, "channel #{identifier.inspect} does not exist"
    end

    @channels.delete channel
  end
end

#endpoint_eql?(endpoint) ⇒ Boolean

Checks whether the endpoint associated with this integration is equal to another endpoint.

Parameters:

  • endpoint (String)

    the other endpoint

Returns:

  • (Boolean)

    true if the endpoints are equal, false otherwise



143
144
145
# File 'lib/codebot/integration.rb', line 143

def endpoint_eql?(endpoint)
  @endpoint.eql? endpoint
end

#name_eql?(name) ⇒ Boolean

Checks whether the name of this integration is equal to another name.

Parameters:

  • name (String)

    the other name

Returns:

  • (Boolean)

    true if the names are equal, false otherwise



134
135
136
# File 'lib/codebot/integration.rb', line 134

def name_eql?(name)
  @name.casecmp(name).zero?
end

#serialize(conf) ⇒ Array, Hash

Serializes this integration.

Parameters:

  • conf (Hash)

    the deserialized configuration

Returns:

  • (Array, Hash)

    the serialized object



151
152
153
154
155
156
157
158
159
160
161
# File 'lib/codebot/integration.rb', line 151

def serialize(conf)
  check_channel_networks!(conf)
  [name, {
    'endpoint' => endpoint,
    'secret' => secret,
    'gitlab' => gitlab,
    'shortener_url' => shortener_url,
    'shortener_secret' => shortener_secret,
    'channels' => Channel.serialize_all(channels, conf)
  }]
end

#set_channels(channels, conf) ⇒ Object

Sets the list of channels.

Parameters:

  • channels (Array<Channel>)

    the list of channels

  • conf (Hash)

    the previously deserialized configuration



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

def set_channels(channels, conf)
  if channels.nil?
    @channels = [] if @channels.nil?
    return
  end
  @channels = valid!(channels, Channel.deserialize_all(channels, conf),
                     :@channels,
                     invalid_error: 'invalid channel list %s') { [] }
end

#update!(params) ⇒ Object

Updates the integration from the supplied hash.

Parameters:

  • params (Hash)

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



44
45
46
47
48
49
50
51
52
# File 'lib/codebot/integration.rb', line 44

def update!(params)
  self.name     = params[:name]
  self.endpoint = params[:endpoint]
  self.secret   = params[:secret]
  self.gitlab   = params[:gitlab] || false
  self.shortener_url = params[:shortener_url]
  self.shortener_secret = params[:shortener_secret]
  set_channels params[:channels], params[:config]
end

#verify_payloads?Boolean

Checks whether payloads delivered to this integration must be verified.

Returns:

  • (Boolean)

    whether verification is required



112
113
114
# File 'lib/codebot/integration.rb', line 112

def verify_payloads?
  !secret.to_s.strip.empty?
end