<commit_before>require 'rack/test'
require 'sinatra'
require 'rspec_api_docs/dsl'

RSpec.describe RspecApiDocs do
  include Rack::Test::Methods
  include RspecApiDocs::Dsl

  class TestApp < Sinatra::Base
    CHARACTERS = {
      1 => {name: '<NAME>'},
      2 => {name: '<NAME>'},
    }

    PLACES = {
      1 => {name: 'Candy Kingdom'},
      2 => {name: 'Tree Fort'},
    }

    get '/characters' do
      characters = CHARACTERS.map { |id, character| {id: id}.merge(character) }

      JSON.dump(data: characters)
    end

    get '/characters/:id' do
      id = params[:id].to_i
      character = CHARACTERS[id]

      if character
        JSON.dump(character: {id: id}.merge(character))
      else
        status 404
        JSON.dump(
          errors: {
            message: 'Character not found.',
          },
        )
      end
    end

    delete '/characters/:id' do
      JSON.dump(
        message: 'Character not found.',
      )
    end

    get '/places' do
      if params[:page]
        JSON.dump(data: [])
      else
        places = PLACES.map { |id, place| {id: id}.merge(place) }

        JSON.dump(data: places)
      end
    end
  end

  def app
    TestApp
  end

  describe 'Characters' do
    before do
      doc do
        resource_name 'Characters'
        resource_description <<-EOF.gsub(/^ {10}/, '')
          Characters inhabit the Land of Ooo.

          Use the following endpoints to fetch information and modify them.
        EOF
      end
    end

    it 'returns all characters' do
      doc do
        name 'Listing all characters'
        description <<-EOF.gsub(/^ {10}/, '')
          Getting all the characters.

          For when you need everything!
        EOF

        field :id, 'The id of a character', scope: [:data, nil], type: 'integer'
        field :name, "The character's name", scope: [:data, nil], type: 'string'
      end

      get '/characters'
    end

    it 'returns a character' do
      doc do
        name 'Fetching a Character'
        description 'For getting information about a Character.'
        path '/characters/:id'

        note 'You need to supply an id!'
        note :warning, "An error will be thrown if you don't supply an id!"

        param :id, 'The id of a character', type: 'integer', required: true

        field :id, 'The id of a character', scope: :character, type: 'integer'
        field :name, "The character's name", scope: :character, type: 'string'
      end

      get '/characters/1'
    end

    it 'returns 404' do
      doc do
        name 'When a Character cannot be found'
        description 'Returns an error'
        path '/characters/:id'

        note :danger, 'This is an error case'

        field :message, 'Error message', scope: :errors, type: 'string'
      end

      get '/characters/404'
    end

    it 'Deleting a Character' do
      doc do
        # NOTE: name defaults to the description of the RSpec example
        description 'For getting information about a Character.'
        path '/characters/:id'

        param :id, 'The id of a character', type: 'integer', required: true

        field :message, 'Success message', type: 'string'
      end

      delete '/characters/1'
    end

    it 'is unrelated' do
      doc false
    end
  end

  describe 'Places' do
    before do
      doc do
        resource_name 'Places'
        resource_description <<-EOF.gsub(/^ {10}/, '')
          This category consists of locations in the Land of Ooo.

          These are all great places!

          The Characters that live here are great too.
        EOF
      end
    end

    describe 'GET /places' do
      before do
        doc do
          field :id, 'The id of the place', scope: [:data, '<Array>'], type: 'integer'
          field :name, "The place's name", scope: [:data, '<Array>'], type: 'string'
        end<commit_msg>Fix remaining scope change in integration spec<commit_after>require 'rack/test'
require 'sinatra'
require 'rspec_api_docs/dsl'

RSpec.describe RspecApiDocs do
  include Rack::Test::Methods
  include RspecApiDocs::Dsl

  class TestApp < Sinatra::Base
    CHARACTERS = {
      1 => {name: '<NAME> Human'},
      2 => {name: '<NAME>'},
    }

    PLACES = {
      1 => {name: 'Candy Kingdom'},
      2 => {name: 'Tree Fort'},
    }

    get '/characters' do
      characters = CHARACTERS.map { |id, character| {id: id}.merge(character) }

      JSON.dump(data: characters)
    end

    get '/characters/:id' do
      id = params[:id].to_i
      character = CHARACTERS[id]

      if character
        JSON.dump(character: {id: id}.merge(character))
      else
        status 404
        JSON.dump(
          errors: {
            message: 'Character not found.',
          },
        )
      end
    end

    delete '/characters/:id' do
      JSON.dump(
        message: 'Character not found.',
      )
    end

    get '/places' do
      if params[:page]
        JSON.dump(data: [])
      else
        places = PLACES.map { |id, place| {id: id}.merge(place) }

        JSON.dump(data: places)
      end
    end
  end

  def app
    TestApp
  end

  describe 'Characters' do
    before do
      doc do
        resource_name 'Characters'
        resource_description <<-EOF.gsub(/^ {10}/, '')
          Characters inhabit the Land of Ooo.

          Use the following endpoints to fetch information and modify them.
        EOF
      end
    end

    it 'returns all characters' do
      doc do
        name 'Listing all characters'
        description <<-EOF.gsub(/^ {10}/, '')
          Getting all the characters.

          For when you need everything!
        EOF

        field :id, 'The id of a character', scope: [:data, nil], type: 'integer'
        field :name, "The character's name", scope: [:data, nil], type: 'string'
      end

      get '/characters'
    end

    it 'returns a character' do
      doc do
        name 'Fetching a Character'
        description 'For getting information about a Character.'
        path '/characters/:id'

        note 'You need to supply an id!'
        note :warning, "An error will be thrown if you don't supply an id!"

        param :id, 'The id of a character', type: 'integer', required: true

        field :id, 'The id of a character', scope: :character, type: 'integer'
        field :name, "The character's name", scope: :character, type: 'string'
      end

      get '/characters/1'
    end

    it 'returns 404' do
      doc do
        name 'When a Character cannot be found'
        description 'Returns an error'
        path '/characters/:id'

        note :danger, 'This is an error case'

        field :message, 'Error message', scope: :errors, type: 'string'
      end

      get '/characters/404'
    end

    it 'Deleting a Character' do
      doc do
        # NOTE: name defaults to the description of the RSpec example
        description 'For getting information about a Character.'
        path '/characters/:id'

        param :id, 'The id of a character', type: 'integer', required: true

        field :message, 'Success message', type: 'string'
      end

      delete '/characters/1'
    end

    it 'is unrelated' do
      doc false
    end
  end

  describe 'Places' do
    before do
      doc do
        resource_name 'Places'
        resource_description <<-EOF.gsub(/^ {10}/, '')
          This category consists of locations in the Land of Ooo.

          These are all great places!

          The Characters that live here are great too.
        EOF
      end
    end

    describe 'GET /places' do
      before do
        doc do
          field :id, 'The id of the place', scope: [:data, nil], type: 'integer'
          field :name, "The place's name", scope: [:data, nil], type: 'string'
        end
{'fraction_non_alphanumeric': 0.106676998579362, 'fraction_numerical': 0.005424254165052305, 'mean_word_length': 1.9647779479326186, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 17, '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': '4540802', 'n_tokens_mistral': 2344, 'n_tokens_neox': 2233, 'n_words': 840}