<commit_before>require 'open-uri'

class Apitome::DocsController < ActionController::Base
  layout Apitome.configuration.layout

  helper_method *[
    :resources,
    :example,
    :formatted_body,
    :param_headers,
    :param_extras,
    :formatted_readme,
    :set_example,
    :id_for,
    :rendered_markdown
  ]

  def index
  end

  def show
  end

  def simulate
    request = example["requests"][0]
    if request
      request["response_headers"].each { |k, v| headers[k] = v }
      render plain: request["response_body"], status: request["response_status"]
    else
      render plain: "No simulation for this endpoint", status: 404
    end
  end

  private

  def file_for(file, readme: false)
    if Apitome.configuration.remote_docs
      file = if readme
        "#{file}"
      else
        "#{Apitome.configuration.doc_path}/#{file}"
      end

      file = "#{Apitome.configuration.remote_url}/#{file}"
    else
      file = Apitome.configuration.root.join(Apitome.configuration.doc_path, file)
      raise Apitome::FileNotFoundError.new("Unable to find #{file}") unless File.exists?(file)
    end

    open(file).read
  end

  def resources
    @resources ||= JSON.parse(file_for("index.json"))["resources"]
  end

  def example
    @example ||= JSON.parse(file_for("#{params[:path]}.json"))<commit_msg>Changes render :plain to render :body, and removes content length from headers because it can mess with the length in simulation.<commit_after>require 'open-uri'

class Apitome::DocsController < ActionController::Base
  layout Apitome.configuration.layout

  helper_method *[
    :resources,
    :example,
    :formatted_body,
    :param_headers,
    :param_extras,
    :formatted_readme,
    :set_example,
    :id_for,
    :rendered_markdown
  ]

  def index
  end

  def show
  end

  def simulate
    request = example["requests"][0]
    if request
      request["response_headers"].delete('Content-Length')
      request["response_headers"].each { |k, v| headers[k] = v }
      render body: request["response_body"], status: request["response_status"]
    else
      render body: "No simulation for this endpoint", status: 404
    end
  end

  private

  def file_for(file, readme: false)
    if Apitome.configuration.remote_docs
      file = if readme
        "#{file}"
      else
        "#{Apitome.configuration.doc_path}/#{file}"
      end

      file = "#{Apitome.configuration.remote_url}/#{file}"
    else
      file = Apitome.configuration.root.join(Apitome.configuration.doc_path, file)
      raise Apitome::FileNotFoundError.new("Unable to find #{file}") unless File.exists?(file)
    end

    open(file).read
  end

  def resources
    @resources ||= JSON.parse(file_for("index.json"))["resources"]
  end

  def example
    @example ||= JSON.parse(file_for("#{params[:path]}.json"))
{'fraction_non_alphanumeric': 0.12123360510457284, 'fraction_numerical': 0.002835873803615739, 'mean_word_length': 3.3083969465648857, 'pattern_counts': {'":': 0, '<': 5, '<?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': '4728025', 'n_tokens_mistral': 901, 'n_tokens_neox': 877, 'n_words': 200}