<commit_before>module Apilayer
  module ConnectionHelper

    def reset_connection
      @connection = nil
    end

    def get_and_parse(url, params={})
      resp = get_request(url, params)
      parse_response(resp)
    end

    def get_request(url, params={})
      # calls connection method on the extended module
      connection.get do |req|
        req.url "api/#{url}"
        params.each_pair do |k,v|
          req.params[k] = v
        end
      end
    end

    def parse_response(resp)
      body = JSON.parse(resp.body)

      # Damn it apilayer! Why an exception for vat#validation response? valid instead of success like everywhere else!
      if body["success"] || body["valid"]
        return body
      else
        raise Apilayer::Error.new(
          body['error']['info'],
          body['error']['code']
        )
      end      
    end
  end
end
<commit_msg>Rewritten how connection_helper should return body or raise error<commit_after>module Apilayer
  module ConnectionHelper

    def reset_connection
      @connection = nil
    end

    def get_and_parse(url, params={})
      resp = get_request(url, params)
      parse_response(resp)
    end

    def get_request(url, params={})
      # calls connection method on the extended module
      connection.get do |req|
        req.url "api/#{url}"
        params.each_pair do |k,v|
          req.params[k] = v
        end
      end
    end

    def parse_response(resp)
      body = JSON.parse(resp.body)
      # According to documentation, currencylayer has a "success" field 
      # while vatlayer has a "valid" field to indicate whether the request was succesful or not.
      # However, for both layers, an unsuccesful request would contain an "error" field.
      # That's why the presence of "error" is chosen to determine whether we should raise an error or not.
      if body['error']
        raise Apilayer::Error.new(
          body['error']['info'],
          body['error']['code']
        )
      else
        body
      end      
    end
  end
end

{'fraction_non_alphanumeric': 0.08823529411764706, 'fraction_numerical': 0.0, 'mean_word_length': 2.24483306836248, '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': '5543427', 'n_tokens_mistral': 588, 'n_tokens_neox': 573, 'n_words': 191}