Class: Roda::Component::Faye

Inherits:
Object
  • Object
show all
Includes:
Native
Defined in:
lib/roda/component/faye.rb,
lib/roda/component/faye.rb

Defined Under Namespace

Classes: ChannelManager, CsrfProtection

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ Faye

Returns a new instance of Faye.



21
22
23
24
25
26
27
28
# File 'lib/roda/component/faye.rb', line 21

def initialize url
  super `new Faye.Client(#{url})`
  set_header 'X-CSRF-TOKEN', Element.find('meta[name=_csrf]').attr('content')
  add_extension({
    incoming: ->(message, block) { incoming message, block },
    outgoing: ->(message, block) { outgoing message, block }
  })
end

Instance Attribute Details

#disconnectedObject

Returns the value of attribute disconnected.



19
20
21
# File 'lib/roda/component/faye.rb', line 19

def disconnected
  @disconnected
end

#onlineObject

Returns the value of attribute online.



19
20
21
# File 'lib/roda/component/faye.rb', line 19

def online
  @online
end

Instance Method Details

#incoming(message, block) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/roda/component/faye.rb', line 38

def incoming message, block
  # puts '====INCOMING===='
  # `console.log(#{message})`
  # puts '================'

  if (!@public_id && !@private_id) && message[:channel] == '/meta/handshake'
    subscribe "/components/incoming/#{private_id}/#{public_id}" do |data|
      data     = Native(data)
      event_id = data[:event_id]
      body     = Element['body']

      body.trigger(event_id, data[:local], data)
      body.off event_id
    end
  end

  block.call message
end

#outgoing(message, block) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/roda/component/faye.rb', line 57

def outgoing message, block
  message = %x{
    message = #{message}
    message.ext = message.ext || {};
    message.ext.csrfToken = $('meta[name=_csrf]').attr('content');
    message.ext.public_id = #{public_id};
    message.ext.private_id = #{private_id};
  }

  # puts '====OUTGOING===='
  # `console.log(#{message})`
  # puts '================'

  block.call message
end

#private_idObject



34
35
36
# File 'lib/roda/component/faye.rb', line 34

def private_id
  @private_id ||= generate_id
end

#public_idObject



30
31
32
# File 'lib/roda/component/faye.rb', line 30

def public_id
  @public_id ||= generate_id
end