bencode 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,12 +5,12 @@
5
5
  #
6
6
  # == Synopsis
7
7
  #
8
- # Bencoding (pronounced _bee-encode_) is a simple protocol, consiting of
8
+ # Bencoding (pronounced <i>bee-encode</i>) is a simple protocol, consisting of
9
9
  # only 4 value types.
10
10
  #
11
11
  # === Integers
12
12
  #
13
- # An integer is encoded an _i_ followed by the numeral itself, followed
13
+ # An integer is encoded as an _i_ followed by the numeral itself, followed
14
14
  # by an _e_. Leading zeros are not allowed. Negative values are prefixed
15
15
  # with a minus sign.
16
16
  #
@@ -20,8 +20,8 @@
20
20
  #
21
21
  # === Strings
22
22
  #
23
- # Strings are a sequence of zero or more bytes. It is encoded as
24
- # _<length>:<contents>_, where _length_ is the lenth of _contents_. _length_
23
+ # Strings are sequences of zero or more bytes. They are encoded as
24
+ # <i><length>:<contents></i>, where _length_ is the length of _contents_. _length_
25
25
  # must be non-negative.
26
26
  #
27
27
  # "".bencode #=> "0:"
@@ -36,9 +36,9 @@
36
36
  #
37
37
  # === Dictionaries
38
38
  #
39
- # Dictionaries are encoded as _d<contents>e_, where _contents_ is a sequence
40
- # of keys and values. Each value is immediately preceded by a key. Keys must
41
- # be strings, and will appear in lexicographical order.
39
+ # Dictionaries are encoded as _d_ followed by a sequence of key-value pairs, followed by _e_.
40
+ # Each value must be immediately preceded by a key. Keys must be strings, and must appear in
41
+ # lexicographical order.
42
42
  #
43
43
  # {"foo" => 3, "bar" => 1, "baz" => 2}.bencode
44
44
  # #=> "d3:bari1e3:bazi2e3:fooi3ee"
@@ -67,6 +67,7 @@
67
67
  #
68
68
 
69
69
  class Object
70
+ #
70
71
  # Raises an exception. Subclasses of Object must themselves
71
72
  # define meaningful #bencode methods.
72
73
  def bencode
@@ -75,6 +76,7 @@ class Object
75
76
  end
76
77
 
77
78
  class Integer
79
+ #
78
80
  # Bencodes the Integer object. Bencoded integers are represented
79
81
  # as +ixe+, where +x+ is the integer with an optional
80
82
  # hyphen prepended, indicating negativity.
@@ -87,8 +89,9 @@ class Integer
87
89
  end
88
90
 
89
91
  class String
92
+ #
90
93
  # Bencodes the String object. Bencoded strings are represented
91
- # as +x:y+, where +y+ is the string and x is the length of the
94
+ # as +x+:+y+, where +y+ is the string and +x+ is the length of the
92
95
  # string.
93
96
  #
94
97
  # "foo".bencode #=> "3:foo"
@@ -98,6 +101,7 @@ class String
98
101
  "#{length}:#{self}"
99
102
  end
100
103
 
104
+ #
101
105
  # Bdecodes the String object and returns the data serialized
102
106
  # through bencoding.
103
107
  #
@@ -107,6 +111,7 @@ class String
107
111
  Bencode.load(self)
108
112
  end
109
113
 
114
+ #
110
115
  # Tests whether the String object is a valid bencoded string.
111
116
  def bencoded?
112
117
  bdecode
@@ -118,8 +123,9 @@ class String
118
123
  end
119
124
 
120
125
  class Array
126
+ #
121
127
  # Bencodes the Array object. Bencoded arrays are represented as
122
- # +lxe+, where x is zero or more bencoded objects.
128
+ # +lxe+, where +x+ is zero or more bencoded objects.
123
129
  #
124
130
  # [1, "foo"].bencode #=> "li1e3:fooe"
125
131
  #
@@ -129,8 +135,9 @@ class Array
129
135
  end
130
136
 
131
137
  class Hash
138
+ #
132
139
  # Bencodes the Hash object. Bencoded hashes are represented as
133
- # +dxe+, where x is zero or a power of two bencoded objects.
140
+ # +dxe+, where +x+ is zero or a power of two bencoded objects.
134
141
  # each key is immediately followed by its associated value.
135
142
  # All keys must be strings. The keys of the bencoded hash will
136
143
  # be in lexicographical order.
@@ -141,12 +148,12 @@ class Hash
141
148
  end
142
149
 
143
150
  class IO
144
- def self.bdecode(*args)
145
- new(*args).bdecode
151
+ def self.bdecode(filename)
152
+ open(filename, 'r').bdecode
146
153
  end
147
154
 
148
- def self.bencode(*args)
149
- new(*args).bencode
155
+ def self.bencode(filename)
156
+ open(filename, 'r').bencode
150
157
  end
151
158
 
152
159
  def bdecode
@@ -154,7 +161,7 @@ class IO
154
161
  end
155
162
 
156
163
  def bencode
157
- read.chomp.bdecode
164
+ read.chomp.bencode
158
165
  end
159
166
  end
160
167
 
@@ -1,7 +1,6 @@
1
1
 
2
2
  require 'test/unit'
3
- require 'rubygems'
4
- require 'bencode'
3
+ require "#{File.dirname(__FILE__)}/../lib/bencode"
5
4
 
6
5
  class BdecodeTest < Test::Unit::TestCase
7
6
  def test_string
@@ -1,7 +1,6 @@
1
1
 
2
2
  require 'test/unit'
3
- require 'rubygems'
4
- require 'bencode'
3
+ require "#{File.dirname(__FILE__)}/../lib/bencode"
5
4
 
6
5
  class BencodeTest < Test::Unit::TestCase
7
6
  def test_string
@@ -9,6 +8,11 @@ class BencodeTest < Test::Unit::TestCase
9
8
  assert_equal "0:", "".bencode
10
9
  end
11
10
 
11
+ def test_multiline_string
12
+ assert_equal "1:\n", "\n".bencode
13
+ assert_equal "2:\n\n", "\n\n".bencode
14
+ end
15
+
12
16
  def test_integer
13
17
  assert_equal "i42e", 42.bencode
14
18
  assert_equal "i-3e", -3.bencode
@@ -28,4 +32,9 @@ class BencodeTest < Test::Unit::TestCase
28
32
  assert_equal "d1:a3:foo1:g3:bar1:z3:baze",
29
33
  {"a" => "foo", "g" => "bar", "z" => "baz"}.bencode
30
34
  end
35
+
36
+ def test_hash_julien
37
+ assert_equal "d1:ai1e2:bbi1e1:ci1ee",
38
+ {'a' => 1, 'c' => 1, 'bb' => 1}.bencode
39
+ end
31
40
  end
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.0
3
3
  specification_version: 1
4
4
  name: bencode
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.3.1
7
- date: 2006-10-24 00:00:00 +02:00
6
+ version: 0.3.2
7
+ date: 2006-12-26 00:00:00 +01:00
8
8
  summary: A Ruby implementation of the Bencode encoding used by BitTorrent
9
9
  require_paths:
10
10
  - lib