Sha256: f8ec510727843dbe84bf48f47714e337c3c5a8581ec5f2890f57ba529145e444

Contents?: true

Size: 973 Bytes

Versions: 2

Compression:

Stored size: 973 Bytes

Contents

module BEncodr
  module Object
    def self.bencode(object)
      return object.bencode if object.respond_to?(:bencode)
      
      case object
      when ::String, Symbol, URI::Generic
        return String.bencode(object)
      when Numeric, Time
        return Integer.bencode(object)
      when Array
        return List.bencode(object)
      when Hash
        return Dictionary.bencode(object)
      else
        [String, Integer, List, Dictionary].each do |type|
          begin
            return type.bencode(object)
          rescue
          end
        end
      end
      
      raise BEncodeError, "BEncodr::Object.bencode was unable to infer the type of the object passed in."
    end
    
    def self.bdecode(string)
      object = Parser.parse_object(StringScanner.new(string))
      object or raise BEncodeError, "BEncodr::Object.bdecode was unable to parse the string passed in."
    end
    
    def bdecode
      Object.bdecode(self)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
bencodr-2.0.1 lib/bencodr/object.rb
bencodr-2.0.0 lib/bencodr/object.rb