<commit_before>require 'tire'
require 'cgi'
require 'uri'

module ElasticSearchThrift
  class TireClient
    def self.install
      Tire::Configuration.client(new)
    end

    def initialize
      @client = ElasticSearchThrift.client
      @client.open
    end

    attr_reader :client

    def get(url, data = {})
      execute :get, url, data
    end

    def post(url, data)
      execute :post, url, data
    end

    def put(url, data)
      execute :put, url, data
    end

    def delete(url)
      execute :delete, url, nil
    end

    def head(url)
      execute :head, url, nil
    end

    private

    def execute(method, url, data)
      path, params = parse_url(url)
      path.chomp!('/')
      response = client.send(method, path, params, normalize_data(data))
      Tire::HTTP::Response.new(response.body, response.status, response.headers)
    end

    def parse_url(url)
      uri = URI.parse(url)

      params = {}
      if uri.query
        uri.query.split('&').each do |pair|
          key, value = pair.split('=', 2)
          params[CGI.unescape(key)] = CGI.unescape(value)
        end
      end
<commit_msg>Lazily open the Tire connection.<commit_after>require 'tire'
require 'cgi'
require 'uri'

module ElasticSearchThrift
  class TireClient
    def self.install
      Tire::Configuration.client(new)
    end

    def initialize
      @client = ElasticSearchThrift.client
      @opened = false
    end

    attr_reader :client

    def get(url, data = {})
      execute :get, url, data
    end

    def post(url, data)
      execute :post, url, data
    end

    def put(url, data)
      execute :put, url, data
    end

    def delete(url)
      execute :delete, url, nil
    end

    def head(url)
      execute :head, url, nil
    end

    private

    def execute(method, url, data)
      @client.open if !@opened
      path, params = parse_url(url)
      path.chomp!('/')
      response = client.send(method, path, params, normalize_data(data))
      Tire::HTTP::Response.new(response.body, response.status, response.headers)
    end

    def parse_url(url)
      uri = URI.parse(url)

      params = {}
      if uri.query
        uri.query.split('&').each do |pair|
          key, value = pair.split('=', 2)
          params[CGI.unescape(key)] = CGI.unescape(value)
        end
      end

{'fraction_non_alphanumeric': 0.11063280241067586, 'fraction_numerical': 0.0008609556607834697, 'mean_word_length': 2.268635724331927, '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': '6600333', 'n_tokens_mistral': 766, 'n_tokens_neox': 741, 'n_words': 200}