tnetstring 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
data/lib/tnetstring.rb CHANGED
@@ -82,10 +82,9 @@ module TNetstring
82
82
 
83
83
  def self.parse_pair(data) # :nodoc:
84
84
  key, extra = parse(data)
85
- assert key.kind_of?(String), "Dictionary keys must be Strings"
85
+ assert key.kind_of?(String) || key.kind_of?(Symbol), "Dictionary keys must be Strings or Symbols"
86
86
  assert extra, "Unbalanced dictionary store"
87
87
  value, extra = parse(extra)
88
- assert value, "Got an invalid value, null not allowed"
89
88
 
90
89
  [key, value, extra]
91
90
  end
@@ -101,6 +100,8 @@ module TNetstring
101
100
  end
102
101
  end
103
102
 
103
+ # <b>DEPRECATED:</b> Please use <tt>dump</tt> instead.
104
+ #
104
105
  # Constructs a tnetstring out of the given object. Valid Ruby object types
105
106
  # include strings, integers, boolean values, nil, arrays, and hashes. Arrays
106
107
  # and hashes may contain any of the previous valid Ruby object types, but
@@ -109,44 +110,67 @@ module TNetstring
109
110
  # === Example
110
111
  #
111
112
  # int = 12345
112
- # TNetstring.encode(int)
113
+ # TNetstring.dump(int)
113
114
  #
114
115
  # #=> '5:12345#'
115
116
  #
116
117
  # hash = {'hello' => 'world'}
117
- # TNetstring.encode(hash)
118
+ # TNetstring.dump(hash)
118
119
  #
119
120
  # #=> '16:5:hello,5:world,}'
120
121
  #
121
122
  def self.encode(obj)
123
+ warn "[DEPRECATION] `encode` is deprecated. Please use `dump` instead."
124
+ dump obj
125
+ end
126
+
127
+ # Constructs a tnetstring out of the given object. Valid Ruby object types
128
+ # include strings, integers, boolean values, nil, arrays, and hashes. Arrays
129
+ # and hashes may contain any of the previous valid Ruby object types, but
130
+ # hash keys must be strings.
131
+ #
132
+ # === Example
133
+ #
134
+ # int = 12345
135
+ # TNetstring.dump(int)
136
+ #
137
+ # #=> '5:12345#'
138
+ #
139
+ # hash = {'hello' => 'world'}
140
+ # TNetstring.dump(hash)
141
+ #
142
+ # #=> '16:5:hello,5:world,}'
143
+ #
144
+ def self.dump(obj)
122
145
  if obj.kind_of?(Integer)
123
146
  int_str = obj.to_s
124
147
  "#{int_str.length}:#{int_str}#"
125
- elsif obj.kind_of?(String)
148
+ elsif obj.kind_of?(String) || obj.kind_of?(Symbol)
126
149
  "#{obj.length}:#{obj},"
127
- elsif obj.is_a?(TrueClass) || obj.is_a?(FalseClass)
128
- bool_str = obj.to_s
129
- "#{bool_str.length}:#{bool_str}!"
150
+ elsif obj.is_a?(TrueClass)
151
+ "4:true!"
152
+ elsif obj.is_a?(FalseClass)
153
+ "5:false!"
130
154
  elsif obj == nil
131
155
  "0:~"
132
156
  elsif obj.kind_of?(Array)
133
- encode_list(obj)
157
+ dump_list(obj)
134
158
  elsif obj.kind_of?(Hash)
135
- encode_dictionary(obj)
159
+ dump_dictionary(obj)
136
160
  else
137
161
  assert false, "Object must be of a primitive type: #{obj.inspect}"
138
162
  end
139
163
  end
140
164
 
141
- def self.encode_list(list) # :nodoc:
142
- contents = list.map {|item| encode(item)}.join
165
+ def self.dump_list(list) # :nodoc:
166
+ contents = list.map {|item| dump(item)}.join
143
167
  "#{contents.length}:#{contents}]"
144
168
  end
145
169
 
146
- def self.encode_dictionary(dict) # :nodoc:
170
+ def self.dump_dictionary(dict) # :nodoc:
147
171
  contents = dict.map do |key, value|
148
- assert key.kind_of?(String), "Dictionary keys must be Strings"
149
- "#{encode(key)}#{encode(value)}"
172
+ assert key.kind_of?(String) || key.kind_of?(Symbol), "Dictionary keys must be Strings or Symbols"
173
+ "#{dump(key)}#{dump(value)}"
150
174
  end.join
151
175
  "#{contents.length}:#{contents}}"
152
176
  end
@@ -1,5 +1,5 @@
1
1
  module TNetstring # :nodoc:
2
2
  module Version # :nodoc:
3
- STRING = '0.3.1'
3
+ STRING = '0.3.2'
4
4
  end
5
5
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 3
8
- - 1
9
- version: 0.3.1
8
+ - 2
9
+ version: 0.3.2
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-17 00:00:00 -04:00
17
+ date: 2011-06-21 00:00:00 -04:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency