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

- (Channel) initialize(channel, endpoint = nil)



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

- (Object) channel (readonly)

Returns the value of attribute channel



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

def channel
  @channel
end

- (Object) endpoint (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

- (Object) client



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

def client
  FayeRails.client(endpoint)
end

- (Object) filter(direction = :any, &block)



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

- (Object) monitor(event, &block)

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 File.fnmatch(channel, m.channel)
    end
  end
end

- (Object) publish(message)



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

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

- (Object) subscribe(&block)



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
    @subscription = 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

- (Object) unsubscribe



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

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