bencodr 2.0.1 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -8,7 +8,7 @@
8
8
  ## Synopsis
9
9
  This gem provides a way to encode and parse bencodings used by the Bit Torrent protocol.
10
10
 
11
- _Note: If using ruby 1.9.x, use a bencodr version >= 3.0.0 as it takes advantage of 1.9.x's encoding features._
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
12
 
13
13
  ## Installation
14
14
 
@@ -67,6 +67,7 @@ This will extend:
67
67
  * Hash
68
68
  * BEncodr::IO
69
69
  * IO
70
+ * File
70
71
 
71
72
  ### String
72
73
  BEncoded strings are length-prefixed base ten followed by a colon and the string.
data/Rakefile CHANGED
@@ -1,3 +1,5 @@
1
+ # encoding: UTF-8
2
+
1
3
  require 'bundler'
2
4
  Bundler::GemHelper.install_tasks
3
5
 
data/lib/bencodr/ext.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # encoding: UTF-8
2
+
1
3
  module BEncodr
2
4
  module Ext
3
5
  def self.include!
data/lib/bencodr/io.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # encoding: UTF-8
2
+
1
3
  module BEncodr
2
4
  module IO
3
5
  module ClassMethods
@@ -15,7 +17,7 @@ module BEncodr
15
17
  end
16
18
 
17
19
  def bdecode
18
- Object.bdecode(read)
20
+ Object.bdecode(read.force_encoding('UTF-8'))
19
21
  end
20
22
 
21
23
  def self.included(base)
@@ -1,3 +1,5 @@
1
+ # encoding: UTF-8
2
+
1
3
  module BEncodr
2
4
  module Object
3
5
  def self.bencode(object)
@@ -25,7 +27,8 @@ module BEncodr
25
27
  end
26
28
 
27
29
  def self.bdecode(string)
28
- object = Parser.parse_object(StringScanner.new(string))
30
+ scanner = StringScanner.new(string)
31
+ object = Parser.parse_object(scanner)
29
32
  object or raise BEncodeError, "BEncodr::Object.bdecode was unable to parse the string passed in."
30
33
  end
31
34
 
@@ -35,9 +35,10 @@ module BEncodr
35
35
  # @param [StringScanner] scanner the scanner of a bencoded string
36
36
  # @return [::String] the parsed string
37
37
  def parse_string(scanner)
38
- length = scanner.scan(/[1-9][0-9]*|0/) or raise BEncodeError, "Invalid string: length invalid. #{scanner.pos}"
39
- scanner.scan(/:/) or raise BEncodeError, "Invalid string: missing colon(:). #{scanner.pos}"
40
- scanner.scan(/.{#{length}}/m) or raise BEncodeError, "Invalid string: length too long(#{length}) #{scanner.pos}."
38
+ length = scanner.scan(/[1-9][0-9]*|0/) or raise BEncodeError, "Invalid string: length invalid. #{scanner.pos}"
39
+ scanner.scan(/:/) or raise BEncodeError, "Invalid string: missing colon(:). #{scanner.pos}"
40
+ byte_string = scanner.scan(/.{#{length}}/m) or raise BEncodeError, "Invalid string: length too long(#{length}) #{scanner.pos}."
41
+ byte_string.encode('UTF-8') rescue byte_string.force_encoding('UTF-8')
41
42
  end
42
43
 
43
44
  # This method parases a bencoded integer.
@@ -1,3 +1,3 @@
1
1
  module BEncodr
2
- VERSION = "2.0.1"
2
+ VERSION = "3.0.0"
3
3
  end
data/spec/bencode_spec.rb CHANGED
@@ -27,7 +27,7 @@ describe BEncodr do
27
27
  describe "#bdecode_file" do
28
28
  it "should parse a bencoded file" do
29
29
  dirname = File.dirname(__FILE__)
30
- BEncodr.bdecode_file("#{dirname}/samples/mini.bencode").should == {"ba" => 3}
30
+ BEncodr.bdecode_file("#{dirname}/samples/mini.bencode").should == {"£" => 3}
31
31
  end
32
32
  end
33
33
 
@@ -75,7 +75,7 @@ describe BEncodr do
75
75
  context "when parsing and then encoding" do
76
76
  it "should be equal to the pre-parsed and encoded bencoded string" do
77
77
  file = File.dirname(__FILE__) + "/samples/bencode.rb.torrent"
78
- BEncodr.bencode(BEncodr.bdecode_file(file)).should == File.open(file, "rb") {|f| f.read}
78
+ BEncodr.bencode(BEncodr.bdecode_file(file)).should == File.open(file, "r:UTF-8") {|f| f.read}
79
79
  end
80
80
  end
81
81
  end
@@ -1,3 +1,4 @@
1
+ # encoding: UTF-8
1
2
  require "spec_helper"
2
3
 
3
4
  describe File do
@@ -0,0 +1 @@
1
+ d8:announce38:udp://tracker.publicbt.com:80/announce7:comment11:Awesome! £10:created by13:uTorrent/102013:creation datei1308787427e12:encoded ratei-1e8:encoding5:UTF-84:infod6:lengthi785e4:name10:bencodr.rb12:piece lengthi16384e6:pieces20:�b� �F�������O�ګ�ee
@@ -1 +1 @@
1
- d2:bai3ee
1
+ d2:£i3ee
@@ -1,8 +1,11 @@
1
+ # encoding: UTF-8
2
+
1
3
  shared_examples_for "BEncodr::String" do |obj|
2
4
  subject{ obj }
3
5
 
4
6
  describe "#bencode" do
5
7
  it{ should bencode("string").to("6:string") }
8
+ it{ should bencode("£").to("2:£") }
6
9
  it{ should bencode("").to("0:") }
7
10
  it{ should bencode(:symbol).to("6:symbol") }
8
11
  it{ should bencode(URI.parse("http://github.com/blatyo/bencodr")).to("32:http://github.com/blatyo/bencodr") }
data/spec/spec_helper.rb CHANGED
@@ -1,3 +1,4 @@
1
+ # encoding: UTF-8
1
2
  require File.join(File.dirname(__FILE__), '..', 'lib', 'bencodr')
2
3
  require 'rspec'
3
4
  require 'fuubar'
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: bencodr
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 2.0.1
5
+ version: 3.0.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Allen Madsen
@@ -86,6 +86,7 @@ files:
86
86
  - spec/bencodr/string_spec.rb
87
87
  - spec/custom_matchers.rb
88
88
  - spec/samples/bencode.rb.torrent
89
+ - spec/samples/bencodr.torrent
89
90
  - spec/samples/mini.bencode
90
91
  - spec/samples/python.torrent
91
92
  - spec/shared_examples.rb
@@ -130,6 +131,7 @@ test_files:
130
131
  - spec/bencodr/string_spec.rb
131
132
  - spec/custom_matchers.rb
132
133
  - spec/samples/bencode.rb.torrent
134
+ - spec/samples/bencodr.torrent
133
135
  - spec/samples/mini.bencode
134
136
  - spec/samples/python.torrent
135
137
  - spec/shared_examples.rb