<commit_before>class Brain

  require "net/http"

  def self.request(path:, method: :get, payload: nil, query: nil, access_token: nil)
    uri = URI("#{ENV["BRAIN_URL"]}#{path}")
    uri.query = URI.encode_www_form(query) if query
    http = Net::HTTP.new(uri.host, uri.port)
    req = case method
          when :get
            Net::HTTP::Get.new(uri.path)
          when :post
            Net::HTTP::Post.new(uri.path, "Content-Type" => "application/json")
          else
            raise "unknown method"
          end
    if access_token
      req["Authorization"] = "Bearer #{access_token}"
    end
    if !%i(get head).include?(method) && payload
      req.body = payload.to_json
    end
    response = http.request req
    json_response = JSON.parse(response.body, symbolize_names: true)
    if block_given?
      yield response, json_response
    else
      return response, json_response
    end
  end

  def self.ok?(path:, method: :get, payload: nil, query: nil, access_token: nil)
    response, json_response = request path: path, method: method, payload: payload, query: query,
                                      access_token: access_token
    response.code.to_i.between? 200, 299
  end

end<commit_msg>Style<commit_after>class Brain

  require "net/http"

  def self.request(path:, method: :get, payload: nil, query: nil, access_token: nil)
    uri = URI("#{ENV["BRAIN_URL"]}#{path}")
    uri.query = URI.encode_www_form(query) if query
    http = Net::HTTP.new(uri.host, uri.port)
    req = case method
          when :get
            Net::HTTP::Get.new(uri.path)
          when :post
            Net::HTTP::Post.new(uri.path, "Content-Type" => "application/json")
          else
            raise "unknown method"
          end
    if access_token
      req["Authorization"] = "Bearer #{access_token}"
    end
    if !%i(get head).include?(method) && payload
      req.body = payload.to_json
    end
    response = http.request req
    json_response = JSON.parse response.body, symbolize_names: true
    if block_given?
      yield response, json_response
    else
      return response, json_response
    end
  end

  def self.ok?(path:, method: :get, payload: nil, query: nil, access_token: nil)
    response, json_response = request path: path, method: method, payload: payload, query: query,
                                      access_token: access_token
    response.code.to_i.between? 200, 299
  end

end
{'fraction_non_alphanumeric': 0.11672831894780107, 'fraction_numerical': 0.004932182490752158, 'mean_word_length': 2.5377906976744184, 'pattern_counts': {'":': 0, '<': 3, '<?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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1816379', 'n_tokens_mistral': 765, 'n_tokens_neox': 748, 'n_words': 196}