class FayeRails::Filter::DSL

Attributes

callback[R]

A small wrapper class around filter blocks to add some sugar to ease filter (Faye extension) creation.

channel[R]

A small wrapper class around filter blocks to add some sugar to ease filter (Faye extension) creation.

direction[R]

A small wrapper class around filter blocks to add some sugar to ease filter (Faye extension) creation.

message[R]

A small wrapper class around filter blocks to add some sugar to ease filter (Faye extension) creation.

original_message[R]

A small wrapper class around filter blocks to add some sugar to ease filter (Faye extension) creation.

Public Class Methods

new(block, message, channel='/**', callback, direction) click to toggle source

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

block(reason="Message blocked by filter") click to toggle source

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
channel_matches?(glob,test) click to toggle source
# File lib/faye-rails/filter.rb, line 135
def channel_matches?(glob,test)
  FayeRails::Matcher.match? glob, test
end
client_id?(x=nil) click to toggle source
# 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
data() click to toggle source
# File lib/faye-rails/filter.rb, line 119
def data
  message['data']
end
data?() click to toggle source
# File lib/faye-rails/filter.rb, line 123
def data?
  !!data
end
drop() click to toggle source

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
in?()
Alias for: incoming?
incoming?() click to toggle source
# File lib/faye-rails/filter.rb, line 109
def incoming?
  direction == :incoming
end
Also aliased as: in?
meta?() click to toggle source
# File lib/faye-rails/filter.rb, line 101
def meta?
  message['channel'][0..5] == '/meta/'
end
modify(new_message) click to toggle source

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
out?()
Alias for: outgoing?
outgoing?() click to toggle source
# File lib/faye-rails/filter.rb, line 114
def outgoing?
  direction == :outgoing
end
Also aliased as: out?
pass() click to toggle source

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
service?() click to toggle source
# File lib/faye-rails/filter.rb, line 105
def service?
  message['channel'][0..8] == '/service/'
end
subscribing?() click to toggle source

Easier than testing message every time

# File lib/faye-rails/filter.rb, line 93
def subscribing?
  message['channel'] == '/meta/subscribe'
end
subscription?(channel) click to toggle source
# File lib/faye-rails/filter.rb, line 139
def subscription?(channel)
  message['subscription'] && channel_matches?(channel, message['subscription'])
end
unsubscribing?() click to toggle source
# File lib/faye-rails/filter.rb, line 97
def unsubscribing?
  message['channel'] == '/meta/unsubscribe'
end