<commit_before>require 'blockscore/util'
require 'blockscore/errors/api_error'
require 'blockscore/errors/authentication_error'
require 'blockscore/errors/error'
require 'blockscore/errors/invalid_request_error'

module BlockScore
  module Responder
    def handle_response(response)
      case response.code
      when 200, 201
        json_obj = Util.parse_json(response.body)

        if json_obj.respond_to?(:key?) && json_obj.key?(:matches)
          Util.create_watchlist_hit_array json_obj[:matches]
        elsif json_obj.class == Array
          build_response_from_arr json_obj
        elsif json_obj[:object] == 'list'
          data = json_obj[:data]

          if data.first.class == Hash && data.first.key?(:matching_info)
            Util.create_watchlist_hit_array data
          else
            build_response_from_arr data
          end
        else
          Util.create_object resource, json_obj
        end
      else
        Util.handle_api_error response
      end
    end

    private

    def build_response_from_arr(arr_obj)
      arr_obj.map { |obj| Util.create_object(resource, obj) }
    end
  end
end<commit_msg>start cleaning up Responder<commit_after>require 'blockscore/util'
require 'blockscore/errors/api_error'
require 'blockscore/errors/authentication_error'
require 'blockscore/errors/error'
require 'blockscore/errors/invalid_request_error'

module BlockScore
  module Responder
    def handle_response(response)
      case response.code
      when 200, 201
        json_obj = Util.parse_json(response.body)

        dispatch_success(json_obj)
      else
        Util.handle_api_error response
      end
    end

    private

    def build_response_from_arr(arr_obj)
      arr_obj.map { |obj| Util.create_object(resource, obj) }
    end

    def dispatch_success(json)
      if watchlist_search?(json)
        Util.create_watchlist_hit_array json[:matches]
      elsif resource_array?(json)
        build_response_from_arr json
      elsif resource_index?(json)
        index_response(json)
      else # single object
        Util.create_object resource, json
      end
    end

    # candidates#search endpoint
    def watchlist_search?(data)
      data.respond_to?(:key?) && data.key?(:matches)
    end

    # hash style list format
    def resource_index?(data)
      data.class == Hash && data[:object] == 'list'
    end

    def index_response(json)
      data = json[:data]

      if data.first.class == Hash && data.first.key?(:matching_info)
        Util.create_watchlist_hit_array data
      else
        build_response_from_arr data
      end
    end

    # array formatted response
    def resource_array?(data)
      data.class == Array
    end
  end
end
{'fraction_non_alphanumeric': 0.07908351810790835, 'fraction_numerical': 0.004434589800443459, 'mean_word_length': 2.786013986013986, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3921442', 'n_tokens_mistral': 825, 'n_tokens_neox': 799, 'n_words': 171}