Class: SlackSender::ChannelNormalizer

Inherits:
Object
  • Object
show all
Defined in:
lib/slack_sender/channel_normalizer.rb

Overview

Encapsulates channel normalization logic for call kwargs. Handles:

  • Conflict validation (channel: vs channels:)
  • Empty channels validation
  • Single-element array normalization (channels: [:foo] -> channel: :foo)
  • Symbol to string preprocessing with validation flag
  • Default channel application

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(channel: nil, channels: nil) ⇒ ChannelNormalizer

Returns a new instance of ChannelNormalizer.



14
15
16
17
18
# File 'lib/slack_sender/channel_normalizer.rb', line 14

def initialize(channel: nil, channels: nil)
  validate_conflict!(channel, channels)
  @channel, @channels = normalize(channel, channels)
  @validate_known_channel = false
end

Instance Attribute Details

#channelObject (readonly)

Returns the value of attribute channel.



12
13
14
# File 'lib/slack_sender/channel_normalizer.rb', line 12

def channel
  @channel
end

#channelsObject (readonly)

Returns the value of attribute channels.



12
13
14
# File 'lib/slack_sender/channel_normalizer.rb', line 12

def channels
  @channels
end

#validate_known_channelObject (readonly)

Returns the value of attribute validate_known_channel.



12
13
14
# File 'lib/slack_sender/channel_normalizer.rb', line 12

def validate_known_channel
  @validate_known_channel
end

Instance Method Details

#apply_default!(default_channel) ⇒ Object

Apply default if no channel specified



32
33
34
35
36
# File 'lib/slack_sender/channel_normalizer.rb', line 32

def apply_default!(default_channel)
  return if @channel.present? || @channels.present?

  @channel = default_channel if default_channel.present?
end

#multi?Boolean

Returns:

  • (Boolean)


21
# File 'lib/slack_sender/channel_normalizer.rb', line 21

def multi? = !single?

#preprocess_channel!Object

Converts symbol channel to string and flags for validation



24
25
26
27
28
29
# File 'lib/slack_sender/channel_normalizer.rb', line 24

def preprocess_channel!
  return unless @channel.is_a?(Symbol)

  @channel = @channel.to_s
  @validate_known_channel = true
end

#single?Boolean

Returns:

  • (Boolean)


20
# File 'lib/slack_sender/channel_normalizer.rb', line 20

def single? = @channels.nil?

#to_kwargsObject

Returns kwargs to merge back



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/slack_sender/channel_normalizer.rb', line 39

def to_kwargs
  if @channels
    { channels: @channels }
  elsif @channel
    result = { channel: @channel }
    result[:validate_known_channel] = true if @validate_known_channel
    result
  else
    {}
  end
end