A modern, minimal Ruby implementation of the bencode encoding format with a small command-line interface.
gem build bencode.gemspec
# gem install ./bencode-0.1.0.gemrequire "bencode"
payload = Bencode.encode({ "hello" => [1, "world"] })
# => "d5:hellol1e5:worldee"
object = Bencode.decode(payload)
# => {"hello"=>[1, "world"]}Encoding supports strings, symbols, integers, arrays, and hashes (with string or symbol keys). Dictionary keys are sorted to produce deterministic output. Decoding returns plain Ruby strings, integers, arrays, and hashes.
The bencode executable can decode bencoded input to pretty-printed JSON or encode JSON into bencode. Input defaults to STDIN and output defaults to STDOUT.
# Decode a torrent file to JSON
bencode --decode --file ./example.torrent
# Encode JSON to bencode, writing the output to a file
cat payload.json | bencode --encode --output payload.bencodeOptions:
-e,--encode– encode JSON input into bencode-d,--decode– decode bencode input into JSON (default)-f,--file FILE– read input from a file instead of STDIN-o,--output FILE– write output to a file instead of STDOUT-h,--help– show usage information
Run the test suite with:
bundle install
bundle exec rspecOr using the default rake task:
bundle exec rake