module BEncode
BEncode
module provides functionality for encoding/decoding Ruby objects in BitTorrent loosly structured data format - bencode. To decode data you sould use one of the following:
'string'.bdecode BEncode.decode('string')
Where string contains bencoded data (i.e. some torrent file) To encode your objects into bencode format:
object.bencode # or BEncode.encode(object)
bencode format has only following datatypes:
Integer String List Dictionary (Hash)
No other types allowed. Only symbols are converted to string for convenience (but they're decoded as strings).
BEncode is included into Object on load.
Public Class Methods
Returns data structure from parsed string. String
must be valid bencoded data, or BEncode::DecodeError
will be raised with description of error.
Examples:
BEncode.decode('i1e') => 1 BEncode.decode('i-1e') => -1 BEncode.decode('6:string') => 'string'
static VALUE decode(VALUE self, VALUE encoded){
Loads content of file and decodes it. file may be either IO instance or String
path to file.
Examples:
BEncode.decode_file('/path/to/file.torrent') open('/path/to/file.torrent', 'rb') do |f| BEncode.decode_file(f) end
static VALUE decode_file(VALUE self, VALUE path){
Shortcut to object.bencode
static VALUE mod_encode(VALUE self, VALUE x){
Get maximum depth of parsed structure.
static VALUE get_max_depth(VALUE self){
Sets maximum depth of parsed structure. Expects integer greater or equal to 0. By default this value is 5000. Assigning nil will disable depth check.
static VALUE set_max_depth(VALUE self, VALUE depth){
Public Instance Methods
Returns a string representing object in bencoded format. object must be one of:
Integer String Symbol (will be converter to String) Array Hash
If object does not belong to these types or their derivates BEncode::EncodeError
exception will be raised.
Because BEncode
is included into Object This method is avilable for all objects.
Examples:
1.bencode => 'i1e' -1.bencode => 'i-1e' 'string'.bencode => '6:string'
static VALUE encode(VALUE self){