Sha256: f88035f28e9bf93a4467345a443b5063fe1103bef0e15ca324e4c6c0bdd7c5cf

Contents?: true

Size: 1016 Bytes

Versions: 3

Compression:

Stored size: 1016 Bytes

Contents

# encoding: UTF-8

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)
      scanner = StringScanner.new(string)
      object = Parser.parse_object(scanner)
      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

3 entries across 3 versions & 1 rubygems

Version Path
bencodr-3.0.2 lib/bencodr/object.rb
bencodr-3.0.1 lib/bencodr/object.rb
bencodr-3.0.0 lib/bencodr/object.rb