bencodr 1.0.1 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -12,7 +12,7 @@ This gem provides a way to encode and parse bencodings used by the Bit Torrent p
12
12
  == Installation
13
13
 
14
14
  # install the gem
15
- > gem install bencodr
15
+ > [sudo] gem install bencodr
16
16
  Successfully installed bencodr
17
17
  1 gem installed
18
18
  Installing ri documentation for bencodr...
@@ -80,12 +80,20 @@ followed by an 'e'. Keys appear in sorted order (sorted as raw strings, not alph
80
80
  # Note: keys are sorted as raw strings.
81
81
  {:a => 1, "A" => 1, 1=> 1}.bencode #=> "d1:1i1e1:Ai1e1:ai1ee"
82
82
 
83
- === Encoding and Decoding
83
+ === Decoding
84
+ You can decode a bencoding by calling bdecode on the string.
85
+
86
+ "6:string".bdecode #=> "string"
87
+ "i1e".bdecode #=> 1
88
+ "le".bdecode #=> []
89
+ "de".bdecode #=> {}
90
+
91
+ === Encoding and Decoding with BEncodr
84
92
 
85
93
  # encoding is just like calling bencode on the object
86
94
  BEncodr.encode("string") #=> "6:string"
87
95
 
88
- # decoding takes a string and return either a String, Integer, Array, or Hash
96
+ # decoding is just like calling bdecode on a bencoding
89
97
  BEncodr.decode("6:string") #=> "string"
90
98
  BEncodr.decode("i1e") #=> 1
91
99
  BEncodr.decode("le") #=> []
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.1
1
+ 1.1.0
@@ -4,13 +4,13 @@
4
4
  # -*- encoding: utf-8 -*-
5
5
 
6
6
  Gem::Specification.new do |s|
7
- s.name = %q{bencode_blatyo}
8
- s.version = "1.0.1"
7
+ s.name = %q{bencodr}
8
+ s.version = "1.1.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Allen Madsen"]
12
- s.date = %q{2010-02-02}
13
- s.description = %q{This gem has been renamed to bencodr. Use that one instead.}
12
+ s.date = %q{2010-02-04}
13
+ s.description = %q{This gem provides a way to encode and parse bencodings used by the Bit Torrent protocol.}
14
14
  s.email = %q{blatyo@gmail.com}
15
15
  s.extra_rdoc_files = [
16
16
  "LICENSE",
@@ -25,21 +25,21 @@ Gem::Specification.new do |s|
25
25
  "Rakefile",
26
26
  "VERSION",
27
27
  "autotest/discover.rb",
28
- "bencode_blatyo.gemspec",
28
+ "bencodr.gemspec",
29
29
  "lib/bencodr.rb",
30
30
  "lib/bencodr/dictionary.rb",
31
31
  "lib/bencodr/integer.rb",
32
32
  "lib/bencodr/list.rb",
33
33
  "lib/bencodr/parser.rb",
34
34
  "lib/bencodr/string.rb",
35
+ "spec/bencode_spec.rb",
35
36
  "spec/bencodr/dictionary_spec.rb",
36
37
  "spec/bencodr/integer_spec.rb",
37
38
  "spec/bencodr/list_spec.rb",
38
39
  "spec/bencodr/parser_spec.rb",
39
40
  "spec/bencodr/string_spec.rb",
40
- "spec/bencode_spec.rb",
41
- "spec/samples/bencodr.rb.torrent",
42
- "spec/samples/mini.bencodr",
41
+ "spec/samples/bencode.rb.torrent",
42
+ "spec/samples/mini.bencode",
43
43
  "spec/samples/python.torrent",
44
44
  "spec/spec.opts",
45
45
  "spec/spec_helper.rb"
@@ -48,14 +48,14 @@ Gem::Specification.new do |s|
48
48
  s.rdoc_options = ["--charset=UTF-8"]
49
49
  s.require_paths = ["lib"]
50
50
  s.rubygems_version = %q{1.3.5}
51
- s.summary = %q{This gem has been renamed to bencodr. Use that one instead.}
51
+ s.summary = %q{This gem provides a way to encode and parse bencodings used by the Bit Torrent protocol.}
52
52
  s.test_files = [
53
- "spec/bencodr/dictionary_spec.rb",
53
+ "spec/bencode_spec.rb",
54
+ "spec/bencodr/dictionary_spec.rb",
54
55
  "spec/bencodr/integer_spec.rb",
55
56
  "spec/bencodr/list_spec.rb",
56
57
  "spec/bencodr/parser_spec.rb",
57
58
  "spec/bencodr/string_spec.rb",
58
- "spec/bencode_spec.rb",
59
59
  "spec/spec_helper.rb"
60
60
  ]
61
61
 
@@ -110,4 +110,18 @@ module BEncodr
110
110
  private :parse_key_value
111
111
  end
112
112
  end
113
+ end
114
+
115
+ class String
116
+ # This method decodes a bencoded string into a ruby object.
117
+ #
118
+ # "6:string".bdecode #=> "string"
119
+ # "i-1e".bdecode #=> -1
120
+ # "le".bdecode #=> []
121
+ # "de".bdecode #=> {}
122
+ #
123
+ # @return [::String, ::Integer, ::Array, ::Hash] the decoded object
124
+ def bdecode
125
+ BEncodr::Parser.parse_object(StringScanner.new self) or raise BEncodr::BEncodeError, "Invalid bencoding"
126
+ end
113
127
  end
data/lib/bencodr.rb CHANGED
@@ -19,8 +19,7 @@ module BEncodr
19
19
  # @param [::String] string the bencoded string to decode
20
20
  # @return [::String, ::Integer, ::Hash, ::Array] the decoded object
21
21
  def decode(string)
22
- scanner = StringScanner.new(string)
23
- Parser.parse_object(scanner) or raise BEncodeError, "Invalid bencoding"
22
+ string.bdecode
24
23
  end
25
24
 
26
25
  # This method decodes a bencoded file.
@@ -29,8 +28,8 @@ module BEncodr
29
28
  #
30
29
  # @param [::String] file the file to decode
31
30
  # @return [::String, ::Integer, ::Hash, ::Array] the decoded object
32
- def decode_file(file)
33
- decode(File.open(file, 'rb') {|f| f.read})
31
+ def decode_file(name)
32
+ decode(File.open(name, 'rb') {|file| file.read})
34
33
  end
35
34
 
36
35
  # This method encodes a bencoded object.
@@ -49,8 +48,8 @@ module BEncodr
49
48
  #
50
49
  # @param [::String] file the file to write the bencoded object to
51
50
  # @param [#bencodr] object the object to encode
52
- def encode_file(file, object)
53
- File.open(file, 'wb') {|f| f.write encode(object)}
51
+ def encode_file(name, object)
52
+ File.open(name, 'wb') {|file| file.write encode(object)}
54
53
  end
55
54
  end
56
55
  end
@@ -173,4 +173,24 @@ describe BEncodr::Parser do
173
173
  lambda{BEncodr::Parser.parse_dictionary(scanner)}.should raise_error BEncodr::BEncodeError
174
174
  end
175
175
  end
176
+ end
177
+
178
+ describe String do
179
+ describe "#bdecode" do
180
+ it "should decode a bencoded string into a ruby string" do
181
+ "6:string".bdecode.should == "string"
182
+ end
183
+
184
+ it "should decode a bencoded integer into a ruby integer" do
185
+ "i-1e".bdecode.should == -1
186
+ end
187
+
188
+ it "should decode a bencoded list into a ruby array" do
189
+ "le".bdecode.should == []
190
+ end
191
+
192
+ it "should decode a bencoded dictionary into a ruby hash" do
193
+ "de".bdecode.should == {}
194
+ end
195
+ end
176
196
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bencodr
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Allen Madsen
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-02-02 00:00:00 -05:00
12
+ date: 2010-02-04 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -50,7 +50,7 @@ files:
50
50
  - Rakefile
51
51
  - VERSION
52
52
  - autotest/discover.rb
53
- - bencode_blatyo.gemspec
53
+ - bencodr.gemspec
54
54
  - lib/bencodr.rb
55
55
  - lib/bencodr/dictionary.rb
56
56
  - lib/bencodr/integer.rb