class FayeRails::Filter::DSL
Attributes
A small wrapper class around filter blocks to add some sugar to ease filter (Faye extension) creation.
A small wrapper class around filter blocks to add some sugar to ease filter (Faye extension) creation.
A small wrapper class around filter blocks to add some sugar to ease filter (Faye extension) creation.
A small wrapper class around filter blocks to add some sugar to ease filter (Faye extension) creation.
A small wrapper class around filter blocks to add some sugar to ease filter (Faye extension) creation.
Public Class Methods
Called by FayeRails::Filter
when Faye passes messages in for evaluation. @param block
The block you wish to execute whenever a matching message is recieved.
@param channel
optional: if present then the block will only be called for matching messages, otherwise all messages will be passed.
# File lib/faye-rails/filter.rb, line 75 def initialize(block, message, channel='/**', callback, direction) raise ArgumentError, "Block cannot be nil" unless block @channel = channel @original_message = message.dup @message = message @callback = callback @direction = direction if channel_matches?(@channel, @original_message['channel']) || (subscribing? && subscription?(@channel)) || (unsubscribing? && subscription?(@channel)) instance_eval(&block) else pass end end
Public Instance Methods
Syntactic sugar around callback.call which adds an error message to the message and passes it back to Faye, which will send back a rejection message to the sending client. @param reason
The error message to be sent back to the client.
# File lib/faye-rails/filter.rb, line 164 def block(reason="Message blocked by filter") new_message = message new_message['error'] = reason callback.call(new_message) end
# File lib/faye-rails/filter.rb, line 135 def channel_matches?(glob,test) FayeRails::Matcher.match? glob, test end
# File lib/faye-rails/filter.rb, line 127 def client_id?(x=nil) if !!x message['client_id'] == x else !!message['client_id'] end end
# File lib/faye-rails/filter.rb, line 119 def data message['data'] end
# File lib/faye-rails/filter.rb, line 123 def data? !!data end
Syntactic sugar around callback.call which returns nil to Faye - effectively dropping the message.
# File lib/faye-rails/filter.rb, line 172 def drop callback.call(nil) end
# File lib/faye-rails/filter.rb, line 109 def incoming? direction == :incoming end
# File lib/faye-rails/filter.rb, line 101 def meta? message['channel'][0..5] == '/meta/' end
Syntactic sugar around callback.call which passes the passed argument back to Faye in place of the original message. @param new_message
Replacement message to send back to Faye.
# File lib/faye-rails/filter.rb, line 154 def modify(new_message) callback.call(new_message) end
# File lib/faye-rails/filter.rb, line 114 def outgoing? direction == :outgoing end
Syntactic sugar around callback.call which passes back the original message unmodified.
# File lib/faye-rails/filter.rb, line 145 def pass callback.call(original_message) end
# File lib/faye-rails/filter.rb, line 105 def service? message['channel'][0..8] == '/service/' end
Easier than testing message every time
# File lib/faye-rails/filter.rb, line 93 def subscribing? message['channel'] == '/meta/subscribe' end
# File lib/faye-rails/filter.rb, line 139 def subscription?(channel) message['subscription'] && channel_matches?(channel, message['subscription']) end
# File lib/faye-rails/filter.rb, line 97 def unsubscribing? message['channel'] == '/meta/unsubscribe' end