Class: Viaduct::WebPush::Channel

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Channel

Returns a new instance of Channel.



9
10
11
# File 'lib/viaduct/web_push/channel.rb', line 9

def initialize(name)
  @name = name
end

Instance Attribute Details

#bindingsObject

Returns the value of attribute bindings.



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

def bindings
  @bindings
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#subscribedObject

Returns the value of attribute subscribed.



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

def subscribed
  @subscribed
end

Class Method Details

.generate_signature(session_id, channel) ⇒ Object

Generate a HMAC signature for private channels



54
55
56
# File 'lib/viaduct/web_push/channel.rb', line 54

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

.multi_trigger(channels, event, data = {}) ⇒ Object

Trigger an event on multiple channels simultaneously

Raises:



46
47
48
49
# File 'lib/viaduct/web_push/channel.rb', line 46

def self.multi_trigger(channels, event, data = {})
  raise Error, "`channels` must an arrayof strings" unless channels.all? { |c| c.is_a?(String) }
  WebPush.request('trigger', {:channel => channels.join(','), :event => event, :data => data.to_json})
end

.trigger(channel, event, data = {}) ⇒ Object

Trigger a single even on a given channel



39
40
41
# File 'lib/viaduct/web_push/channel.rb', line 39

def self.trigger(channel, event, data = {})
  WebPush.request('trigger', {:channel => channel, :event => event, :data => data.to_json})
end

Instance Method Details

#global?Boolean

Blank name indicates global channel

Returns:

  • (Boolean)


16
17
18
# File 'lib/viaduct/web_push/channel.rb', line 16

def global?
  @name.nil?
end

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

Trigger an event on this channel



23
24
25
# File 'lib/viaduct/web_push/channel.rb', line 23

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

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

Trigger an event on the channel in its own thread



30
31
32
33
34
# File 'lib/viaduct/web_push/channel.rb', line 30

def trigger!(event, data = {})
  Thread.new do
    self.class.trigger(@name, event, data)
  end
end