tnetstring 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
1
1
  module TNetstring # :nodoc:
2
2
  module Version # :nodoc:
3
- STRING = '0.2.0'
3
+ STRING = '0.3.0'
4
4
  end
5
5
  end
data/lib/tnetstring.rb CHANGED
@@ -1,10 +1,6 @@
1
1
  module TNetstring
2
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)
3
+ payload, payload_type, remain = parse_payload(tnetstring)
8
4
  value = case payload_type
9
5
  when '#'
10
6
  payload.to_i
@@ -43,11 +39,11 @@ module TNetstring
43
39
  def self.parse_list(data)
44
40
  return [] if data.length == 0
45
41
  list = []
46
- value, remain = parse_tnetstring(data)
42
+ value, remain = parse(data)
47
43
  list << value
48
44
 
49
45
  while remain.length > 0
50
- value, remain = parse_tnetstring(remain)
46
+ value, remain = parse(remain)
51
47
  list << value
52
48
  end
53
49
  list
@@ -67,9 +63,9 @@ module TNetstring
67
63
  end
68
64
 
69
65
  def self.parse_pair(data)
70
- key, extra = parse_tnetstring(data)
66
+ key, extra = parse(data)
71
67
  assert extra, "Unbalanced dictionary store."
72
- value, extra = parse_tnetstring(extra)
68
+ value, extra = parse(extra)
73
69
  assert value, "Got an invalid value, null not allowed."
74
70
 
75
71
  [key, value, extra]
@@ -86,6 +82,39 @@ module TNetstring
86
82
  end
87
83
  end
88
84
 
85
+ def self.encode(obj)
86
+ if obj.kind_of?(Integer)
87
+ int_str = obj.to_s
88
+ "#{int_str.length}:#{int_str}#"
89
+ elsif obj.kind_of?(String)
90
+ "#{obj.length}:#{obj},"
91
+ elsif obj.is_a?(TrueClass) || obj.is_a?(FalseClass)
92
+ bool_str = obj.to_s
93
+ "#{bool_str.length}:#{bool_str}!"
94
+ elsif obj == nil
95
+ "0:~"
96
+ elsif obj.kind_of?(Array)
97
+ encode_list(obj)
98
+ elsif obj.kind_of?(Hash)
99
+ encode_dictionary(obj)
100
+ else
101
+ assert false, "Object must be of a primitive type"
102
+ end
103
+ end
104
+
105
+ def self.encode_list(list)
106
+ contents = list.map {|item| encode(item)}.join
107
+ "#{contents.length}:#{contents}]"
108
+ end
109
+
110
+ def self.encode_dictionary(dict)
111
+ contents = dict.map do |key, value|
112
+ assert key.kind_of?(String), "Dictionary keys must be Strings"
113
+ "#{encode(key)}#{encode(value)}"
114
+ end.join
115
+ "#{contents.length}:#{contents}}"
116
+ end
117
+
89
118
  def self.assert(truthy, message)
90
119
  raise message unless truthy
91
120
  end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 2
7
+ - 3
8
8
  - 0
9
- version: 0.2.0
9
+ version: 0.3.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Matt Yoho
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-04-13 00:00:00 +01:00
17
+ date: 2011-04-16 00:00:00 -04:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -47,7 +47,7 @@ dependencies:
47
47
  version: 1.0.12
48
48
  type: :development
49
49
  version_requirements: *id002
50
- description: Ruby implementation of the typed netstring specification, a simple data interchange format better suited to low-level network communication than JSON. See http://tnetstrings.org/ for more details.
50
+ description: Ruby implementation of the tagged netstring specification, a simple data interchange format better suited to low-level network communication than JSON. See http://tnetstrings.org/ for more details.
51
51
  email: mby@mattyoho.com
52
52
  executables: []
53
53
 
@@ -93,6 +93,6 @@ rubyforge_project:
93
93
  rubygems_version: 1.3.7
94
94
  signing_key:
95
95
  specification_version: 3
96
- summary: Ruby implementation of the typed netstring specification.
96
+ summary: Ruby implementation of the tagged netstring specification.
97
97
  test_files: []
98
98