Class: KHL::WebSocket::Message

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

Constant Summary collapse

TYPES =
%i[
  event
  hello
  ping
  pong
  resume
  reconnect
  resume_ack
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type: nil, data: nil, sn: nil) ⇒ Message

Returns a new instance of Message.



29
30
31
32
33
# File 'lib/khl/web_socket/message.rb', line 29

def initialize(type: nil, data: nil, sn: nil)
  @type = type
  @data = data
  @sn = sn
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



16
17
18
# File 'lib/khl/web_socket/message.rb', line 16

def data
  @data
end

#snObject

Returns the value of attribute sn.



16
17
18
# File 'lib/khl/web_socket/message.rb', line 16

def sn
  @sn
end

#typeObject

Returns the value of attribute type.



16
17
18
# File 'lib/khl/web_socket/message.rb', line 16

def type
  @type
end

Class Method Details

.parse(raw) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/khl/web_socket/message.rb', line 18

def self.parse(raw)
  return unless raw

  data = JSON.parse(raw)
  message = Message.new
  message.type = TYPES[data["s"]]
  message.data = data["d"]
  message.sn = data["sn"]
  message
end

Instance Method Details

#to_hObject



35
36
37
38
39
40
41
# File 'lib/khl/web_socket/message.rb', line 35

def to_h
  {
    s: TYPES.index(type),
    d: data,
    sn: sn
  }
end

#to_jsonObject



43
44
45
# File 'lib/khl/web_socket/message.rb', line 43

def to_json
  to_h.to_json
end