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 CHANGED
@@ -1,3 +1,10 @@
1
+ == 0.0.2 2008-09-01
2
+
3
+ * 3 major enhancement:
4
+ * Objects can be dumped
5
+ * Better tests
6
+ * Better error reporting
7
+
1
8
  == 0.0.1 2008-08-19
2
9
 
3
10
  * 1 major enhancement:
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/integer.rb
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
@@ -3,7 +3,7 @@ module Bencoding
3
3
  module Hash
4
4
 
5
5
  def to_bencoding
6
- "d"+sort.collect{|key, item|key.to_bencoding+item.to_bencoding}.join+"e"
6
+ "d"+sort{|a,b|a[0].to_s<=>b[0].to_s}.collect{|key, item|key.to_bencoding+item.to_bencoding}.join+"e"
7
7
  end
8
8
 
9
9
  end
@@ -1,9 +1,9 @@
1
1
 
2
2
  module Bencoding
3
- module Integer
3
+ module Numeric
4
4
 
5
5
  def to_bencoding
6
- "i"+to_s+"e"
6
+ "i"+to_i.to_s+"e"
7
7
  end
8
8
 
9
9
  end
@@ -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
@@ -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
- else
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
@@ -0,0 +1,10 @@
1
+
2
+ module Bencoding
3
+ module Symbol
4
+
5
+ def to_bencoding
6
+ self.to_s.to_bencoding
7
+ end
8
+
9
+ end
10
+ end
@@ -2,7 +2,7 @@ module Bencoding
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 0
4
4
  MINOR = 0
5
- TINY = 1
5
+ TINY = 2
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
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/integer")
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
- Integer.send :include, Bencoding::Integer
26
+ Numeric.send :include, Bencoding::Numeric
22
27
  String.send :include, Bencoding::String
23
-
28
+ Symbol.send :include, Bencoding::Symbol
@@ -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 setup
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 test_truth
10
- data = ["hello", "bye", 5, { "simon" => 60 }]
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.1
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/integer.rb
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