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

decode(string) click to toggle source

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){
decode_file(file) click to toggle source

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){
encode(object) click to toggle source

Shortcut to object.bencode

static VALUE mod_encode(VALUE self, VALUE x){
max_depth click to toggle source

Get maximum depth of parsed structure.

static VALUE get_max_depth(VALUE self){
max_depth = _integer_ click to toggle source

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

bencode click to toggle source

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){