<commit_before># -*- encoding : utf-8 -*-

require "net/http"
require "uri"

module Roempro
  ##
  # This specific class is responsible to actually perform
  # the request to the given Oempro API.
  #
  # It give a way to merely deal with the API handling all
  # the undestood commands. Actually, it build the request
  # chain from the methods names and the hash submit as argument.
  #
  # For instance, to fetch all emails :
  #
  #   req = Roempro::Request.new
  #   # => <#Roempro::Request>
  #   req.emails.get    # It does the trick
  #
  # Then, `req.emails.get` return Roempro::Response object.
  # See Roempro::Response to learn more.
  class Request

    ##
    # Provide a new Roempro::Request object
    def initialize(params={})
      @url , @user, @password = params[:url], params[:username], params[:password]
    end

    def method_missing(method_id, *args)
      if not @query
        if args.any?
          raise ArgumentError, "#{method_id.to_s} doesn't accept any arguments"
        end

        @query = { :command => [method_id.to_s.capitalize] }
        return self
      else
        unless args.empty? or args.first.kind_of? Hash
          raise ArgumentError, "#{self.class}##{method_id.to_s} only accept hash argument"
        end

        @query[:command].push(method_id.to_s.capitalize)
        perform args.first if login
      end

    rescue ArgumentError => message
      @query = nil
      puts message
      return self
    end

    def last_response
      @last_response
    end

    def login
      if not @session_id
        unless @user and @password
          raise ArgumentError, "You have to submit your username and password to log into Oempro"
        end

        last_query = @query
        @query = { :command => ["User", "Login"] }
        perform :username => @user, :password => <PASSWORD>, :disablecaptcha => true
        @query = last_query

        unless @last_response.success
          raise RuntimeError, @last_response.error_text.join("\n")
        end

        @session_id = @last_response.session_id
      end

    rescue ArgumentError => message
      puts message
    end

    private

      def perform(params={})
        unless @url
          raise ArgumentError, "Unable to perform the request : Uknown URL to Oempro"
        end
        unless params.nil? or params.kind_of? Hash
          raise ArgumentError, "Unable to perform the request : params have to be a hash"
        end

        @query[:command] = @query[:command].join(".")<commit_msg>Add Roempro::Request#logged_in? method<commit_after># -*- encoding : utf-8 -*-

require "net/http"
require "uri"

module Roempro
  ##
  # This specific class is responsible to actually perform
  # the request to the given Oempro API.
  #
  # It give a way to merely deal with the API handling all
  # the undestood commands. Actually, it build the request
  # chain from the methods names and the hash submit as argument.
  #
  # For instance, to fetch all emails :
  #
  #   req = Roempro::Request.new
  #   # => <#Roempro::Request>
  #   req.emails.get    # It does the trick
  #
  # Then, `req.emails.get` return Roempro::Response object.
  # See Roempro::Response to learn more.
  class Request

    ##
    # Provide a new Roempro::Request object
    def initialize(params={})
      @url , @user, @password = params[:url], params[:username], params[:password]
    end

    def method_missing(method_id, *args)
      if not @query
        if args.any?
          raise ArgumentError, "#{method_id.to_s} doesn't accept any arguments"
        end

        @query = { :command => [method_id.to_s.capitalize] }
        return self
      else
        unless args.empty? or args.first.kind_of? Hash
          raise ArgumentError, "#{self.class}##{method_id.to_s} only accept hash argument"
        end

        login unless logged_in?
        @query[:command].push(method_id.to_s.capitalize)
        perform args.first if logged_in?
      end

    rescue ArgumentError => message
      @query = nil
      puts message
      return self
    end

    def last_response
      @last_response
    end

    def logged_in?
      @session_id ? true : false;
    end

    def login
      unless logged_in?
        unless @user and @password
          raise ArgumentError, "You have to submit your username and password to log into Oempro"
        end

        last_query = @query
        @query = { :command => ["User", "Login"] }
        perform :username => @user, :password => <PASSWORD>, :disablecaptcha => true
        @query = last_query

        unless @last_response.success
          raise RuntimeError, @last_response.error_text.join("\n")
        end

        @session_id = @last_response.session_id
      end

    rescue ArgumentError => message
      puts message
    end

    private

      def perform(params={})
        unless @url
          raise ArgumentError, "Unable to perform the request : Uknown URL to Oempro"
        end
        unless params.nil? or params.kind_of? Hash
          raise ArgumentError, "Unable to perform the request : params have to be a hash"
        end

        @query[:command] = @query[:command].join(".")
{'fraction_non_alphanumeric': 0.09482257126236183, 'fraction_numerical': 0.00038782237735117316, 'mean_word_length': 2.3756544502617802, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 23, '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': '623313', 'n_tokens_mistral': 1511, 'n_tokens_neox': 1481, 'n_words': 493}