Class: VtacFriendly::VtacPacket
- Inherits:
-
Object
- Object
- VtacFriendly::VtacPacket
- 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
- #[](k) ⇒ Object
-
#initialize(type = :response, contents = "", from_packet: nil) ⇒ VtacPacket
constructor
A new instance of VtacPacket.
- #to_h ⇒ Object
- #to_json ⇒ Object
- #to_s ⇒ Object
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_h ⇒ Object
21 22 23 |
# File 'lib/vtac_friendly/vtac_packet.rb', line 21 def to_h return {type: @type.to_s, contents: @contents} end |
#to_json ⇒ Object
29 30 31 |
# File 'lib/vtac_friendly/vtac_packet.rb', line 29 def to_json return self.to_h.to_json end |
#to_s ⇒ Object
25 26 27 |
# File 'lib/vtac_friendly/vtac_packet.rb', line 25 def to_s return self.to_json.to_s end |