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.

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.



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



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

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

#client_id?(x = nil) ⇒ 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



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?



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

def incoming?
  direction == :incoming
end

#meta?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.



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?



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



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



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

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

#subscription?(channel) ⇒ 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



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

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