require 'bencode'

module Torckapi
  module Response
    class Base
      protected

      def self.bdecode_and_check(data, key)
        begin
          bdecoded_data = BEncode.load(data)
        rescue BEncode::DecodeError
          raise Torckapi::Tracker::MalformedResponseError, "Can't decode '%s'" % data
        end

        raise Torckapi::Tracker::MalformedResponseError, "bdecoded data: '%s'" % bdecoded_data + " didn't contain key: '%s'" % key unless bdecoded_data.is_a? Hash and bdecoded_data.has_key? key

        bdecoded_data
      end
    end
  end
end

require 'bencode'

module Torckapi
  module Response
    class Base
      protected

      def self.bdecode_and_check(data, key)
        begin
          bdecoded_data = BEncode.load(data, :ignore_trailing_junk => true)
        rescue BEncode::DecodeError
          raise Torckapi::Tracker::MalformedResponseError, "Can't decode '%s'" % data
        end

        raise Torckapi::Tracker::MalformedResponseError, "bdecoded data: '%s'" % bdecoded_data + " didn't contain key: '%s'" % key unless bdecoded_data.is_a? Hash and bdecoded_data.has_key? key

        bdecoded_data
      end
    end
  end
end

Revert "Revert "Allow proper loading of failed Bencoded data""
Revert "Revert "Allow proper loading of failed Bencoded data""

This reverts commit 5262935bf43adf18d10be95c926df5d643f8b5d5.
