class MQTTPipe::Listener
Used to store topics along with their actions. Contains conveniens methods for matching the topic to a given string as well as calling the action.
Attributes
action[R]
pattern[R]
topic[R]
Public Class Methods
new(topic, &action)
click to toggle source
The listener requires a topic string and a callable action to initialize.
An ArgumentError is raised if no action is given
# File lib/mqtt_pipe/listener.rb, line 17 def initialize topic, &action raise ArgumentError, 'No block given' if action.nil? @topic = topic @action = action pattern = topic.gsub('*', '([^/]+)').gsub('/#', '/?(.*)').gsub('#', '(.*)') @pattern = %r{^#{pattern}$} end
Public Instance Methods
===(topic)
click to toggle source
Returns true if the topic matches listener topic Otherwise false.
# File lib/mqtt_pipe/listener.rb, line 43 def === topic @pattern === topic end
call(*args)
click to toggle source
Call the listener action
# File lib/mqtt_pipe/listener.rb, line 50 def call *args #raise ArgumentError, 'No value provided' if args.empty? @action.call *args end
Also aliased as: run
match(topic)
click to toggle source
Check if a given topic string matches the listener topic.
Returns an array containing any matched sections of topic, if there was a match. False otherwise.
# File lib/mqtt_pipe/listener.rb, line 34 def match topic m = @pattern.match topic m.nil? ? false : m.captures end