Class: FayeRails::Filter::DSL

Inherits:
Object
  • Object
show all
Defined in:
lib/faye-rails/filter.rb

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (DSL) initialize(block, message, channel = '/**', callback, direction)

Called by FayeRails::Filter when Faye passes messages in for evaluation.

Parameters:

  • block

    The block you wish to execute whenever a matching message is recieved.

  • channel (defaults to: '/**')

    optional: if present then the block will only be called for matching messages, otherwise all messages will be passed.

Raises:

  • (ArgumentError)


75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# 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

Instance Attribute Details

- (Object) callback (readonly)

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



66
67
68
# File 'lib/faye-rails/filter.rb', line 66

def callback
  @callback
end

- (Object) channel (readonly)

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



66
67
68
# File 'lib/faye-rails/filter.rb', line 66

def channel
  @channel
end

- (Object) direction (readonly)

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



66
67
68
# File 'lib/faye-rails/filter.rb', line 66

def direction
  @direction
end

- (Object) message (readonly)

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



66
67
68
# File 'lib/faye-rails/filter.rb', line 66

def message
  @message
end

- (Object) original_message (readonly)

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



66
67
68
# File 'lib/faye-rails/filter.rb', line 66

def original_message
  @original_message
end

Instance Method Details

- (Object) block(reason = "Message blocked by filter")

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.

Parameters:

  • reason (defaults to: "Message blocked by filter")

    The error message to be sent back to the client.



164
165
166
167
168
# 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

- (Boolean) channel_matches?(glob, test)

Returns:

  • (Boolean)


135
136
137
# File 'lib/faye-rails/filter.rb', line 135

def channel_matches?(glob,test)
  File.fnmatch? glob, test
end

- (Boolean) client_id?(x = nil)

Returns:

  • (Boolean)


127
128
129
130
131
132
133
# 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

- (Object) data



119
120
121
# File 'lib/faye-rails/filter.rb', line 119

def data
  message['data']
end

- (Boolean) data?

Returns:

  • (Boolean)


123
124
125
# File 'lib/faye-rails/filter.rb', line 123

def data?
  !!data
end

- (Object) drop

Syntactic sugar around callback.call which returns nil to Faye - effectively dropping the message.



172
173
174
# File 'lib/faye-rails/filter.rb', line 172

def drop
  callback.call(nil)
end

- (Boolean) incoming? Also known as: in?

Returns:

  • (Boolean)


109
110
111
# File 'lib/faye-rails/filter.rb', line 109

def incoming?
  direction == :incoming
end

- (Boolean) meta?

Returns:

  • (Boolean)


101
102
103
# File 'lib/faye-rails/filter.rb', line 101

def meta?
  message['channel'][0..5] == '/meta/'
end

- (Object) modify(new_message)

Syntactic sugar around callback.call which passes the passed argument back to Faye in place of the original message.

Parameters:

  • new_message

    Replacement message to send back to Faye.



154
155
156
# File 'lib/faye-rails/filter.rb', line 154

def modify(new_message)
  callback.call(new_message)
end

- (Boolean) outgoing? Also known as: out?

Returns:

  • (Boolean)


114
115
116
# File 'lib/faye-rails/filter.rb', line 114

def outgoing?
  direction == :outgoing
end

- (Object) pass

Syntactic sugar around callback.call which passes back the original message unmodified.



145
146
147
# File 'lib/faye-rails/filter.rb', line 145

def pass
  callback.call(original_message)
end

- (Boolean) service?

Returns:

  • (Boolean)


105
106
107
# File 'lib/faye-rails/filter.rb', line 105

def service?
  message['channel'][0..8] == '/service/'
end

- (Boolean) subscribing?

Easier than testing message every time

Returns:

  • (Boolean)


93
94
95
# File 'lib/faye-rails/filter.rb', line 93

def subscribing?
  message['channel'] == '/meta/subscribe'
end

- (Boolean) subscription?(channel)

Returns:

  • (Boolean)


139
140
141
# File 'lib/faye-rails/filter.rb', line 139

def subscription?(channel)
  message['subscription'] && channel_matches?(channel, message['subscription'])
end

- (Boolean) unsubscribing?

Returns:

  • (Boolean)


97
98
99
# File 'lib/faye-rails/filter.rb', line 97

def unsubscribing?
  message['channel'] == '/meta/unsubscribe'
end