<commit_before>module Xapi
  module Routes
    class Exercises < Core
      get '/exercises' do
        unless params[:key]
          halt 401, {error: "Please provide your Exercism.io API key"}.to_json
        end

        pg :exercises, locals: {exercises: Xapi::UserHomework.exercises_for(params[:key])}
      end

      get '/exercises/restore' do
        unless params[:key]
          halt 401, {error: "Please provide your Exercism.io API key"}.to_json
        end

        pg :exercises, locals: {exercises: Xapi::UserHomework.restore(params[:key])}
      end

      get '/exercises/:language/:slug' do |language, slug|
        exercise = Exercise.new(language, slug)
        if exercise.unknown_language?
          halt 404, {error: "We don't have exercises in '#{language}'"}.to_json
        elsif exercise.unknown_exercise?
          halt 404, {error: "We don't have '#{slug}' for '#{language}'"}.to_json
        end
        pg :exercises, locals: {exercises: [exercise]}
      end
    end
  end
end<commit_msg>Move guard into helper method<commit_after>module Xapi
  module Routes
    class Exercises < Core
      helpers do
        def require_key
          unless params[:key]
            halt 401, {error: "Please provide your Exercism.io API key"}.to_json
          end
        end
      end

      get '/exercises' do
        require_key
        pg :exercises, locals: {exercises: Xapi::UserHomework.exercises_for(params[:key])}
      end

      get '/exercises/restore' do
        require_key
        pg :exercises, locals: {exercises: Xapi::UserHomework.restore(params[:key])}
      end

      get '/exercises/:language/:slug' do |language, slug|
        exercise = Exercise.new(language, slug)
        if exercise.unknown_language?
          halt 404, {error: "We don't have exercises in '#{language}'"}.to_json
        elsif exercise.unknown_exercise?
          halt 404, {error: "We don't have '#{slug}' for '#{language}'"}.to_json
        end
        pg :exercises, locals: {exercises: [exercise]}
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.11089399120664387, 'fraction_numerical': 0.010258915486077186, 'mean_word_length': 2.436241610738255, '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': '1433691', 'n_tokens_mistral': 664, 'n_tokens_neox': 622, 'n_words': 177}