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
29
30
# File 'lib/roda/component/faye.rb', line 21

def initialize url
  return false unless `(typeof Faye === 'function')`

  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



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

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



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

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



36
37
38
# File 'lib/roda/component/faye.rb', line 36

def private_id
  @private_id ||= generate_id
end

#public_idObject



32
33
34
# File 'lib/roda/component/faye.rb', line 32

def public_id
  @public_id ||= generate_id
end