Class: Garufa::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/garufa/message.rb

Constant Summary collapse

ACTIVITY_TIMEOUT =
120
ATTRIBUTES =
[:channels, :channel, :event, :data, :name, :socket_id]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Message

Returns a new instance of Message.



10
11
12
13
14
15
16
17
18
19
# File 'lib/garufa/message.rb', line 10

def initialize(attributes)
  @attributes = ATTRIBUTES.each_with_object({}) do |key, hash|
    hash[key] = attributes[key] || attributes[key.to_s]
  end

  @attributes.each do |name, value|
    instance_variable_set("@#{name}", value)
    self.class.send(:attr_reader, name)
  end
end

Class Method Details

.channel_event(channel, event, data) ⇒ Object



25
26
27
# File 'lib/garufa/message.rb', line 25

def self.channel_event(channel, event, data)
   new(channel: channel, event: event, data: data)
end

.connection_established(socket_id) ⇒ Object



29
30
31
32
# File 'lib/garufa/message.rb', line 29

def self.connection_established(socket_id)
  data = { socket_id: socket_id, activity_timeout: ACTIVITY_TIMEOUT }.to_json
  new(event: 'pusher:connection_established', data: data)
end

.error(code, message) ⇒ Object



42
43
44
45
# File 'lib/garufa/message.rb', line 42

def self.error(code, message)
  data = { code: code, message: message }.to_json
  new(event: 'pusher:error', data: data)
end

.pongObject



38
39
40
# File 'lib/garufa/message.rb', line 38

def self.pong
  new(event: 'pusher:pong', data: {})
end

.subscription_succeeded(channel) ⇒ Object



34
35
36
# File 'lib/garufa/message.rb', line 34

def self.subscription_succeeded(channel)
  new(event: 'pusher_internal:subscription_succeeded', channel: channel, data: {})
end

Instance Method Details

#to_jsonObject



21
22
23
# File 'lib/garufa/message.rb', line 21

def to_json
  @attributes.delete_if { |k, v| v.nil? }.to_json
end