Sha256: ba7cb2fbc8fb1915a96d13ab64183d2c780777332223873906133dfdb2d822fc

Contents?: true

Size: 1.46 KB

Versions: 1

Compression:

Stored size: 1.46 KB

Contents

# encoding: UTF-8

require 'uri'

module BEncode
  module String
    module Generic
      module InstanceMethods
        # Encodes object into a bencoded string. BEncoded strings are length-prefixed base ten followed by a colon and
        # the string.
        #
        #   :symbol.bencode #=> "6:symbol"
        #
        # @return [String] the bencoded string
        def bencode
          (respond_to?(:to_s) ? to_s : to_str).bencode
        end
      end
    end

    # Registers a class as an object that can be converted into a bencoded string. Class must have instance method to_s
    # or to_str.
    #
    #   class MyClass
    #     def to_s
    #       "string"
    #     end
    #   end
    #
    #   BEncode::String.register MyClass
    #   my_class = MyClass.new
    #   my_class.bencode  #=> "6:string"
    #
    # @param [Class#to_s, Class#to_str] type the class to add the bencode instance method to
    def self.register(type)
      type.send :include, Generic::InstanceMethods
    end

    register Symbol
    register URI::Generic

    module String
      module InstanceMethods
        # Encodes a string into a bencoded string. BEncoded strings are length-prefixed base ten followed by a colon and
        # the string.
        #
        #   "string".bencode #=> "6:string"
        #
        # @return [String] the bencoded string
        def bencode
          [length, ':', self].join
        end
      end

      ::String.send :include, InstanceMethods
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bencode_blatyo-1.0.0 lib/bencode/string.rb