tnetstring 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/tnetstring.rb +74 -0
  2. metadata +82 -0
data/lib/tnetstring.rb ADDED
@@ -0,0 +1,74 @@
1
+ module TNetstring
2
+ def self.parse(tnetstring)
3
+ parse_tnetstring(tnetstring)[0]
4
+ end
5
+
6
+ def self.parse_tnetstring(data)
7
+ payload, payload_type, remain = parse_payload(data)
8
+ value = case payload_type
9
+ when '#'
10
+ payload.to_i
11
+ when '"'
12
+ payload
13
+ when ']'
14
+ parse_list(payload)
15
+ when '}'
16
+ parse_dictionary(payload)
17
+ else
18
+ assert false, "Invalid payload type: #{payload_type}"
19
+ end
20
+ [value, remain]
21
+ end
22
+
23
+ def self.parse_payload(data)
24
+ assert data, "Invalid data to parse, it's empty."
25
+ length, extra = data.split(':', 2)
26
+ length = length.to_i
27
+
28
+ payload, extra = extra[0, length], extra[length..-1]
29
+ assert extra, "No payload type: %s, %s" % [payload, extra]
30
+ payload_type, remain = extra[0], extra[1..-1]
31
+
32
+ assert payload.length == length, "Data is wrong length %d vs %d" % [length, payload.length]
33
+ [payload, payload_type, remain]
34
+ end
35
+
36
+ def self.parse_list(data)
37
+ return [] if data.length == 0
38
+ list = []
39
+ value, remain = parse_tnetstring(data)
40
+ list << value
41
+
42
+ while remain.length > 0
43
+ value, remain = parse_tnetstring(remain)
44
+ list << value
45
+ end
46
+ list
47
+ end
48
+
49
+ def self.parse_dictionary(data)
50
+ return {} if data.length == 0
51
+
52
+ key, value, extra = parse_pair(data)
53
+ result = {key => value}
54
+
55
+ while extra.length > 0
56
+ key, value, extra = parse_pair(extra)
57
+ result[key] = value
58
+ end
59
+ result
60
+ end
61
+
62
+ def self.parse_pair(data)
63
+ key, extra = parse_tnetstring(data)
64
+ assert extra, "Unbalanced dictionary store."
65
+ value, extra = parse_tnetstring(extra)
66
+ assert value, "Got an invalid value, null not allowed."
67
+
68
+ [key, value, extra]
69
+ end
70
+
71
+ def self.assert(truthy, message)
72
+ raise message unless truthy
73
+ end
74
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tnetstring
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Matt Yoho
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-04-12 00:00:00 +01:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 2
30
+ - 5
31
+ - 0
32
+ version: 2.5.0
33
+ type: :development
34
+ version_requirements: *id001
35
+ description: Ruby implementation of Zed Shaw's typed netstring experiment, a simple data interchange format better suited to low-level network communication than JSON.
36
+ email: mby@mattyoho.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files: []
42
+
43
+ files:
44
+ - lib/tnetstring.rb
45
+ has_rdoc: true
46
+ homepage: http://github.com/mattyoho/tnetstring-rb
47
+ licenses: []
48
+
49
+ post_install_message:
50
+ rdoc_options: []
51
+
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ segments:
60
+ - 1
61
+ - 8
62
+ - 7
63
+ version: 1.8.7
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ segments:
70
+ - 1
71
+ - 3
72
+ - 7
73
+ version: 1.3.7
74
+ requirements: []
75
+
76
+ rubyforge_project:
77
+ rubygems_version: 1.3.7
78
+ signing_key:
79
+ specification_version: 3
80
+ summary: Ruby implementation of Zed Shaw's typed netstring experiment, a simple data interchange format better suited to low-level network communication than JSON.
81
+ test_files: []
82
+