<commit_before>class Api::UsersController < ApplicationController
  protect_from_forgery with: :null_session, if: Proc.new { |c| c.request.format.json? }

  def connect
    if params[:secret] != CatarseSettings[:api_secret]
      failure
      return
    end
    user = params['user']
    begin
      u= User.new(raiseit_id: user['id'],
                   raiseit_key: user['api_key'],
                   email: user['email'],
                   name: user['name']
                  )
      u.save!
      u.reload
    rescue
      render :json => { :error => "Error creating user" }, :status => 400
      return
    end

    render json: {api_key: u.authentication_token, id: u.id}.to_json, status: 200
  end

  def subscriptions
    if params[:secret] != CatarseSettings[:api_secret]
      failure
      return
    end
    user = User.find_by_authentication_token params[:authentication_token]
    subscriptions = user.projects.with_state(:online).last.subscriptions.active.order('subscriptions.created_at desc').limit(25)
    api_response = {
      "_total": subscriptions.count,
      "subscriptions": subscriptions_json(subscriptions)
    }
    render json: api_response.to_json, status: 200
  end

  private
  def failure
    render :json => { :error => "Login Credentials Failed" }, :status => 401
  end

  def subscriptions_json(subscriptions)<commit_msg>add user not found error<commit_after>class Api::UsersController < ApplicationController
  protect_from_forgery with: :null_session, if: Proc.new { |c| c.request.format.json? }

  def connect
    if params[:secret] != CatarseSettings[:api_secret]
      failure
      return
    end
    user = params['user']
    begin
      u= User.new(raiseit_id: user['id'],
                   raiseit_key: user['api_key'],
                   email: user['email'],
                   name: user['name']
                  )
      u.save!
      u.reload
    rescue
      render :json => { :error => "Error creating user" }, :status => 400
      return
    end

    render json: {api_key: u.authentication_token, id: u.id}.to_json, status: 200
  end

  def subscriptions
    if params[:secret] != CatarseSettings[:api_secret]
      failure
      return
    end
    user = User.find_by_authentication_token params[:authentication_token]
    if !user
      render json: { :error => "User not found" }.to_json, status: 404
      return
    end
    subscriptions = user.projects.with_state(:online).last.subscriptions.active.order('subscriptions.created_at desc').limit(25)
    api_response = {
      "_total": subscriptions.count,
      "subscriptions": subscriptions_json(subscriptions)
    }
    render json: api_response.to_json, status: 200
  end

  private
  def failure
    render :json => { :error => "Login Credentials Failed" }, :status => 401
  end

  def subscriptions_json(subscriptions)
{'fraction_non_alphanumeric': 0.11056338028169015, 'fraction_numerical': 0.010915492957746478, 'mean_word_length': 2.7629139072847684, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 16, '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': '3210224', 'n_tokens_mistral': 878, 'n_tokens_neox': 827, 'n_words': 198}