Method: TNetstring.parse

Defined in:
lib/tnetstring.rb

.parse(tnetstring) ⇒ Object

Converts a tnetstring into the encoded data structure.

It expects a string argument prefixed with a valid tnetstring and returns a tuple of the parsed object and any remaining string input.

Example

str = '5:12345#'
TNetstring.parse(str)

#=> [12345, '']

str = '11:hello world,abc123'

#=> ['hello world', 'abc123']


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/tnetstring.rb', line 20

def self.parse(tnetstring)
  payload, payload_type, remain = parse_payload(tnetstring)
  value = case payload_type
  when '#'
    payload.to_i
  when '^'
    payload.to_f
  when ','
    payload
  when ']'
    parse_list(payload)
  when '}'
    parse_dictionary(payload)
  when '~'
    assert payload.length == 0, "Payload must be 0 length for null"
    nil
  when '!'
    parse_boolean(payload)
  else
    assert false, "Invalid payload type: #{payload_type}"
  end
  [value, remain]
end