<commit_before>require 'apsis/version'
require 'faraday'
require 'json'

class Apsis
  ApsisError = Class.new(StandardError)

  def initialize(api_key = nil, default_parameters = {})
    @api_key = api_key || ENV['APSIS_API_KEY']

    @conn = Faraday.new(
      url:      'https://se.api.anpdm.com/v1/',
      ssl:      { verify: false },
      headers:  { accept: 'application/json' })
    @conn.basic_auth(@api_key, '')
  end

  def call(endpoint, params)
    response = @conn.post do |req|
      req.url endpoint
      req.headers['Content-Type'] = 'application/json'
      req.body = params.to_json
    end
    unless response.status == 200
      raise ApsisError.new("Apsis API Error: #{response.body} (status code #{response.status})")
    end
    response.body
  end<commit_msg>It's nicer to return a hash instead of a JSON string.<commit_after>require 'apsis/version'
require 'faraday'
require 'json'

class Apsis
  ApsisError = Class.new(StandardError)

  def initialize(api_key = nil, default_parameters = {})
    @api_key = api_key || ENV['APSIS_API_KEY']

    @conn = Faraday.new(
      url:      'https://se.api.anpdm.com/v1/',
      ssl:      { verify: false },
      headers:  { accept: 'application/json' })
    @conn.basic_auth(@api_key, '')
  end

  def call(endpoint, params)
    response = @conn.post do |req|
      req.url endpoint
      req.headers['Content-Type'] = 'application/json'
      req.body = params.to_json
    end
    unless response.status == 200
      raise ApsisError.new("Apsis API Error: #{response.body} (status code #{response.status})")
    end
    JSON.parse response.body
  end
{'fraction_non_alphanumeric': 0.135101789019124, 'fraction_numerical': 0.004935225169648365, 'mean_word_length': 3.014851485148515, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3456481', 'n_tokens_mistral': 561, 'n_tokens_neox': 532, 'n_words': 124}