<commit_before>module Netsweet
  class Client
    attr_reader :connection, :response

    def initialize
      @connection = Connection.new
    end

    def get_record(ns_type, ns_id)
      call do
        connection.get_record(ns_type, ns_id)
      end
    end

    def search_records(ns_type, ns_field_name, ns_field_value, ns_operator='is', return_columns)
      call do
        query    = { "#{ns_field_name}" => { "value" => ns_field_value, "operator" => ns_operator } }
        response = connection.search_records(ns_type, query, return_columns)
        response.map do |properties|
          # elevate :column results to top level
          properties.merge(properties.delete(:columns))
        end
      end
    end


    private

    def call(&blk)
      @response = yield blk
      raise error unless valid?
      @response
    end

    def valid?
      response.is_a?(Hash) || response[0].is_a?(Hash) # yes, seriously.
    end

    def error
      raise error_klass(response[0]).new(response.join(' '))<commit_msg>Protect against all searched records being returned<commit_after>module Netsweet
  class Client
    attr_reader :connection, :response

    def initialize
      @connection = Connection.new
    end

    def get_record(ns_type, ns_id)
      call do
        connection.get_record(ns_type, ns_id)
      end
    end

    def search_records(ns_type, ns_field_name, ns_field_value, ns_operator='is', return_columns)
      raise ClientError.new("A search value must be provided") if ns_field_value.blank?

      call do
        query    = { "#{ns_field_name}" => { "value" => ns_field_value, "operator" => ns_operator } }
        response = connection.search_records(ns_type, query, return_columns)
        response.map do |properties|
          # elevate :column results to top level
          properties.merge(properties.delete(:columns))
        end
      end
    end


    private

    def call(&blk)
      @response = yield blk
      raise error unless valid?
      @response
    end

    def valid?
      response.is_a?(Hash) || response[0].is_a?(Hash) # yes, seriously.
    end

    def error
      raise error_klass(response[0]).new(response.join(' '))
{'fraction_non_alphanumeric': 0.09407985314364388, 'fraction_numerical': 0.0018357044515832951, 'mean_word_length': 2.6638655462184873, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2061293', 'n_tokens_mistral': 657, 'n_tokens_neox': 643, 'n_words': 165}