bencode 0.6.0 → 0.7.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.
@@ -0,0 +1,5 @@
1
+ --no-private
2
+ --title "BEncode Documentation"
3
+ --readme README.md
4
+ lib/**/*.rb -
5
+ LICENSE
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "http://rubygems.org"
2
+
3
+ group :test do
4
+ gem "minitest", :require => "minitest/unit"
5
+ gem "shoulda", :git => "git://github.com/dasch/shoulda.git", :branch => "minitest"
6
+ end
data/README.md CHANGED
@@ -42,8 +42,9 @@ Released under the MIT license.
42
42
  Contributors
43
43
  ------------
44
44
 
45
- - Daniel Schierbeck
46
- - Mike Hodgson
45
+ - Daniel Schierbeck ([dasch](https://github.com/dasch))
46
+ - Mike Hodgson ([mikehodgson](https://github.com/mikehodgson))
47
47
  - Andrew Danforth
48
48
  - Eric Himmelreich
49
- - Allen Madsen
49
+ - Allen Madsen ([blatyo](https://github.com/blatyo))
50
+ - Ian Levesque ([ianlevesque](https://github.com/ianlevesque))
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.6.0
1
+ 0.7.0
@@ -1,9 +1,15 @@
1
1
 
2
- # TODO: Write some documentation here.
2
+ # Support for loading and dumping bencoded data.
3
+ #
4
+ # See {BEncode.load} and {BEncode.dump}.
3
5
  module BEncode
4
- VERSION = '0.5.0'
6
+ class DecodeError < StandardError
7
+ end
8
+
9
+ class EncodeError < StandardError
10
+ end
5
11
  end
6
12
 
7
13
  glob = File.join(File.dirname(__FILE__), 'bencode/**/*.rb')
8
14
 
9
- Dir[glob].sort.each{|file| require file }
15
+ Dir[glob].sort.each {|file| require file }
@@ -8,7 +8,7 @@ class Array
8
8
  #
9
9
  def bencode
10
10
  begin
11
- "l#{map{|obj| obj.bencode }.join('')}e"
11
+ "l#{map{|obj| obj.bencode }.join}e"
12
12
  rescue BEncode::EncodeError
13
13
  raise BEncode::EncodeError, "array items must be encodable"
14
14
  end
@@ -7,8 +7,8 @@ class Hash
7
7
  # All keys must be strings. The keys of the bencoded hash will
8
8
  # be in lexicographical order.
9
9
  def bencode
10
- pairs = sort.map{|key, val| [key.to_str.bencode, val.bencode] }
11
- "d#{pairs.join('')}e"
10
+ pairs = sort.map{|key, val| [key.to_s.bencode, val.bencode] }
11
+ "d#{pairs.join}e"
12
12
  rescue NoMethodError => error
13
13
  raise BEncode::EncodeError, "dictionary keys must be strings"
14
14
  end
File without changes
@@ -2,69 +2,83 @@
2
2
  require 'strscan'
3
3
 
4
4
  module BEncode
5
- class << self
6
- # BEncodes +obj+
7
- def dump(obj)
8
- obj.bencode
9
- end
5
+ # Encodes the Ruby object +obj+ into a bencoded string.
6
+ #
7
+ # @param [Hash, Array, Integer, String] obj the object to encode
8
+ # @return [String] a bencoded string
9
+ # @raise [EncodeError] if +obj+ is not a supported object type
10
+ def self.dump(obj)
11
+ obj.bencode
12
+ end
13
+
14
+ # Decodes +str+ into a Ruby structure.
15
+ #
16
+ # @param [String] str a bencoded string
17
+ # @option opts [Boolean] :ignore_trailing_junk (false) whether
18
+ # to ignore invalid bencode at the end of +str+
19
+ # @return [Object] a Ruby object
20
+ # @raise [DecodeError] if +str+ is malformed
21
+ def self.load(str, opts = {})
22
+ scanner = StringScanner.new(str)
23
+ obj = parse(scanner)
24
+ raise BEncode::DecodeError unless (opts[:ignore_trailing_junk] || scanner.eos?)
25
+ return obj
26
+ end
10
27
 
11
- # Bdecodes +str+
12
- def load(str)
13
- scanner = StringScanner.new(str)
14
- obj = parse(scanner)
15
- raise BEncode::DecodeError unless scanner.eos?
16
- return obj
17
- end
28
+ # Decodes the file located at +path+.
29
+ #
30
+ # @param [String] path path to the bencoded file
31
+ # @option (see .load)
32
+ # @return (see .load)
33
+ def self.load_file(path, opts = {})
34
+ load(File.open(path, 'rb').read, opts)
35
+ end
18
36
 
19
- # Bdecodes the file located at +path+
20
- def load_file(path)
21
- load(File.open(path, 'rb') {|f| f.read})
22
- end
37
+ private
23
38
 
24
- def parse(scanner) # :nodoc:
25
- case scanner.peek(1)[0]
26
- when ?i
27
- scanner.pos += 1
28
- num = scanner.scan_until(/e/) or raise BEncode::DecodeError
29
- return Integer(num.chop)
30
- when ?l
31
- scanner.pos += 1
32
- ary = []
33
- ary.push(parse(scanner)) until scanner.scan(/e/)
34
- return ary
35
- when ?d
36
- scanner.pos += 1
37
- hsh = {}
38
- until scanner.scan(/e/)
39
- key = parse(scanner)
39
+ def self.parse(scanner) # :nodoc:
40
+ val = case scanner.peek(1)[0]
41
+ when ?i
42
+ scanner.pos += 1
43
+ num = scanner.scan_until(/e/) or raise BEncode::DecodeError
44
+ num.chop.to_i
45
+ when ?l
46
+ scanner.pos += 1
47
+ ary = []
48
+ ary.push(parse(scanner)) until scanner.scan(/e/)
49
+ ary
50
+ when ?d
51
+ scanner.pos += 1
52
+ hsh = {}
53
+ until scanner.scan(/e/)
54
+ key = parse(scanner)
40
55
 
41
- unless key.is_a? String or key.is_a? Fixnum
42
- raise BEncode::DecodeError, "key must be a string or number"
43
- end
56
+ unless key.is_a? String or key.is_a? Fixnum
57
+ raise BEncode::DecodeError, "key must be a string or number"
58
+ end
44
59
 
45
- hsh.store(key.to_s, parse(scanner))
46
- end
47
- return hsh
48
- when ?0 .. ?9
49
- num = scanner.scan_until(/:/) or
50
- raise BEncode::DecodeError, "invalid string length (no colon)"
60
+ hsh.store(key.to_s, parse(scanner))
61
+ end
62
+ hsh
63
+ when ?0 .. ?9
64
+ num = scanner.scan_until(/:/) or
65
+ raise BEncode::DecodeError, "invalid string length (no colon)"
51
66
 
52
- begin
53
- length = Integer(num.chop)
54
- str = scanner.peek(length)
55
- scanner.pos += length
56
- rescue
57
- raise BEncode::DecodeError, "invalid string length"
58
- end
67
+ begin
68
+ length = num.chop.to_i
69
+ str = scanner.peek(length)
70
+ scanner.pos += num.chop.to_i
71
+ rescue
72
+ raise BEncode::DecodeError, "invalid string length"
73
+ end
59
74
 
60
- return str
61
- else
62
- raise BEncode::DecodeError, "Invalid specifier #{scanner.peek(1).inspect} at position #{scanner.pos+1}"
63
- end
64
- end
75
+ str
76
+ end
65
77
 
66
- private :parse
78
+ raise BEncode::DecodeError if val.nil?
79
+ val
67
80
  end
81
+
68
82
  end
69
83
 
70
84
  class String
@@ -2,10 +2,9 @@
2
2
  require 'test/environment'
3
3
 
4
4
 
5
- class BitTorrentTest < Test::Unit::TestCase
5
+ class BitTorrentTest < MiniTest::Unit::TestCase
6
6
  should "load a bencoded torrent file" do
7
- assert_nothing_raised do
8
- BEncode.load_file("test/fixtures/python.torrent")
9
- end
7
+ BEncode.load_file("test/fixtures/python.torrent")
8
+ pass
10
9
  end
11
10
  end
@@ -1,7 +1,7 @@
1
1
 
2
2
  require 'test/environment'
3
3
 
4
- class DecodingTest < Test::Unit::TestCase
4
+ class DecodingTest < MiniTest::Unit::TestCase
5
5
  context "The BEncode decoder" do
6
6
  should_decode 42, "i42e"
7
7
  should_decode 0, "i0e"
@@ -11,5 +11,7 @@ class DecodingTest < Test::Unit::TestCase
11
11
  should_decode "", "0:"
12
12
 
13
13
  should_decode [1, 2, 3], "li1ei2ei3ee"
14
+
15
+ should_decode 42, "i42eBOGUS", :ignore_trailing_junk => true
14
16
  end
15
17
  end
@@ -1,7 +1,7 @@
1
1
 
2
2
  require 'test/environment'
3
3
 
4
- class EncodingTest < Test::Unit::TestCase
4
+ class EncodingTest < MiniTest::Unit::TestCase
5
5
  context "The BEncode encoder" do
6
6
  should_encode "i42e", 42
7
7
  should_encode "3:foo", "foo"
@@ -1,7 +1,11 @@
1
1
 
2
2
  require 'rubygems'
3
- require 'test/unit'
4
- require 'shoulda'
3
+ require 'bundler/setup'
4
+
5
+ Bundler.require(:default, :test)
6
+
7
+ MiniTest::Unit.autorun
8
+
5
9
  require File.dirname(__FILE__) + '/../lib/bencode'
6
10
  Dir.glob(File.dirname(__FILE__) + '/shoulda_macros/*.rb').each do |macro|
7
11
  require macro
@@ -1,8 +1,8 @@
1
1
 
2
- class Test::Unit::TestCase
3
- def self.should_decode(expected, encoded)
2
+ class MiniTest::Unit::TestCase
3
+ def self.should_decode(expected, encoded, opts = {})
4
4
  should "decode #{encoded.inspect} into #{expected.inspect}" do
5
- assert_equal expected, BEncode.load(encoded)
5
+ assert_equal expected, BEncode.load(encoded, opts)
6
6
  end
7
7
  end
8
8
  end
@@ -1,5 +1,5 @@
1
1
 
2
- class Test::Unit::TestCase
2
+ class MiniTest::Unit::TestCase
3
3
  def self.should_encode(expected, value)
4
4
  should "encode #{value.inspect} into #{expected.inspect}" do
5
5
  assert_equal expected, value.bencode
@@ -1,12 +1,11 @@
1
1
 
2
2
  require 'test/environment'
3
3
 
4
- class UTF8DecodingTest < Test::Unit::TestCase
4
+ class UTF8DecodingTest < MiniTest::Unit::TestCase
5
5
  context "The BEncode decoder" do
6
6
  should "be able to handle UTF8-encoded data" do
7
- assert_nothing_raised do
8
- BEncode.load_file('test/fixtures/test.torrent')
9
- end
7
+ BEncode.load_file('test/fixtures/test.torrent')
8
+ pass
10
9
  end
11
10
  end
12
11
  end
metadata CHANGED
@@ -1,7 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bencode
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ hash: 3
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 7
9
+ - 0
10
+ version: 0.7.0
5
11
  platform: ruby
6
12
  authors:
7
13
  - Daniel Schierbeck
@@ -9,8 +15,7 @@ autorequire:
9
15
  bindir: bin
10
16
  cert_chain: []
11
17
 
12
- date: 2010-02-06 00:00:00 +01:00
13
- default_executable:
18
+ date: 2011-06-15 00:00:00 Z
14
19
  dependencies: []
15
20
 
16
21
  description: A simple encoder and decoder for the BitTorrent serialization format.
@@ -23,21 +28,20 @@ extra_rdoc_files:
23
28
  - LICENSE
24
29
  - README.md
25
30
  files:
26
- - .gitignore
31
+ - .yardopts
32
+ - Gemfile
27
33
  - LICENSE
28
34
  - README.md
29
35
  - Rakefile
30
36
  - VERSION
31
37
  - lib/bencode.rb
38
+ - lib/bencode/core_ext/array.rb
39
+ - lib/bencode/core_ext/hash.rb
40
+ - lib/bencode/core_ext/integer.rb
41
+ - lib/bencode/core_ext/io.rb
42
+ - lib/bencode/core_ext/object.rb
43
+ - lib/bencode/core_ext/string.rb
32
44
  - lib/bencode/decode.rb
33
- - lib/bencode/decode_error.rb
34
- - lib/bencode/encode/array.rb
35
- - lib/bencode/encode/hash.rb
36
- - lib/bencode/encode/integer.rb
37
- - lib/bencode/encode/object.rb
38
- - lib/bencode/encode/string.rb
39
- - lib/bencode/encode_error.rb
40
- - lib/bencode/io.rb
41
45
  - test/benchmark/decoding.rb
42
46
  - test/benchmark/encoding.rb
43
47
  - test/bittorrent_test.rb
@@ -49,41 +53,38 @@ files:
49
53
  - test/shoulda_macros/decoding.rb
50
54
  - test/shoulda_macros/encoding.rb
51
55
  - test/utf8_decoding_test.rb
52
- has_rdoc: true
53
56
  homepage: http://github.com/dasch/ruby-bencode
54
57
  licenses: []
55
58
 
56
59
  post_install_message:
57
- rdoc_options:
58
- - --charset=UTF-8
60
+ rdoc_options: []
61
+
59
62
  require_paths:
60
63
  - lib
61
64
  required_ruby_version: !ruby/object:Gem::Requirement
65
+ none: false
62
66
  requirements:
63
67
  - - ">="
64
68
  - !ruby/object:Gem::Version
69
+ hash: 3
70
+ segments:
71
+ - 0
65
72
  version: "0"
66
- version:
67
73
  required_rubygems_version: !ruby/object:Gem::Requirement
74
+ none: false
68
75
  requirements:
69
76
  - - ">="
70
77
  - !ruby/object:Gem::Version
78
+ hash: 3
79
+ segments:
80
+ - 0
71
81
  version: "0"
72
- version:
73
82
  requirements: []
74
83
 
75
84
  rubyforge_project:
76
- rubygems_version: 1.3.5
85
+ rubygems_version: 1.8.5
77
86
  signing_key:
78
87
  specification_version: 3
79
88
  summary: Encode and decode bencoded data
80
- test_files:
81
- - test/bittorrent_test.rb
82
- - test/decoding_test.rb
83
- - test/utf8_decoding_test.rb
84
- - test/shoulda_macros/decoding.rb
85
- - test/shoulda_macros/encoding.rb
86
- - test/environment.rb
87
- - test/encoding_test.rb
88
- - test/benchmark/decoding.rb
89
- - test/benchmark/encoding.rb
89
+ test_files: []
90
+
data/.gitignore DELETED
@@ -1,3 +0,0 @@
1
- doc
2
- *.gem
3
- .idea
@@ -1,4 +0,0 @@
1
-
2
- unless BEncode.const_defined? :DecodeError
3
- BEncode::DecodeError = Class.new(StandardError)
4
- end
@@ -1,4 +0,0 @@
1
-
2
- unless BEncode.const_defined? :EncodeError
3
- BEncode::EncodeError = Class.new(StandardError)
4
- end