<commit_before># Assign this to a variable in the function that requires it
# ENV['DBAAS_COORDINATOR_ENDPOINT']

module OntotextUser
  private
  def new_api_connexion
    Faraday.new(:url => 'https://api.datagraft.net') do |faraday|
      faraday.request :multipart
      faraday.request :url_encoded

      faraday.response :logger                  # log requests to STDOUT
      faraday.use :cookie_jar                   # yes we do use cookies
      faraday.adapter  Faraday.default_adapter  # make requests with Net::HTTP
    end
  end

  # Register new ontotext account
  def register_ontotext_account
    return if has_ontotext_account

    # We generate a random token so the process should still work after a failure
    # or if two concurrents registration happen.
    # The Ontotext db requires both usernames and emails to be unique.
    # Thus we need the random token to ensure uniqueness of these.
    # We should ask Ontotext to remove the uniqeness requirement for emails.
    account_token = rand(1..100000000).to_i

    conn = new_api_connexion
    resp = conn.post do |req|
      req.options[:timeout] = 5 
      req.url '/dapaas-management-services/api/accounts'
      req.headers['Content-Type'] = 'application/json'
      req.body = {
        username: username+'_'+account_token.to_s,
        password: <PASSWORD>,
        name: name,
        role: 'rails wrapper',
        # email: self.email
        email: 'a.pultier+'+account_token.to_s+'@gmail.com'
        }.to_json
      puts req.body
    end

    if resp.status.between?(200, 299)
      self.ontotext_account = account_token
      save!
    else 
      puts resp.body
      throw "Unable to register an Ontotext user"
    end

    conn
  end

  # Register new ontotext test account (only used for testing)
  def register_ontotext_test_account
    return if has_ontotext_account

    # The Ontotext db requires both usernames and emails to be unique.
    # In the register ontotext account method we generate a random token 
    # to ensure uniqueness of these. For the test user we set a fixed token.
    account_token = <PASSWORD>

    conn = new_api_connexion
    resp = conn.post do |req|
      req.options[:timeout] = 5 
      req.url '/dapaas-management-services/api/accounts'
      req.headers['Content-Type'] = 'application/json'
      req.body = {
        username: username+'_'+account_token.to_s,
        password: password,
        name: name,
        role: 'rails wrapper',
        # email: self.email
        email: 'a.pultier+'+account_token.to_s+'@gmail.com'
        }.to_json
      puts req.body
    end

    if resp.status.between?(200, 299)
      self.ontotext_account = account_token
      save!
    else
      puts resp.body
      throw "Unable to register an Ontotext test user"
    end<commit_msg>Use the environment (testing, development, staging or production) as a role for users<commit_after># Assign this to a variable in the function that requires it
# ENV['DBAAS_COORDINATOR_ENDPOINT']

module OntotextUser
  private
  def new_api_connexion
    Faraday.new(:url => 'https://api.datagraft.net') do |faraday|
      faraday.request :multipart
      faraday.request :url_encoded

      faraday.response :logger                  # log requests to STDOUT
      faraday.use :cookie_jar                   # yes we do use cookies
      faraday.adapter  Faraday.default_adapter  # make requests with Net::HTTP
    end
  end

  # Register new ontotext account
  def register_ontotext_account
    return if has_ontotext_account

    # We generate a random token so the process should still work after a failure
    # or if two concurrents registration happen.
    # The Ontotext db requires both usernames and emails to be unique.
    # Thus we need the random token to ensure uniqueness of these.
    # We should ask Ontotext to remove the uniqeness requirement for emails.
    account_token = rand(1..100000000).to_i

    conn = new_api_connexion
    resp = conn.post do |req|
      req.options[:timeout] = 5 
      req.url '/dapaas-management-services/api/accounts'
      req.headers['Content-Type'] = 'application/json'
      req.body = {
        username: username+'_'+account_token.to_s,
        password: <PASSWORD>,
        name: name,
        role: ENV['RAILS_ENV'],
        # email: self.email
        email: 'a.pultier+'+account_token.to_s+'@gmail.com'
        }.to_json
      puts req.body
    end

    if resp.status.between?(200, 299)
      self.ontotext_account = account_token
      save!
    else 
      puts resp.body
      throw "Unable to register an Ontotext user"
    end

    conn
  end

  # Register new ontotext test account (only used for testing)
  def register_ontotext_test_account
    return if has_ontotext_account

    # The Ontotext db requires both usernames and emails to be unique.
    # In the register ontotext account method we generate a random token 
    # to ensure uniqueness of these. For the test user we set a fixed token.
    account_token = <PASSWORD>

    conn = new_api_connexion
    resp = conn.post do |req|
      req.options[:timeout] = 5 
      req.url '/dapaas-management-services/api/accounts'
      req.headers['Content-Type'] = 'application/json'
      req.body = {
        username: username+'_'+account_token.to_s,
        password: password,
        name: name,
        role: ENV['RAILS_ENV'],
        # email: self.email
        email: 'a.p<EMAIL>ier+'+account_token.to_s+'@gmail.com'
        }.to_json
      puts req.body
    end

    if resp.status.between?(200, 299)
      self.ontotext_account = account_token
      save!
    else
      puts resp.body
      throw "Unable to register an Ontotext test user"
    end
{'fraction_non_alphanumeric': 0.07960549489256781, 'fraction_numerical': 0.008453680873547023, 'mean_word_length': 2.592030360531309, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 10, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '4482518', 'n_tokens_mistral': 1779, 'n_tokens_neox': 1699, 'n_words': 529}