Class: Codebot::Integration
- Inherits:
-
Serializable
- Object
- Serializable
- Codebot::Integration
- 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
-
#channels ⇒ Array<Channel>
readonly
The channels notifications will be delivered to.
-
#endpoint ⇒ String
The endpoint mapped to this integration.
-
#gitlab ⇒ Object
Returns the value of attribute gitlab.
-
#name ⇒ String
The name of this integration.
-
#secret ⇒ String
The secret for verifying the authenticity of payloads delivered to the endpoint.
-
#shortener_secret ⇒ Object
Returns the value of attribute shortener_secret.
-
#shortener_url ⇒ Object
Returns the value of attribute shortener_url.
Class Method Summary collapse
-
.deserialize(name, data) ⇒ Hash
Deserializes an integration.
-
.serialize_as_hash? ⇒ true
To indicate that data is serialized into a hash.
Instance Method Summary collapse
-
#add_channels!(channels, conf) ⇒ Object
Adds the specified channels to this integration.
-
#check_channel_networks!(conf) ⇒ Object
Compares the channels against the specified configuration, dropping any channels belonging to networks that no longer exist.
-
#delete_channels!(identifiers) ⇒ Object
Deletes the specified channels from this integration.
-
#endpoint_eql?(endpoint) ⇒ Boolean
Checks whether the endpoint associated with this integration is equal to another endpoint.
-
#initialize(params) ⇒ Integration
constructor
Creates a new integration from the supplied hash.
-
#name_eql?(name) ⇒ Boolean
Checks whether the name of this integration is equal to another name.
-
#serialize(conf) ⇒ Array, Hash
Serializes this integration.
-
#set_channels(channels, conf) ⇒ Object
Sets the list of channels.
-
#update!(params) ⇒ Object
Updates the integration from the supplied hash.
-
#verify_payloads? ⇒ Boolean
Checks whether payloads delivered to this integration must be verified.
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.
36 37 38 |
# File 'lib/codebot/integration.rb', line 36 def initialize(params) update!(params) end |
Instance Attribute Details
#channels ⇒ Array<Channel> (readonly)
Returns the channels notifications will be delivered to.
25 26 27 |
# File 'lib/codebot/integration.rb', line 25 def channels @channels end |
#endpoint ⇒ String
Returns the endpoint mapped to this integration.
18 19 20 |
# File 'lib/codebot/integration.rb', line 18 def endpoint @endpoint end |
#gitlab ⇒ Object
Returns the value of attribute gitlab.
27 28 29 |
# File 'lib/codebot/integration.rb', line 27 def gitlab @gitlab end |
#name ⇒ String
Returns the name of this integration.
15 16 17 |
# File 'lib/codebot/integration.rb', line 15 def name @name end |
#secret ⇒ String
Returns 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_secret ⇒ Object
Returns the value of attribute shortener_secret.
29 30 31 |
# File 'lib/codebot/integration.rb', line 29 def shortener_secret @shortener_secret end |
#shortener_url ⇒ Object
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.
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.
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
This method is not thread-safe and should only be called from an active transaction.
Adds the specified channels to this integration.
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.
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
This method is not thread-safe and should only be called from an active transaction.
Deletes the specified channels from this integration.
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.
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.
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.
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.
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.
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.
112 113 114 |
# File 'lib/codebot/integration.rb', line 112 def verify_payloads? !secret.to_s.strip.empty? end |