Class: Nanobot::Channels::BaseChannel
- Inherits:
-
Object
- Object
- Nanobot::Channels::BaseChannel
- Defined in:
- lib/nanobot/channels/base.rb
Overview
Base class for all channel implementations
Instance Attribute Summary collapse
-
#bus ⇒ Object
readonly
Returns the value of attribute bus.
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
-
#allowed?(sender_id) ⇒ Boolean
Check if a sender is allowed to use this channel.
-
#initialize(name:, config:, bus:, logger: nil) ⇒ BaseChannel
constructor
A new instance of BaseChannel.
-
#running? ⇒ Boolean
Check if channel is running.
-
#send(message) ⇒ Object
Send a message through this channel.
-
#start ⇒ Object
Start the channel (connect and begin listening).
-
#stop ⇒ Object
Stop the channel (disconnect and cleanup).
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.
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
59 60 61 |
# File 'lib/nanobot/channels/base.rb', line 59 def running? @running end |
#send(message) ⇒ Object
Send a message through this channel
31 32 33 |
# File 'lib/nanobot/channels/base.rb', line 31 def send() raise NotImplementedError, "#{self.class} must implement #send" end |
#start ⇒ Object
Start the channel (connect and begin listening)
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)
25 26 27 |
# File 'lib/nanobot/channels/base.rb', line 25 def stop raise NotImplementedError, "#{self.class} must implement #stop" end |