bencodr 3.0.0 → 3.0.1
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/.rvmrc +1 -1
- data/.travis.yml +5 -0
- data/README.md +5 -3
- data/Rakefile +3 -1
- data/bencodr.gemspec +3 -2
- data/lib/bencodr/io.rb +6 -6
- data/lib/bencodr/parser.rb +5 -1
- data/lib/bencodr/version.rb +1 -1
- data/spec/bencodr/io_spec.rb +45 -22
- data/spec/custom_matchers.rb +9 -6
- data/spec/shared_examples.rb +28 -11
- data/spec/spec_helper.rb +4 -2
- metadata +59 -53
data/.rvmrc
CHANGED
@@ -1 +1 @@
|
|
1
|
-
rvm --create use 1.9.
|
1
|
+
rvm --create use 1.9.3@bencodr
|
data/README.md
CHANGED
@@ -4,12 +4,11 @@
|
|
4
4
|
* **Gem** http://gemcutter.org/gems/bencodr
|
5
5
|
* **Source** http://github.com/blatyo/bencodr
|
6
6
|
* **Issue Tracker** http://github.com/blatyo/bencodr/issues
|
7
|
+
* **Build Status** [](http://travis-ci.org/blatyo/bencodr)
|
7
8
|
|
8
9
|
## Synopsis
|
9
10
|
This gem provides a way to encode and parse bencodings used by the Bit Torrent protocol.
|
10
11
|
|
11
|
-
_Note: If using ruby 1.8.x, use bencodr version 2.1.0. 3.0.0 is 1.9.x only, because it uses the 1.9.x encoding features._
|
12
|
-
|
13
12
|
## Installation
|
14
13
|
|
15
14
|
Install the gem:
|
@@ -165,7 +164,7 @@ You can also write and read bencodings.
|
|
165
164
|
# read from file
|
166
165
|
File.bdecode("a.bencode") #=> "string"
|
167
166
|
|
168
|
-
file = File.open("a.bencode", "
|
167
|
+
file = File.open("a.bencode", "rb")
|
169
168
|
file.bdecode #=> "string"
|
170
169
|
```
|
171
170
|
|
@@ -197,6 +196,9 @@ When using bencodings it may be useful to translate your own objects into bencod
|
|
197
196
|
MyClass.new.bencode #=> "d1:a1:a1:b1:be"
|
198
197
|
```
|
199
198
|
|
199
|
+
## Contributors
|
200
|
+
* [Andrew Nikolaev](https://github.com/quaternion) - Fixed encoding issue
|
201
|
+
|
200
202
|
## Note on Reporting Issues
|
201
203
|
|
202
204
|
* Try to make a failing test case
|
data/Rakefile
CHANGED
data/bencodr.gemspec
CHANGED
@@ -13,9 +13,10 @@ Gem::Specification.new do |s|
|
|
13
13
|
s.description = "This gem provides a way to encode and decode bencode used by the Bit Torrent protocol. Normal ruby objects can be marshalled as bencode and demarshalled back to ruby."
|
14
14
|
|
15
15
|
s.required_rubygems_version = ">= 1.3.6"
|
16
|
-
s.add_development_dependency "
|
16
|
+
s.add_development_dependency "rake"
|
17
|
+
s.add_development_dependency "rspec", "~> 2.8.0"
|
17
18
|
s.add_development_dependency "bundler", ">= 1.0.0"
|
18
|
-
s.add_development_dependency "fuubar", ">= 0.0
|
19
|
+
s.add_development_dependency "fuubar", ">= 1.0.0"
|
19
20
|
|
20
21
|
s.files = `git ls-files`.split("\n")
|
21
22
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
data/lib/bencodr/io.rb
CHANGED
@@ -6,22 +6,22 @@ module BEncodr
|
|
6
6
|
def bencode(fd, object)
|
7
7
|
open(fd, "wb") {|file| file.bencode(object)}
|
8
8
|
end
|
9
|
-
|
9
|
+
|
10
10
|
def bdecode(fd)
|
11
11
|
open(fd, "rb") {|file| file.bdecode}
|
12
12
|
end
|
13
13
|
end
|
14
|
-
|
14
|
+
|
15
15
|
def bencode(object)
|
16
16
|
write(Object.bencode(object))
|
17
17
|
end
|
18
|
-
|
18
|
+
|
19
19
|
def bdecode
|
20
|
-
Object.bdecode(read
|
20
|
+
Object.bdecode(read)
|
21
21
|
end
|
22
|
-
|
22
|
+
|
23
23
|
def self.included(base)
|
24
24
|
base.extend ClassMethods
|
25
25
|
end
|
26
26
|
end
|
27
|
-
end
|
27
|
+
end
|
data/lib/bencodr/parser.rb
CHANGED
@@ -38,7 +38,11 @@ module BEncodr
|
|
38
38
|
length = scanner.scan(/[1-9][0-9]*|0/) or raise BEncodeError, "Invalid string: length invalid. #{scanner.pos}"
|
39
39
|
scanner.scan(/:/) or raise BEncodeError, "Invalid string: missing colon(:). #{scanner.pos}"
|
40
40
|
byte_string = scanner.scan(/.{#{length}}/m) or raise BEncodeError, "Invalid string: length too long(#{length}) #{scanner.pos}."
|
41
|
-
|
41
|
+
if RUBY_VERSION =~ /1\.9/
|
42
|
+
byte_string.encode('UTF-8') rescue byte_string.force_encoding('UTF-8')
|
43
|
+
else
|
44
|
+
byte_string
|
45
|
+
end
|
42
46
|
end
|
43
47
|
|
44
48
|
# This method parases a bencoded integer.
|
data/lib/bencodr/version.rb
CHANGED
data/spec/bencodr/io_spec.rb
CHANGED
@@ -1,35 +1,58 @@
|
|
1
1
|
# encoding: UTF-8
|
2
2
|
require "spec_helper"
|
3
|
+
require 'tempfile'
|
3
4
|
|
4
5
|
describe File do
|
5
6
|
before :all do
|
6
|
-
@path = "tmp"
|
7
|
-
Dir.mkdir(@path) unless File.exists? @path
|
8
7
|
BEncodr.include!
|
9
8
|
end
|
10
9
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
10
|
+
let(:file) { Tempfile.new('test.bencodr') }
|
11
|
+
let(:object) { "string" }
|
12
|
+
|
13
|
+
describe ".bencode" do
|
14
|
+
it "should encode object to file" do
|
15
|
+
File.bencode(file.path, object)
|
16
|
+
file.rewind
|
17
|
+
file.read.should == "6:string"
|
18
|
+
end
|
15
19
|
end
|
16
|
-
|
20
|
+
|
17
21
|
describe "#bencode" do
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
describe "#bdecode" do
|
24
|
-
subject{ File }
|
25
|
-
it{ should bdecode(@file).to(@object) }
|
26
|
-
end
|
27
|
-
|
28
|
-
after :each do
|
29
|
-
File.delete(@file)
|
22
|
+
it "should encode object to file" do
|
23
|
+
file.bencode(object)
|
24
|
+
file.rewind
|
25
|
+
file.read.should == "6:string"
|
26
|
+
end
|
30
27
|
end
|
31
28
|
|
32
|
-
|
33
|
-
|
29
|
+
context "decode" do
|
30
|
+
let(:sample_path) { 'spec/samples/bencodr.torrent' }
|
31
|
+
|
32
|
+
before do
|
33
|
+
file.bencode(object)
|
34
|
+
file.rewind
|
35
|
+
end
|
36
|
+
|
37
|
+
describe ".bdecode" do
|
38
|
+
it "should decode object from file" do
|
39
|
+
File.bdecode(file.path).should == object
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should decode sample file without error" do
|
43
|
+
expect{ File.bdecode(sample_path) }.to_not raise_error
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
describe "#bdecode" do
|
48
|
+
it "should decode object from file" do
|
49
|
+
file.bdecode.should == object
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should decode sample file without error" do
|
53
|
+
f = File.open(sample_path, 'rb')
|
54
|
+
expect{ f.bdecode }.to_not raise_error
|
55
|
+
end
|
56
|
+
end
|
34
57
|
end
|
35
|
-
end
|
58
|
+
end
|
data/spec/custom_matchers.rb
CHANGED
@@ -1,10 +1,11 @@
|
|
1
1
|
RSpec::Matchers.define :bencode_to do |expected|
|
2
2
|
match do |actual|
|
3
|
-
actual.bencode
|
3
|
+
@got = actual.bencode
|
4
|
+
@got.should equal(expected)
|
4
5
|
end
|
5
6
|
|
6
7
|
failure_message_for_should do |actual|
|
7
|
-
"expected that #{actual} would bencode to #{expected}"
|
8
|
+
"expected that #{actual.inspect} would bencode to #{expected.inspect}, but got #{@got.inspect}}"
|
8
9
|
end
|
9
10
|
end
|
10
11
|
|
@@ -14,11 +15,12 @@ RSpec::Matchers.define :bencode do |actual|
|
|
14
15
|
end
|
15
16
|
|
16
17
|
match do |klass|
|
17
|
-
klass.bencode(actual)
|
18
|
+
@got = klass.bencode(actual)
|
19
|
+
@got.should == @_expected
|
18
20
|
end
|
19
21
|
|
20
22
|
failure_message_for_should do |klass|
|
21
|
-
"expected #{klass.name} to bencode #{actual} to #{@_expected}"
|
23
|
+
"expected #{klass.name} to bencode #{actual.inspect} to #{@_expected.inspect}, but got #{@got.inspect}"
|
22
24
|
end
|
23
25
|
end
|
24
26
|
|
@@ -33,11 +35,12 @@ RSpec::Matchers.define :parse do |actual|
|
|
33
35
|
|
34
36
|
match do |klass|
|
35
37
|
scanner = StringScanner.new(actual)
|
36
|
-
klass.send(:"parse_#{@type}", scanner)
|
38
|
+
@got = klass.send(:"parse_#{@type}", scanner)
|
39
|
+
@got.should == @_expected
|
37
40
|
end
|
38
41
|
|
39
42
|
failure_message_for_should do |klass|
|
40
|
-
"expected #{klass.name} to bdencode #{actual} as #{@type} to #{@_expected}"
|
43
|
+
"expected #{klass.name} to bdencode #{actual.inspect} as #{@type} to #{@_expected.inspect}, but got #{@got.inspect}"
|
41
44
|
end
|
42
45
|
end
|
43
46
|
|
data/spec/shared_examples.rb
CHANGED
@@ -53,21 +53,38 @@ shared_examples_for "BEncodr::Dictionary" do |obj|
|
|
53
53
|
it{ should bencode({:symbol => :symbol}).to("d6:symbol6:symbole") }
|
54
54
|
it{ should bencode({1 => 1}).to("d1:1i1ee")}
|
55
55
|
it{ should bencode({1.1 => 1.1}).to("d3:1.1i1ee") }
|
56
|
-
it{ should bencode({{} => {}}).to("d2:{}dee") }
|
57
56
|
|
58
|
-
|
59
|
-
|
60
|
-
should bencode({uri => uri}).to("d32:http://github.com/blatyo/bencode32:http://github.com/blatyo/bencodee")
|
61
|
-
}
|
57
|
+
describe "ruby 1.9.x", :if => $ruby19 do
|
58
|
+
it{ should bencode({{} => {}}).to("d2:{}dee") }
|
62
59
|
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
60
|
+
it{
|
61
|
+
time = Time.utc(0)
|
62
|
+
should bencode({time => time}).to("d23:0000-01-01 00:00:00 UTCi-62167219200ee")
|
63
|
+
}
|
64
|
+
|
65
|
+
it{
|
66
|
+
array = (1..4).to_a
|
67
|
+
should bencode({array => array}).to("d12:[1, 2, 3, 4]li1ei2ei3ei4eee")
|
68
|
+
}
|
69
|
+
end
|
70
|
+
|
71
|
+
describe "ruby 1.8.x", :unless => $ruby19 do
|
72
|
+
it{ should bencode({{} => {}}).to("d0:dee") }
|
73
|
+
|
74
|
+
it{
|
75
|
+
time = Time.utc(0)
|
76
|
+
should bencode({time => time}).to("d28:Sat Jan 01 00:00:00 UTC 2000i946684800ee")
|
77
|
+
}
|
78
|
+
|
79
|
+
it{
|
80
|
+
array = (1..4).to_a
|
81
|
+
should bencode({array => array}).to("d4:1234li1ei2ei3ei4eee")
|
82
|
+
}
|
83
|
+
end
|
67
84
|
|
68
85
|
it{
|
69
|
-
|
70
|
-
should bencode({
|
86
|
+
uri = URI.parse("http://github.com/blatyo/bencode")
|
87
|
+
should bencode({uri => uri}).to("d32:http://github.com/blatyo/bencode32:http://github.com/blatyo/bencodee")
|
71
88
|
}
|
72
89
|
end
|
73
90
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,65 +1,74 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: bencodr
|
3
|
-
version: !ruby/object:Gem::Version
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 3.0.1
|
4
5
|
prerelease:
|
5
|
-
version: 3.0.0
|
6
6
|
platform: ruby
|
7
|
-
authors:
|
7
|
+
authors:
|
8
8
|
- Allen Madsen
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
12
|
+
date: 2012-04-03 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: &70267883845840 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
18
23
|
prerelease: false
|
19
|
-
|
24
|
+
version_requirements: *70267883845840
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rspec
|
27
|
+
requirement: &70267883844340 !ruby/object:Gem::Requirement
|
20
28
|
none: false
|
21
|
-
requirements:
|
29
|
+
requirements:
|
22
30
|
- - ~>
|
23
|
-
- !ruby/object:Gem::Version
|
24
|
-
version: 2.
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 2.8.0
|
25
33
|
type: :development
|
26
|
-
version_requirements: *id001
|
27
|
-
- !ruby/object:Gem::Dependency
|
28
|
-
name: bundler
|
29
34
|
prerelease: false
|
30
|
-
|
35
|
+
version_requirements: *70267883844340
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: bundler
|
38
|
+
requirement: &70267883842840 !ruby/object:Gem::Requirement
|
31
39
|
none: false
|
32
|
-
requirements:
|
33
|
-
- -
|
34
|
-
- !ruby/object:Gem::Version
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
35
43
|
version: 1.0.0
|
36
44
|
type: :development
|
37
|
-
version_requirements: *id002
|
38
|
-
- !ruby/object:Gem::Dependency
|
39
|
-
name: fuubar
|
40
45
|
prerelease: false
|
41
|
-
|
46
|
+
version_requirements: *70267883842840
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: fuubar
|
49
|
+
requirement: &70267883841280 !ruby/object:Gem::Requirement
|
42
50
|
none: false
|
43
|
-
requirements:
|
44
|
-
- -
|
45
|
-
- !ruby/object:Gem::Version
|
46
|
-
version: 0.0
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 1.0.0
|
47
55
|
type: :development
|
48
|
-
|
49
|
-
|
50
|
-
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *70267883841280
|
58
|
+
description: This gem provides a way to encode and decode bencode used by the Bit
|
59
|
+
Torrent protocol. Normal ruby objects can be marshalled as bencode and demarshalled
|
60
|
+
back to ruby.
|
61
|
+
email:
|
51
62
|
- blatyo@gmail.com
|
52
63
|
executables: []
|
53
|
-
|
54
64
|
extensions: []
|
55
|
-
|
56
65
|
extra_rdoc_files: []
|
57
|
-
|
58
|
-
files:
|
66
|
+
files:
|
59
67
|
- .autotest
|
60
68
|
- .document
|
61
69
|
- .gitignore
|
62
70
|
- .rvmrc
|
71
|
+
- .travis.yml
|
63
72
|
- Gemfile
|
64
73
|
- LICENSE
|
65
74
|
- README.md
|
@@ -91,35 +100,32 @@ files:
|
|
91
100
|
- spec/samples/python.torrent
|
92
101
|
- spec/shared_examples.rb
|
93
102
|
- spec/spec_helper.rb
|
94
|
-
has_rdoc: true
|
95
103
|
homepage: http://github.com/blatyo/bencodr
|
96
104
|
licenses: []
|
97
|
-
|
98
105
|
post_install_message:
|
99
106
|
rdoc_options: []
|
100
|
-
|
101
|
-
require_paths:
|
107
|
+
require_paths:
|
102
108
|
- lib
|
103
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
109
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
104
110
|
none: false
|
105
|
-
requirements:
|
106
|
-
- -
|
107
|
-
- !ruby/object:Gem::Version
|
108
|
-
version:
|
109
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
111
|
+
requirements:
|
112
|
+
- - ! '>='
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
115
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
110
116
|
none: false
|
111
|
-
requirements:
|
112
|
-
- -
|
113
|
-
- !ruby/object:Gem::Version
|
117
|
+
requirements:
|
118
|
+
- - ! '>='
|
119
|
+
- !ruby/object:Gem::Version
|
114
120
|
version: 1.3.6
|
115
121
|
requirements: []
|
116
|
-
|
117
122
|
rubyforge_project:
|
118
|
-
rubygems_version: 1.
|
123
|
+
rubygems_version: 1.8.17
|
119
124
|
signing_key:
|
120
125
|
specification_version: 3
|
121
|
-
summary: This gem provides a way to encode and decode bencode used by the Bit Torrent
|
122
|
-
|
126
|
+
summary: This gem provides a way to encode and decode bencode used by the Bit Torrent
|
127
|
+
protocol.
|
128
|
+
test_files:
|
123
129
|
- spec/bencode_spec.rb
|
124
130
|
- spec/bencodr/dictionary_spec.rb
|
125
131
|
- spec/bencodr/ext_spec.rb
|