bencodr 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -88,16 +88,37 @@ You can decode a bencoding by calling bdecode on the string.
88
88
  "le".bdecode #=> []
89
89
  "de".bdecode #=> {}
90
90
 
91
- === Encoding and Decoding with BEncodr
91
+ === IO and Files
92
+ You can also write and read bencodings.
93
+
94
+ # write to standard out
95
+ IO.bencode(1, "string") #=> "6:string" to stdout
96
+ $stdout.bencode("string") #=> "6:string" to stdout
97
+
98
+ # write to file
99
+ File.bencode("a.bencode", "string") #=> "6:string" to a.bencode
100
+
101
+ file = File.open("a.bencode", "wb")
102
+ file.bencode("string") #=> "6:string" to a.bencode
103
+
104
+ # read from standard in
105
+ IO.bdecode(0) #=> "string"
106
+ $stdin.bdecode #=> "string"
107
+
108
+ # read from file
109
+ File.bdecode("a.bencode") #=> "string"
110
+
111
+ file = File.open("a.bencode", "wb")
112
+ file.bdecode #=> "string"
113
+
114
+ === BEncodr
115
+ Most of the functionality of this library can also be accessed directly on the BEncodr class.
92
116
 
93
117
  # encoding is just like calling bencode on the object
94
118
  BEncodr.encode("string") #=> "6:string"
95
119
 
96
120
  # decoding is just like calling bdecode on a bencoding
97
121
  BEncodr.decode("6:string") #=> "string"
98
- BEncodr.decode("i1e") #=> 1
99
- BEncodr.decode("le") #=> []
100
- BEncodr.decode("de") #=> {}
101
122
 
102
123
  # you can work directly with files too
103
124
  BEncodr.encode_file("my_awesome.torrent", {:announce => "http://www.sometracker.com/announce:80"})
@@ -152,4 +173,4 @@ of the last type registered for.
152
173
 
153
174
  == Copyright
154
175
 
155
- Copyright (c) 2010 blatyo. See LICENSE for details.
176
+ Copyright (c) 2010 Allen Madsen. See LICENSE for details.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.1.0
1
+ 1.2.0
data/bencodr.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{bencodr}
8
- s.version = "1.1.0"
8
+ s.version = "1.2.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"]
@@ -29,12 +29,14 @@ Gem::Specification.new do |s|
29
29
  "lib/bencodr.rb",
30
30
  "lib/bencodr/dictionary.rb",
31
31
  "lib/bencodr/integer.rb",
32
+ "lib/bencodr/io.rb",
32
33
  "lib/bencodr/list.rb",
33
34
  "lib/bencodr/parser.rb",
34
35
  "lib/bencodr/string.rb",
35
36
  "spec/bencode_spec.rb",
36
37
  "spec/bencodr/dictionary_spec.rb",
37
38
  "spec/bencodr/integer_spec.rb",
39
+ "spec/bencodr/io_spec.rb",
38
40
  "spec/bencodr/list_spec.rb",
39
41
  "spec/bencodr/parser_spec.rb",
40
42
  "spec/bencodr/string_spec.rb",
@@ -53,6 +55,7 @@ Gem::Specification.new do |s|
53
55
  "spec/bencode_spec.rb",
54
56
  "spec/bencodr/dictionary_spec.rb",
55
57
  "spec/bencodr/integer_spec.rb",
58
+ "spec/bencodr/io_spec.rb",
56
59
  "spec/bencodr/list_spec.rb",
57
60
  "spec/bencodr/parser_spec.rb",
58
61
  "spec/bencodr/string_spec.rb",
data/lib/bencodr/io.rb ADDED
@@ -0,0 +1,60 @@
1
+ class IO
2
+ class << self
3
+ # This method encodes the object and writes it to the specified output.
4
+ #
5
+ # # write to standard out
6
+ # IO.bencode(1, "string") #=> "6:string" to stdout
7
+ #
8
+ # # write to file
9
+ # File.bencode("a.bencode", "string") #=> "6:string" to a.bencode
10
+ #
11
+ # @param [Object] fd the file descriptor to use for output
12
+ # @param [Object] object the object to write
13
+ def bencode(fd, object)
14
+ open(fd, "wb") {|file| file.bencode object }
15
+ end
16
+
17
+ # This method reads from the specified input and decodes the object.
18
+ #
19
+ # # read from standard in
20
+ # IO.bdecode(0) #=> "string"
21
+ #
22
+ # # read from file
23
+ # File.bdecode("a.bencode") #=> "string"
24
+ #
25
+ # @param [Object] fd the file descriptor to use for input
26
+ # @param [Object] object the object to write
27
+ def bdecode(fd)
28
+ open(fd, "rb") {|file| file.bdecode}
29
+ end
30
+ end
31
+
32
+ # This method encodes the object and writes.
33
+ #
34
+ # # write to standard out
35
+ # $stdout.bencode("string") #=> "6:string" to stdout
36
+ #
37
+ # # write to file
38
+ # file = File.open("a.bencode", "wb")
39
+ # file.bencode("string") #=> "6:string" to a.bencode
40
+ #
41
+ # @param [Object] object the object to write
42
+ def bencode(object)
43
+ write object.bencode
44
+ end
45
+
46
+ # This method reads from the specified input and decodes the object.
47
+ #
48
+ # # read from standard in
49
+ # $stdin.bdecode #=> "string"
50
+ #
51
+ # # read from file
52
+ # file = File.open("a.bencode", "wb")
53
+ # file.bdecode #=> "string"
54
+ #
55
+ # @param [Object] fd the file descriptor to use for input
56
+ # @param [Object] object the object to write
57
+ def bdecode
58
+ read.bdecode
59
+ end
60
+ end
data/lib/bencodr.rb CHANGED
@@ -7,6 +7,7 @@ require path + "/integer"
7
7
  require path + "/list"
8
8
  require path + "/dictionary"
9
9
  require path + "/parser"
10
+ require path + "/io"
10
11
 
11
12
  module BEncodr
12
13
  class BEncodeError < StandardError; end
@@ -29,7 +30,7 @@ module BEncodr
29
30
  # @param [::String] file the file to decode
30
31
  # @return [::String, ::Integer, ::Hash, ::Array] the decoded object
31
32
  def decode_file(name)
32
- decode(File.open(name, 'rb') {|file| file.read})
33
+ File.bdecode name
33
34
  end
34
35
 
35
36
  # This method encodes a bencoded object.
@@ -49,7 +50,7 @@ module BEncodr
49
50
  # @param [::String] file the file to write the bencoded object to
50
51
  # @param [#bencodr] object the object to encode
51
52
  def encode_file(name, object)
52
- File.open(name, 'wb') {|file| file.write encode(object)}
53
+ File.bencode name, object
53
54
  end
54
55
  end
55
56
  end
@@ -0,0 +1,36 @@
1
+ require "spec"
2
+
3
+ describe File do
4
+ describe "#bencode" do
5
+ context "when an object gets bencoded and written to a file" do
6
+ before :all do
7
+ @path = "tmp"
8
+ Dir.mkdir(@path) unless File.exists? @path
9
+ end
10
+
11
+ before :each do
12
+ @file = File.join(@path, 'test.bencodr')
13
+ @object = "string"
14
+ File.bencode(@file, @object)
15
+ end
16
+
17
+ it "should actually write a file" do
18
+ File.exists?(@file).should be_true
19
+ end
20
+
21
+ describe "#bdecode" do
22
+ it "should properly encode the file" do
23
+ File.bdecode(@file).should == @object
24
+ end
25
+ end
26
+
27
+ after :each do
28
+ File.delete(@file)
29
+ end
30
+
31
+ after :all do
32
+ Dir.delete(@path) if File.exists? @path
33
+ end
34
+ end
35
+ end
36
+ 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.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Allen Madsen
@@ -54,12 +54,14 @@ files:
54
54
  - lib/bencodr.rb
55
55
  - lib/bencodr/dictionary.rb
56
56
  - lib/bencodr/integer.rb
57
+ - lib/bencodr/io.rb
57
58
  - lib/bencodr/list.rb
58
59
  - lib/bencodr/parser.rb
59
60
  - lib/bencodr/string.rb
60
61
  - spec/bencode_spec.rb
61
62
  - spec/bencodr/dictionary_spec.rb
62
63
  - spec/bencodr/integer_spec.rb
64
+ - spec/bencodr/io_spec.rb
63
65
  - spec/bencodr/list_spec.rb
64
66
  - spec/bencodr/parser_spec.rb
65
67
  - spec/bencodr/string_spec.rb
@@ -100,6 +102,7 @@ test_files:
100
102
  - spec/bencode_spec.rb
101
103
  - spec/bencodr/dictionary_spec.rb
102
104
  - spec/bencodr/integer_spec.rb
105
+ - spec/bencodr/io_spec.rb
103
106
  - spec/bencodr/list_spec.rb
104
107
  - spec/bencodr/parser_spec.rb
105
108
  - spec/bencodr/string_spec.rb