simonmenke-bencoding 0.0.1 → 0.0.2
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.
- data/History.txt +7 -0
- data/Manifest.txt +4 -2
- data/lib/bencoding/hash.rb +1 -1
- data/lib/bencoding/{integer.rb → numeric.rb} +2 -2
- data/lib/bencoding/object.rb +21 -0
- data/lib/bencoding/parser.rb +11 -1
- data/lib/bencoding/symbol.rb +10 -0
- data/lib/bencoding/version.rb +1 -1
- data/lib/bencoding.rb +8 -3
- data/test/test_bencoding.rb +65 -10
- metadata +7 -5
data/History.txt
CHANGED
data/Manifest.txt
CHANGED
@@ -6,12 +6,14 @@ Rakefile
|
|
6
6
|
config/hoe.rb
|
7
7
|
config/requirements.rb
|
8
8
|
lib/bencoding.rb
|
9
|
-
lib/bencoding/version.rb
|
10
9
|
lib/bencoding/array.rb
|
11
10
|
lib/bencoding/hash.rb
|
12
|
-
lib/bencoding/
|
11
|
+
lib/bencoding/numeric.rb
|
12
|
+
lib/bencoding/object.rb
|
13
13
|
lib/bencoding/parser.rb
|
14
14
|
lib/bencoding/string.rb
|
15
|
+
lib/bencoding/symbol.rb
|
16
|
+
lib/bencoding/version.rb
|
15
17
|
script/console
|
16
18
|
script/destroy
|
17
19
|
script/generate
|
data/lib/bencoding/hash.rb
CHANGED
@@ -0,0 +1,21 @@
|
|
1
|
+
|
2
|
+
module Bencoding
|
3
|
+
module Object
|
4
|
+
|
5
|
+
def attributes_for_bencoding
|
6
|
+
instance_variables
|
7
|
+
end
|
8
|
+
|
9
|
+
def for_bencoding
|
10
|
+
attributes_for_bencoding.inject({}) do |hash, attribute|
|
11
|
+
hash[attribute.to_s[1..-1]] = instance_variable_get(attribute)
|
12
|
+
hash
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def to_bencoding
|
17
|
+
for_bencoding.to_bencoding
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
data/lib/bencoding/parser.rb
CHANGED
@@ -1,4 +1,6 @@
|
|
1
1
|
module Bencoding
|
2
|
+
class ParseError < Exception ; end
|
3
|
+
|
2
4
|
module Parser
|
3
5
|
|
4
6
|
DICTIONARY_TOKEN = 100 # d # :nodoc:
|
@@ -6,8 +8,11 @@ module Bencoding
|
|
6
8
|
INTEGER_TOKEN = 105 # i # :nodoc:
|
7
9
|
TERMINATOR_TOKEN = 101 # e # :nodoc:
|
8
10
|
SEPERATOR_TOKEN = 58 # : # :nodoc:
|
11
|
+
ZERO_TOKEN = 48 # : # :nodoc:
|
12
|
+
NINE_TOKEN = 57 # : # :nodoc:
|
9
13
|
|
10
14
|
def load(io)
|
15
|
+
io = StringIO.new(io) if io.is_a? String
|
11
16
|
parse_anytype(io)
|
12
17
|
end
|
13
18
|
|
@@ -17,15 +22,18 @@ module Bencoding
|
|
17
22
|
when DICTIONARY_TOKEN then parse_dictionary(io)
|
18
23
|
when LIST_TOKEN then parse_list(io)
|
19
24
|
when INTEGER_TOKEN then parse_integer(io)
|
20
|
-
|
25
|
+
when (ZERO_TOKEN..NINE_TOKEN)
|
21
26
|
io.ungetc typechar
|
22
27
|
parse_string(io)
|
28
|
+
else
|
29
|
+
raise ParseError
|
23
30
|
end
|
24
31
|
end
|
25
32
|
|
26
33
|
def parse_dictionary(io) # :nodoc:
|
27
34
|
dictionary = ::Hash.new
|
28
35
|
until (c = io.getc) == TERMINATOR_TOKEN
|
36
|
+
raise ParseError if c.nil?
|
29
37
|
io.ungetc c
|
30
38
|
key = parse_string(io)
|
31
39
|
val = parse_anytype(io)
|
@@ -37,6 +45,7 @@ module Bencoding
|
|
37
45
|
def parse_list(io) # :nodoc:
|
38
46
|
list = ::Array.new
|
39
47
|
until (c = io.getc) == TERMINATOR_TOKEN
|
48
|
+
raise ParseError if c.nil?
|
40
49
|
val = parse_anytype(io, c)
|
41
50
|
list << val
|
42
51
|
end
|
@@ -46,6 +55,7 @@ module Bencoding
|
|
46
55
|
def parse_integer(io, terminator=TERMINATOR_TOKEN) # :nodoc:
|
47
56
|
integer_string = ""
|
48
57
|
until (c = io.getc) == terminator
|
58
|
+
raise ParseError if c.nil?
|
49
59
|
integer_string << c.chr
|
50
60
|
end
|
51
61
|
integer_string.to_i
|
data/lib/bencoding/version.rb
CHANGED
data/lib/bencoding.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
$:.unshift(File.dirname(__FILE__)) unless
|
2
2
|
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
|
3
3
|
|
4
|
+
require "stringio"
|
5
|
+
|
4
6
|
module Bencoding
|
5
7
|
def self.load(io)
|
6
8
|
Parser.load(io)
|
@@ -13,11 +15,14 @@ end
|
|
13
15
|
require File.join(File.dirname(__FILE__), "bencoding/parser")
|
14
16
|
require File.join(File.dirname(__FILE__), "bencoding/array")
|
15
17
|
require File.join(File.dirname(__FILE__), "bencoding/hash")
|
16
|
-
require File.join(File.dirname(__FILE__), "bencoding/
|
18
|
+
require File.join(File.dirname(__FILE__), "bencoding/numeric")
|
17
19
|
require File.join(File.dirname(__FILE__), "bencoding/string")
|
20
|
+
require File.join(File.dirname(__FILE__), "bencoding/symbol")
|
21
|
+
require File.join(File.dirname(__FILE__), "bencoding/object")
|
18
22
|
|
23
|
+
Object.send :include, Bencoding::Object
|
19
24
|
Array.send :include, Bencoding::Array
|
20
25
|
Hash.send :include, Bencoding::Hash
|
21
|
-
|
26
|
+
Numeric.send :include, Bencoding::Numeric
|
22
27
|
String.send :include, Bencoding::String
|
23
|
-
|
28
|
+
Symbol.send :include, Bencoding::Symbol
|
data/test/test_bencoding.rb
CHANGED
@@ -1,17 +1,72 @@
|
|
1
|
-
require "stringio"
|
2
1
|
require File.dirname(__FILE__) + '/test_helper.rb'
|
3
2
|
|
4
3
|
class TestBencoding < Test::Unit::TestCase
|
5
|
-
|
6
|
-
def
|
4
|
+
|
5
|
+
def test_load_string
|
6
|
+
test_load "5:hello", "hello"
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_load_integer
|
10
|
+
test_load "i17e", 17
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_load_list
|
14
|
+
test_load "l5:helloi17ee", ["hello", 17]
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_load_dictionary
|
18
|
+
test_load "d5:key_ai16e5:a_key5:helloe",
|
19
|
+
{ "key_a" => 16, "a_key" => "hello" }
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_load_fake
|
23
|
+
str = "d5:key_ai16e5:a_key4:helloe"
|
24
|
+
assert_raise Bencoding::ParseError do
|
25
|
+
out = Bencoding.load(str)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_dump_string
|
30
|
+
test_dump "hello", "5:hello"
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_dump_integer
|
34
|
+
test_dump 25, "i25e"
|
7
35
|
end
|
8
36
|
|
9
|
-
def
|
10
|
-
|
11
|
-
str = "l5:hello3:byei5ed5:simoni60eee"
|
12
|
-
p str
|
13
|
-
out = Bencoding::Parser.load(StringIO.new(str))
|
14
|
-
p out
|
15
|
-
assert data == out
|
37
|
+
def test_dump_float
|
38
|
+
test_dump 25.4, "i25e"
|
16
39
|
end
|
40
|
+
|
41
|
+
def test_dump_list
|
42
|
+
test_dump([25.4, "hello"], "li25e5:helloe")
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_dump_dictionary
|
46
|
+
test_dump({ :key_a => 25.4, "a_key" => "hello"}, "d5:a_key5:hello5:key_ai25ee")
|
47
|
+
end
|
48
|
+
|
49
|
+
class DummyObject
|
50
|
+
def initialize
|
51
|
+
@name = "Simon Menke"
|
52
|
+
@job = "Web Developer"
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_dump_object
|
57
|
+
test_dump(DummyObject.new, "d3:job13:Web Developer4:name11:Simon Menkee")
|
58
|
+
end
|
59
|
+
|
60
|
+
protected
|
61
|
+
|
62
|
+
def test_load(input, output)
|
63
|
+
data = Bencoding.load(input)
|
64
|
+
assert output == data
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_dump(input, output)
|
68
|
+
data = Bencoding.dump(input)
|
69
|
+
assert data == output
|
70
|
+
end
|
71
|
+
|
17
72
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simonmenke-bencoding
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Simon Menke
|
@@ -21,7 +21,7 @@ dependencies:
|
|
21
21
|
- !ruby/object:Gem::Version
|
22
22
|
version: 1.7.0
|
23
23
|
version:
|
24
|
-
description: this gem loads and dumps bencoded data
|
24
|
+
description: this gem loads and dumps bencoded data
|
25
25
|
email:
|
26
26
|
- simon@5xm.org
|
27
27
|
executables: []
|
@@ -43,12 +43,14 @@ files:
|
|
43
43
|
- config/hoe.rb
|
44
44
|
- config/requirements.rb
|
45
45
|
- lib/bencoding.rb
|
46
|
-
- lib/bencoding/version.rb
|
47
46
|
- lib/bencoding/array.rb
|
48
47
|
- lib/bencoding/hash.rb
|
49
|
-
- lib/bencoding/
|
48
|
+
- lib/bencoding/numeric.rb
|
49
|
+
- lib/bencoding/object.rb
|
50
50
|
- lib/bencoding/parser.rb
|
51
51
|
- lib/bencoding/string.rb
|
52
|
+
- lib/bencoding/symbol.rb
|
53
|
+
- lib/bencoding/version.rb
|
52
54
|
- script/console
|
53
55
|
- script/destroy
|
54
56
|
- script/generate
|
@@ -90,7 +92,7 @@ rubyforge_project: bencoding
|
|
90
92
|
rubygems_version: 1.2.0
|
91
93
|
signing_key:
|
92
94
|
specification_version: 2
|
93
|
-
summary: this gem loads and dumps bencoded data
|
95
|
+
summary: this gem loads and dumps bencoded data
|
94
96
|
test_files:
|
95
97
|
- test/test_bencoding.rb
|
96
98
|
- test/test_helper.rb
|