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

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

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

#callbackObject (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

#channelObject (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

#directionObject (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

#messageObject (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

#original_messageObject (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

#block(reason = "Message blocked by filter") ⇒ Object

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

#channel_matches?(glob, test) ⇒ Boolean

Returns:

  • (Boolean)


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

def channel_matches?(glob,test)
  FayeRails::Matcher.match? glob, test
end

#client_id?(x = nil) ⇒ Boolean

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

#dataObject



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

def data
  message['data']
end

#data?Boolean

Returns:

  • (Boolean)


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

def data?
  !!data
end

#dropObject

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

#incoming?Boolean Also known as: in?

Returns:

  • (Boolean)


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

def incoming?
  direction == :incoming
end

#meta?Boolean

Returns:

  • (Boolean)


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

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

#modify(new_message) ⇒ Object

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

#outgoing?Boolean Also known as: out?

Returns:

  • (Boolean)


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

def outgoing?
  direction == :outgoing
end

#passObject

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

#service?Boolean

Returns:

  • (Boolean)


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

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

#subscribing?Boolean

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

#subscription?(channel) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#unsubscribing?Boolean

Returns:

  • (Boolean)


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

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