Sha256: 532ca8c485009b2e755c0f132efe481a7c6a70fec68fc495e2b5ed68ae70f0fd

Contents?: true

Size: 1.54 KB

Versions: 1

Compression:

Stored size: 1.54 KB

Contents

# encoding: UTF-8

path = File.expand_path(File.dirname(__FILE__)) + "/bencode"

require path + "/string"
require path + "/integer"
require path + "/list"
require path + "/dictionary"
require path + "/parser"

module BEncode
  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)
      scanner = StringScanner.new(string)
      Parser.parse_object(scanner) or raise BEncodeError, "Invalid bencoding"
    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(file)
      decode(File.open(file, 'rb') {|f| f.read})
    end

    # This method encodes a bencoded object.
    #
    #   BEncode.encode("string") #=> "6:string"
    #
    # @param [#bencode] object the object to encode
    # @return [String] the bencoded object
    def encode(object)
      object.bencode
    end

    # This method encodes a bencoded object.
    #
    #   BEncode.encode("string") #=> "6:string"
    #
    # @param [String] file the file to write the bencoded object to
    # @param [#bencode] object the object to encode
    def encode_file(file, object)
      File.open(file, 'wb') {|f| f.write encode(object)}
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bencode_blatyo-1.0.0 lib/bencode.rb