Class: VtacFriendly::VtacPacket

Inherits:
Object
  • Object
show all
Defined in:
lib/vtac_friendly/vtac_packet.rb

Constant Summary collapse

@@allowed_types =
[:command, :response, :id_server, :id_client, :error, :disconnect, :password]

Instance Method Summary collapse

Constructor Details

#initialize(type = :response, contents = "", from_packet: nil) ⇒ VtacPacket

Returns a new instance of VtacPacket.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/vtac_friendly/vtac_packet.rb', line 5

def initialize(type=:response, contents="", from_packet:nil)
  if !from_packet
    if @@allowed_types.include?(type)
      @type = type
      @contents = contents.to_s
    end
  else
    packet = from_packet
    packet = JSON.parse(packet)
    @type = packet["type"]
    @contents = packet["contents"]
  end

  return self
end

Instance Method Details

#[](k) ⇒ Object



33
34
35
# File 'lib/vtac_friendly/vtac_packet.rb', line 33

def [](k)
  return self.to_h[k.to_sym]
end

#to_hObject



21
22
23
# File 'lib/vtac_friendly/vtac_packet.rb', line 21

def to_h
  return {type: @type.to_s, contents: @contents}
end

#to_jsonObject



29
30
31
# File 'lib/vtac_friendly/vtac_packet.rb', line 29

def to_json
  return self.to_h.to_json
end

#to_sObject



25
26
27
# File 'lib/vtac_friendly/vtac_packet.rb', line 25

def to_s
  return self.to_json.to_s
end