Class: Nanobot::Channels::BaseChannel

Inherits:
Object
  • Object
show all
Defined in:
lib/nanobot/channels/base.rb

Overview

Base class for all channel implementations

Direct Known Subclasses

Discord, Email, Gateway, Slack, Telegram

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, config:, bus:, logger: nil) ⇒ BaseChannel

Returns a new instance of BaseChannel.



11
12
13
14
15
16
17
# File 'lib/nanobot/channels/base.rb', line 11

def initialize(name:, config:, bus:, logger: nil)
  @name = name
  @config = config
  @bus = bus
  @logger = logger || Logger.new(IO::NULL)
  @running = false
end

Instance Attribute Details

#bus ⇒ Object (readonly)

Returns the value of attribute bus.



9
10
11
# File 'lib/nanobot/channels/base.rb', line 9

def bus
  @bus
end

#config ⇒ Object (readonly)

Returns the value of attribute config.



9
10
11
# File 'lib/nanobot/channels/base.rb', line 9

def config
  @config
end

#logger ⇒ Object (readonly)

Returns the value of attribute logger.



9
10
11
# File 'lib/nanobot/channels/base.rb', line 9

def logger
  @logger
end

#name ⇒ Object (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/nanobot/channels/base.rb', line 9

def name
  @name
end

Instance Method Details

#allowed?(sender_id) ⇒ Boolean

Check if a sender is allowed to use this channel. An empty allow_from list denies all senders (fail-closed). Use ["*"] to explicitly allow all senders.

Parameters:

  • sender_id (String) —

    sender identifier

Returns:

  • (Boolean)


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/nanobot/channels/base.rb', line 40

def allowed?(sender_id)
  allow_from = config.allow_from || []

  if allow_from.empty?
    unless @allow_from_warned
      @logger.warn "Channel '#{@name}' has an empty allow_from list - denying all senders. " \
                   'Set allow_from: ["*"] to allow everyone.'
      @allow_from_warned = true
    end
    return false
  end

  return true if allow_from.include?('*')

  allow_from.include?(sender_id.to_s)
end

#running? ⇒ Boolean

Check if channel is running

Returns:

  • (Boolean)


59
60
61
# File 'lib/nanobot/channels/base.rb', line 59

def running?
  @running
end

#send(message) ⇒ Object

Send a message through this channel

Parameters:

Raises:

  • (NotImplementedError)


31
32
33
# File 'lib/nanobot/channels/base.rb', line 31

def send(message)
  raise NotImplementedError, "#{self.class} must implement #send"
end

#start ⇒ Object

Start the channel (connect and begin listening)

Raises:

  • (NotImplementedError)


20
21
22
# File 'lib/nanobot/channels/base.rb', line 20

def start
  raise NotImplementedError, "#{self.class} must implement #start"
end

#stop ⇒ Object

Stop the channel (disconnect and cleanup)

Raises:

  • (NotImplementedError)


25
26
27
# File 'lib/nanobot/channels/base.rb', line 25

def stop
  raise NotImplementedError, "#{self.class} must implement #stop"
end