lib/bencodr.rb in bencodr-1.2.0 vs lib/bencodr.rb in bencodr-2.0.0
- old
+ new
@@ -1,56 +1,39 @@
# encoding: UTF-8
-path = File.expand_path(File.dirname(__FILE__)) + "/bencodr"
+path = File.expand_path(File.dirname(__FILE__)) + "/bencodr/"
-require path + "/string"
-require path + "/integer"
-require path + "/list"
-require path + "/dictionary"
-require path + "/parser"
-require path + "/io"
+require path + "string"
+require path + "integer"
+require path + "list"
+require path + "dictionary"
+require path + "object"
+require path + "parser"
+require path + "io"
+require path + "ext"
+require path + "version"
module BEncodr
class BEncodeError < StandardError; end
class << self
- # This method decodes a bencoded string.
- #
- # BEncode.decode("6:string") #=> "string"
- #
- # @param [::String] string the bencoded string to decode
- # @return [::String, ::Integer, ::Hash, ::Array] the decoded object
- def decode(string)
- string.bdecode
+ def bdecode(object)
+ BEncodr::Object.bdecode(object)
end
- # This method decodes a bencoded file.
- #
- # BEncode.decode_file("simple.torrent") #=> "d8:announce32:http://www..."
- #
- # @param [::String] file the file to decode
- # @return [::String, ::Integer, ::Hash, ::Array] the decoded object
- def decode_file(name)
- File.bdecode name
+ def bdecode_file(fd)
+ ::File.open(fd, "rb") {|file| bdecode(file.read)}
end
- # This method encodes a bencoded object.
- #
- # BEncode.encode("string") #=> "6:string"
- #
- # @param [#bencodr] object the object to encode
- # @return [::String] the bencoded object
- def encode(object)
- object.bencode
+ def bencode(object)
+ BEncodr::Object.bencode(object)
end
- # This method encodes a bencoded object.
- #
- # BEncode.encode("string") #=> "6:string"
- #
- # @param [::String] file the file to write the bencoded object to
- # @param [#bencodr] object the object to encode
- def encode_file(name, object)
- File.bencode name, object
+ def bencode_file(fd, object)
+ ::File.open(fd, "wb") {|file| file.write(bencode(object))}
end
+
+ def include!
+ Ext.include!
+ end
end
-end
\ No newline at end of file
+end