Sha256: fefb70260d05001bfde95e972c9dfa0ad4f0db54d3d57f9e31d9d341b7b2d70e

Contents?: true

Size: 1.89 KB

Versions: 3

Compression:

Stored size: 1.89 KB

Contents

# encoding: UTF-8

module BEncodr
  module Dictionary
    module Generic
      module InstanceMethods
        # Encodes an array into a bencoded dictionary. Bencoded dictionaries are encoded as a 'd' followed by a list of
        # alternating keys and their corresponding values followed by an 'e'. Keys appear in sorted order (sorted as raw
        # strings, not alphanumerics).
        #
        #   {:cow => "moo", :seven => 7}.bencodr #=> "d3:cow3:moo5:seveni7ee"
        #
        # @return [::String] the bencoded dictionary
        def bencode
          (respond_to?(:to_h) ? to_h : to_hash).bencode
        end
      end
    end

    # Registers a class as an object that can be converted into a bencoded dictionary. Class must have instance method
    # to_h or to_hash.
    #
    #   class MyClass
    #     def to_h
    #       {:a => :a, :b => 1}
    #     end
    #   end
    #
    #   BEncodr::String.register MyClass
    #   my_class = MyClass.new
    #   my_class.bencodr  #=> "d1:a1:a1:bi1ee"
    #
    # @param [Class#to_h, Class#to_hash] type the class to add the bencodr instance method to
    def self.register(type)
      type.send :include, Generic::InstanceMethods
    end

    module Hash
      module InstanceMethods
        # Encodes an array into a bencoded dictionary. Bencoded dictionaries are encoded as a 'd' followed by a list of
        # alternating keys and their corresponding values followed by an 'e'. Keys appear in sorted order (sorted as raw
        # strings, not alphanumerics).
        #
        #   {:cow => "moo", :seven => 7}.bencodr #=> "d3:cow3:moo5:seveni7ee"
        #
        # @return [::String] the bencoded dictionary 
        def bencode
          keys.sort{|a, b| a.to_s <=> b.to_s}.collect do |key|
            key.to_s.bencode + self[key].bencode
          end.unshift(:d).push(:e).join
        end
      end

      ::Hash.send :include, InstanceMethods
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
bencodr-1.2.0 lib/bencodr/dictionary.rb
bencodr-1.1.0 lib/bencodr/dictionary.rb
bencodr-1.0.1 lib/bencodr/dictionary.rb