<commit_before>module Api
  class AbstractController < ApplicationController
    class OnlyJson < Exception; end;
    CONSENT_REQUIRED = "all device users must agree to terms of service."

    respond_to :json
    before_action :check_fbos_version
    before_action :set_default_response_format
    before_action :authenticate_user!
    skip_before_action :verify_authenticity_token
    after_action :skip_set_cookies_header
    rescue_from(JWT::VerificationError) { |e| auth_err }

    rescue_from Errors::Forbidden do |exc|
      sorry "You can't perform that action. #{exc.message}", 403
    end

    rescue_from Errors::NoBot do |exc|
      sorry "You need to register a device first.", 422
    end

    rescue_from ActiveRecord::RecordNotFound do |exc|
      sorry "Document not found.", 404
    end

    rescue_from ActiveRecord::RecordInvalid do |exc|
      render json: {error: exc.message}, status: 422
    end

    rescue_from Errors::LegalConsent do |exc|
      render json: {error: CONSENT_REQUIRED}, status: 451
    end

private

    def clean_expired_farm_events
      FarmEvents::CleanExpired.run!(device: current_device)
    end

    # Rails 5 params are no longer simple hashes. This was for security reasons.
    # Our API does not do things the "Rails way" (we use Mutations for input
    # sanitation) so we can ignore this and grab the raw input.<commit_msg>Rescue from param parse errors<commit_after>module Api
  class AbstractController < ApplicationController
    class OnlyJson < Exception; end;
    CONSENT_REQUIRED = "all device users must agree to terms of service."

    respond_to :json
    before_action :check_fbos_version
    before_action :set_default_response_format
    before_action :authenticate_user!
    skip_before_action :verify_authenticity_token
    after_action :skip_set_cookies_header
    rescue_from(JWT::VerificationError) { |e| auth_err }

    rescue_from Errors::Forbidden do |exc|
      sorry "You can't perform that action. #{exc.message}", 403
    end

    rescue_from Errors::NoBot do |exc|
      sorry "You need to register a device first.", 422
    end

    rescue_from ActiveRecord::RecordNotFound do |exc|
      sorry "Document not found.", 404
    end

    rescue_from ActiveRecord::RecordInvalid do |exc|
      render json: {error: exc.message}, status: 422
    end

    rescue_from Errors::LegalConsent do |exc|
      render json: {error: CONSENT_REQUIRED}, status: 451
    end

    rescue_from ActionDispatch::ParamsParser::ParseError do |exc|
      sorry "You have a typo in your JSON.", 422
    end

private

    def clean_expired_farm_events
      FarmEvents::CleanExpired.run!(device: current_device)
    end

    # Rails 5 params are no longer simple hashes. This was for security reasons.
    # Our API does not do things the "Rails way" (we use Mutations for input
    # sanitation) so we can ignore this and grab the raw input.
{'fraction_non_alphanumeric': 0.06760952052431873, 'fraction_numerical': 0.012073128665056916, 'mean_word_length': 3.6104928457869634, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, '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': '5819572', 'n_tokens_mistral': 884, 'n_tokens_neox': 848, 'n_words': 291}