Class: Pusher::IO::Channel

Inherits:
Object
  • Object
show all
Defined in:
lib/pusher.io.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Channel

Returns a new instance of Channel.



14
15
16
# File 'lib/pusher.io.rb', line 14

def initialize name
  @name = name.to_s
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



12
13
14
# File 'lib/pusher.io.rb', line 12

def name
  @name
end

Instance Method Details

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



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/pusher.io.rb', line 18

def trigger event, data={}
  cfg = Pusher::IO.config

  msg = {
    app_id: cfg[:app_id].to_s,
    channel: name,
    event: event.to_s,
    data: data,
    timestamp: Time.now.utc.to_i
  }

  sig = Pusher::IO.psignature([
    'POST',
    'events',
    msg[:app_id],
    msg[:channel],
    msg[:event],
    msg[:timestamp],
    cfg[:key],
    cfg[:host]
  ])

  msg[:signature] = sig

  form = Net::HTTP::Post.new('/events')
  form["Content-Type"] = "application/json"
  form.body = msg.to_json

  http = Net::HTTP.new(cfg[:host], cfg[:port])
  http.use_ssl = cfg[:ssl_key].present?
  http.start {|h| h.request(form)}
end