<commit_before>require 'faraday'
require 'json'

# API wrapper class
class Zenvelope
  JSON_RPC_VERSION = '2.0'
  RANDOM_ID_SEED = 100_000
  VERSION = '0.1.0.pre'

  # Subclass that provides the "action", i.e. the "get" in "host.get"
  class ZenvelopeAction
    def initialize(verb, parent)
      @verb = verb
      @parent = parent
    end

    def method_missing(method, *args)
      @parent.query([@verb, '.', method].join, args.first)
    end
  end

  def initialize(url = 'http://localhost')
    @auth = nil
    @url = url.chomp('/') + '/api_jsonrpc.php'
  end

  def login(creds = { user: 'Admin', password: '<PASSWORD>' })
    @auth = query('user.login', creds)
  end

  def method_missing(method)
    instance_variable_set('@' + method.to_s, ZenvelopeAction.new(method, self))
    instance_variable_get('@' + method.to_s)
  end

  def query(method, parameters)
    payload = { id: rand(RANDOM_ID_SEED), jsonrpc: JSON_RPC_VERSION,
                method: method, params: parameters
    }

    payload[:auth] = @auth unless method.include? 'apiinfo'

    begin
      response = JSON.parse(request(payload), symbolize_names: true)
    rescue<commit_msg>auth attr returns authid or false<commit_after>require 'faraday'
require 'json'

# API wrapper class
class Zenvelope
  JSON_RPC_VERSION = '2.0'
  RANDOM_ID_SEED = 100_000
  VERSION = '0.2.0'

  attr_reader :auth

  # Subclass that provides the "action", i.e. the "get" in "host.get"
  class ZenvelopeAction
    def initialize(verb, parent)
      @verb = verb
      @parent = parent
    end

    def method_missing(method, *args)
      @parent.query([@verb, '.', method].join, args.first)
    end
  end

  def initialize(url = 'http://localhost')
    @auth = nil
    @url = url.chomp('/') + '/api_jsonrpc.php'
  end

  def login(creds = { user: 'Admin', password: '<PASSWORD>' })
    results = query('user.login', creds)
    # return the auth id if successful login, otherwise return false
    @auth = if results.is_a?(String) && /[a-zA-Z0-9]{32}/.match(results)
              results
            else
              false
            end
  end

  def method_missing(method)
    instance_variable_set('@' + method.to_s, ZenvelopeAction.new(method, self))
    instance_variable_get('@' + method.to_s)
  end

  def query(method, parameters)
    payload = { id: rand(RANDOM_ID_SEED), jsonrpc: JSON_RPC_VERSION,
                method: method, params: parameters
    }

    payload[:auth] = @auth unless method.include? 'apiinfo'

    begin
      response = JSON.parse(request(payload), symbolize_names: true)
    rescue
{'fraction_non_alphanumeric': 0.12791601866251945, 'fraction_numerical': 0.01010886469673406, 'mean_word_length': 3.0329153605015673, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '1628080', 'n_tokens_mistral': 877, 'n_tokens_neox': 846, 'n_words': 212}