<commit_before>require 'net/http'
require 'uri'

class Content::CasesController < ApplicationController
  before_action :find_case, if: lambda {params[:case_id].present?}
  before_action :set_page_title, except: [:from_capapi]

  # create a temporary resource to display the case
  layout 'casebooks'
  def show
    @include_vuejs = true
    unless @case.try :public
      flash[:notice] = "You are not authorized to access this page."
      redirect_to :root and return
    end

    @resource = Content::Resource.new resource: @case
    @content = @resource
    render 'content/resources/show'
  end

  def page_title
    if @case.present?
      if action_name == 'edit'
        I18n.t 'content.titles.cases.edit', case_name_abbreviation: @case.name_abbreviation
      else
        I18n.t 'content.titles.cases.show', case_name_abbreviation: @case.name_abbreviation
      end
    else
      I18n.t 'content.titles.cases.read', case_name_abbreviation: @case.name_abbreviation
    end
  end

  def from_capapi
    params.require :id
    @case = Case.find_by(capapi_id: params[:id]) ||
            Case.create(
              {public: true,
               created_via_import: true}.merge(
                Capapi::ModelHelpers.to_attributes(
                  Capapi::Case.retrieve({id: params[:id],
                                         full_case: "true",
                                         body_format: "html"}))))
    render json: {id: @case.id}
  end

  def find_case
    @case = Case.find(params[:case_id])
  end

  def set_page_title
    @page_title = page_title
  end

  rescue_from ActionController::ParameterMissing do |exception|
    render json: {exception.param => "is required"}, status: 400
  end
end
<commit_msg>Only search for public cases locally before calling capapi.<commit_after>require 'net/http'
require 'uri'

class Content::CasesController < ApplicationController
  before_action :find_case, if: lambda {params[:case_id].present?}
  before_action :set_page_title, except: [:from_capapi]

  # create a temporary resource to display the case
  layout 'casebooks'
  def show
    @include_vuejs = true
    unless @case.try :public
      flash[:notice] = "You are not authorized to access this page."
      redirect_to :root and return
    end

    @resource = Content::Resource.new resource: @case
    @content = @resource
    render 'content/resources/show'
  end

  def page_title
    if @case.present?
      if action_name == 'edit'
        I18n.t 'content.titles.cases.edit', case_name_abbreviation: @case.name_abbreviation
      else
        I18n.t 'content.titles.cases.show', case_name_abbreviation: @case.name_abbreviation
      end
    else
      I18n.t 'content.titles.cases.read', case_name_abbreviation: @case.name_abbreviation
    end
  end

  def from_capapi
    params.require :id
    @case = Case.find_by(capapi_id: params[:id], public: true) ||
            Case.create(
              {public: true,
               created_via_import: true}.merge(
                Capapi::ModelHelpers.to_attributes(
                  Capapi::Case.retrieve({id: params[:id],
                                         full_case: "true",
                                         body_format: "html"}))))
    render json: {id: @case.id}
  end

  def find_case
    @case = Case.find(params[:case_id])
  end

  def set_page_title
    @page_title = page_title
  end

  rescue_from ActionController::ParameterMissing do |exception|
    render json: {exception.param => "is required"}, status: 400
  end
end

{'fraction_non_alphanumeric': 0.09846765039727583, 'fraction_numerical': 0.00510783200908059, 'mean_word_length': 2.626543209876543, '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': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6708960', 'n_tokens_mistral': 1092, 'n_tokens_neox': 1053, 'n_words': 264}