<commit_before># encoding: utf-8
require 'typhoeus'
require 'open-uri'
require 'json'

module CartoDB
  class SQLApi
    class SQLError        < StandardError; end
    class PermissionError < StandardError; end

    # seconds
    CONNECT_TIMEOUT = 5
    TIMEOUT = 60

    attr_accessor :api_key, :username
    attr_reader   :response_code, :response_body, :parsed_response, :protocol, :timeout

    def initialize(arguments)
      self.username = arguments.fetch(:username)
      self.api_key  = arguments.fetch(:api_key, nil)
      self.protocol = arguments.fetch(:protocol, 'http')
      self.timeout  = arguments.fetch(:timeout, TIMEOUT)
    end

    def fetch(query, format = '')
      params   = { q: query, api_key: api_key, format: format }
      response = Typhoeus::Request.new(
        base_url,
        method: :post,
        body: URI.encode_www_form(params),
        headers: { 'Accept-Encoding' => 'gzip,deflate' },
        connecttimeout: CONNECT_TIMEOUT,
        timeout: timeout
      ).run
      handle_response(response)
    end

    def handle_response(response)
      @response_code    = response.response_code
      body              = inflate(response.body.to_s)
      @parsed_response  = ::JSON.parse(body) rescue nil
      raise_if_error
      parsed_response["rows"] rescue body
    end

    def inflate(text)
      Zlib::GzipReader.new(StringIO.new(text)).read
    rescue Zlib::GzipFile::Error
      return text
    end

    def raise_if_error
      error_message   = parsed_response["error"].first rescue nil
      raise PermissionError if error_message =~ /^permission denied for relation/
      raise SQLError.new(error_message) if response_code != 200
    end

    def base_url
      "#{protocol}://#{username}.cartodb.com/api/v2/sql"
    end

  end
end
<commit_msg>#2942 proper accessors and removed one no longer used<commit_after># encoding: utf-8
require 'typhoeus'
require 'open-uri'
require 'json'

module CartoDB
  class SQLApi
    class SQLError        < StandardError; end
    class PermissionError < StandardError; end

    # seconds
    CONNECT_TIMEOUT = 5
    TIMEOUT = 60

    attr_accessor :api_key, :username, :protocol, :timeout
    attr_reader   :response_code, :parsed_response

    def initialize(arguments)
      self.username = arguments.fetch(:username)
      self.api_key  = arguments.fetch(:api_key, nil)
      self.protocol = arguments.fetch(:protocol, 'http')
      self.timeout  = arguments.fetch(:timeout, TIMEOUT)
    end

    def fetch(query, format = '')
      params   = { q: query, api_key: api_key, format: format }
      response = Typhoeus::Request.new(
        base_url,
        method: :post,
        body: URI.encode_www_form(params),
        headers: { 'Accept-Encoding' => 'gzip,deflate' },
        connecttimeout: CONNECT_TIMEOUT,
        timeout: timeout
      ).run
      handle_response(response)
    end

    def handle_response(response)
      @response_code    = response.response_code
      body              = inflate(response.body.to_s)
      @parsed_response  = ::JSON.parse(body) rescue nil
      raise_if_error
      parsed_response["rows"] rescue body
    end

    def inflate(text)
      Zlib::GzipReader.new(StringIO.new(text)).read
    rescue Zlib::GzipFile::Error
      return text
    end

    def raise_if_error
      error_message   = parsed_response["error"].first rescue nil
      raise PermissionError if error_message =~ /^permission denied for relation/
      raise SQLError.new(error_message) if response_code != 200
    end

    def base_url
      "#{protocol}://#{username}.cartodb.com/api/v2/sql"
    end

  end
end

{'fraction_non_alphanumeric': 0.09812050856826976, 'fraction_numerical': 0.00552791597567717, 'mean_word_length': 2.8255813953488373, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5973375', 'n_tokens_mistral': 1135, 'n_tokens_neox': 1108, 'n_words': 257}