Class: Viaduct::WebPush::WebSocket::Channel

Inherits:
Object
  • Object
show all
Defined in:
lib/viaduct/web_push/web_socket/channel.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, connection) ⇒ Channel

Returns a new instance of Channel.



8
9
10
11
12
13
# File 'lib/viaduct/web_push/web_socket/channel.rb', line 8

def initialize(name, connection)
  @name = name 
  @connection = connection
  @bindings = Hash.new([])
  @subscribed = false
end

Instance Attribute Details

#bindingsObject

Returns the value of attribute bindings.



6
7
8
# File 'lib/viaduct/web_push/web_socket/channel.rb', line 6

def bindings
  @bindings
end

#nameObject

Returns the value of attribute name.



6
7
8
# File 'lib/viaduct/web_push/web_socket/channel.rb', line 6

def name
  @name
end

#subscribedObject

Returns the value of attribute subscribed.



6
7
8
# File 'lib/viaduct/web_push/web_socket/channel.rb', line 6

def subscribed
  @subscribed
end

Class Method Details

.generate_signature(session_id, channel) ⇒ Object

Generate a HMAC signature for private channels



73
74
75
# File 'lib/viaduct/web_push/web_socket/channel.rb', line 73

def self.generate_signature(session_id, channel)
  OpenSSL::HMAC.hexdigest(OpenSSL::Digest::SHA256.new, WebPush.secret, "#{session_id}:#{channel}")
end

Instance Method Details

#bind(event, &block) ⇒ Object

Bind some code to an incoming message on this channel



25
26
27
# File 'lib/viaduct/web_push/web_socket/channel.rb', line 25

def bind(event, &block)
  @bindings[event] += [block]
end

#dispatch(event, data) ⇒ Object

Run the bindings for an event



32
33
34
35
36
# File 'lib/viaduct/web_push/web_socket/channel.rb', line 32

def dispatch(event, data)
  @bindings[event].each do |binding|
    binding.call(data)
  end
end

#global?Boolean

Blank name indicates global channel

Returns:

  • (Boolean)


18
19
20
# File 'lib/viaduct/web_push/web_socket/channel.rb', line 18

def global?
  @name.nil?
end

#inspectObject



60
61
62
63
64
65
66
67
68
# File 'lib/viaduct/web_push/web_socket/channel.rb', line 60

def inspect
  String.new.tap do |s|
    s << "#<#{self.class.name}:#{self.object_id} "
    s << [:name, :subscribed].map do |attrib|
      "#{attrib}: #{self.send(attrib).inspect}"
    end.join(', ')
    s << ">"
  end
end

#registerObject

Send request subscription for this channel from VWP



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/viaduct/web_push/web_socket/channel.rb', line 41

def register
  return if @subscibed || global?

  if @name =~ /^private-/
    signature = self.class.generate_signature(@connection.session_id, @name)
  else
    signature = nil
  end
  
  @connection.register_subscription(@name, signature)
end

#trigger(event, data = {}) ⇒ Object

Trigger an event on this channel



56
57
58
# File 'lib/viaduct/web_push/web_socket/channel.rb', line 56

def trigger(event, data = {})
  @connection.trigger(@name, event, data)
end