<commit_before>module Hopo
  class Rater < API
    include Hopo::Utils::Calculator
    include Hopo::Utils::Sorter

    attr_accessor :line, :premium_mode, :sorter

    BASE_URL = 'https://integral.honestpolicy.com/api'
    API_VERSION = 1
    API_PATH = 'rates'

    def initialize(line, premium_mode='annually', sorter='premiums-asc')
      unless Hopo.api_key
        raise AuthenticationError, 'No API key provided.  Set your API key using "Hopo.api_key = <API-KEY>".'
      end

      super(BASE_URL, "/v#{API_VERSION}/#{API_PATH}", true)

      @premium_mode = premium_mode
      @sorter = sorter

      @default_params = {
        :key => Hopo.api_key,
        :line => line
      }

    end

    private

    def response_json(response)
      format_response( JSON.parse(response.body) )
    end

    def format_response(response)
      # TEMP Hack for v1 Until Integral API is cleaned Up (v2)
      if response['status']
        hash = { :errors => [response['message']] }

      else
        hash = {}
        rates = response['results']['rates']
        errors = response['results']['errors']
        risk = response['risk']

        calculated_rates = factor_rates(rates, premium_mode)
        sorted_rates = sort_rates(calculated_rates, sorter)

        hash.merge!( {:rates => sorted_rates} ) unless rates.blank?
        hash.merge!( {:errors => errors} ) unless errors.blank?
        hash.merge!( {:risk => risk} ) unless risk.blank?
      end

      hash
    end

  end
end<commit_msg>add another error check for Integral v1 API<commit_after>module Hopo
  class Rater < API
    include Hopo::Utils::Calculator
    include Hopo::Utils::Sorter

    attr_accessor :line, :premium_mode, :sorter

    BASE_URL = 'https://integral.honestpolicy.com/api'
    API_VERSION = 1
    API_PATH = 'rates'

    def initialize(line, premium_mode='annually', sorter='premiums-asc')
      unless Hopo.api_key
        raise AuthenticationError, 'No API key provided.  Set your API key using "Hopo.api_key = <API-KEY>".'
      end

      super(BASE_URL, "/v#{API_VERSION}/#{API_PATH}", true)

      @premium_mode = premium_mode
      @sorter = sorter

      @default_params = {
        :key => Hopo.api_key,
        :line => line
      }

    end

    private

    def response_json(response)
      format_response( JSON.parse(response.body) )
    end

    def format_response(response)
      # TEMP Hack for v1 Until Integral API is cleaned Up (v2)
      if response['status']
        hash = { :errors => [response['message']] }

      elsif response['errors']
        hash = {:errors => [response['errors']]}

      else
        hash = {}
        rates = response['results']['rates']
        errors = response['results']['errors']
        risk = response['risk']

        calculated_rates = factor_rates(rates, premium_mode)
        sorted_rates = sort_rates(calculated_rates, sorter)

        hash.merge!( {:rates => sorted_rates} ) unless rates.blank?
        hash.merge!( {:errors => errors} ) unless errors.blank?
        hash.merge!( {:risk => risk} ) unless risk.blank?
      end

      hash
    end

  end
end
{'fraction_non_alphanumeric': 0.12078380982974622, 'fraction_numerical': 0.0022486347574686796, 'mean_word_length': 2.6125290023201857, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 18, 'https://': 2, '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': '6913716', 'n_tokens_mistral': 1013, 'n_tokens_neox': 938, 'n_words': 231}