Class: FayeRails::Controller::Channel

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(channel, endpoint = nil) ⇒ Channel

Returns a new instance of Channel.



7
8
9
10
# File 'lib/faye-rails/controller/channel.rb', line 7

def initialize(channel, endpoint=nil)
  @channel = channel
  @endpoint = endpoint
end

Instance Attribute Details

#channelObject (readonly)

Returns the value of attribute channel.



5
6
7
# File 'lib/faye-rails/controller/channel.rb', line 5

def channel
  @channel
end

#endpointObject (readonly)

Returns the value of attribute endpoint.



5
6
7
# File 'lib/faye-rails/controller/channel.rb', line 5

def endpoint
  @endpoint
end

Instance Method Details

#clientObject



12
13
14
# File 'lib/faye-rails/controller/channel.rb', line 12

def client
  FayeRails.client(endpoint)
end

#filter(direction = :any, &block) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/faye-rails/controller/channel.rb', line 33

def filter(direction=:any, &block)
  filter = FayeRails::Filter.new(channel, direction, block)
  server = FayeRails.server(endpoint)
  server.add_extension(filter)
  filter.server = server
  filter
end

#monitor(event, &block) ⇒ Object

Raises:

  • (ArgumentError)


20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/faye-rails/controller/channel.rb', line 20

def monitor(event, &block)
  raise ArgumentError, "Unknown event #{event.inspect}" unless [:subscribe,:unsubscribe,:publish].member? event

  FayeRails.server(endpoint).bind(event) do |*args|
    Monitor.new.tap do |m|
      m.client_id = args.shift
      m.channel = args.shift
      m.data = args.shift
      m.instance_eval(&block) if FayeRails::Matcher.match? channel, m.channel
    end
  end
end

#publish(message) ⇒ Object



16
17
18
# File 'lib/faye-rails/controller/channel.rb', line 16

def publish(message)
  client.publish(channel, message)
end

#subscribe(&block) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/faye-rails/controller/channel.rb', line 41

def subscribe(&block)
  EM.schedule do
    FayeRails.client(endpoint).subscribe(channel) do |message|
      Message.new.tap do |m|
        m.message = message
        m.channel = channel
        m.instance_eval(&block)
      end
    end
  end
end

#unsubscribeObject



53
54
55
56
57
# File 'lib/faye-rails/controller/channel.rb', line 53

def unsubscribe
  EM.schedule do
    FayeRails.client(endpoint).unsubscribe(channel)
  end
end