<commit_before>require 'erb'

module Codewars
  class TrainSpecific < Thor
    def initialize(id_or_slug)
      message = []
      api_key = Configuration.option('api_key')
      message.push 'You should set an api-key to use this command' unless api_key
      languages = Configuration.option('language')
      message.push 'You should set an default language to use this command' unless languages
      fail Thor::Error, message.join("\n") unless message.empty?

      say "Starting the '#{id_or_slug}' kata."

      client = CodewarsApi::Client.new(api_key: api_key)
      languages.split(',').each do |language|
        kata = client.train_specific_kata(language: language, id_or_slug: id_or_slug) rescue next
        handle_specific_kata(kata, language)
      end
    end

    private

    def handle_specific_kata(kata, language)
      slug = kata.slug
      write_kata(slug, language, 'description.md', description(kata, language))
      exten = LanguageExtensions.get(language)
      unless kata.tests_setup.to_s.empty?
        write_kata(slug, language, "test.#{exten}", kata.tests_setup)
      end
      write_kata(slug, language, "solution.#{exten}", kata.code_setup)
    end

    def write_kata(slug, language, file_name, content)
      dir_to_write = File.expand_path(slug)
      Dir.mkdir dir_to_write unless File.directory?(dir_to_write)
      Dir.mkdir "#{dir_to_write}/#{language}" unless File.directory?("#{dir_to_write}/#{language}")
      file_path = File.expand_path("#{dir_to_write}/#{language}/#{file_name}")
      relative_path = "./#{slug}/#{language}/#{file_name}"
      fail Thor::Error, "'#{relative_path}' already exists." if File.exist? file_path

      File.write(file_path, content)
      say "'#{relative_path}' has been created."
    end

    def description(kata, language)
      b = binding
      b.local_variable_set(:kata, kata)
      b.local_variable_set(:language, language)
      b.local_variable_set(:codewars_url, CODEWARS_URL)
      template_path = File.expand_path("#{File.dirname(__FILE__)}/description_template.erb")
      ERB.new(File.read template_path).result(b)
    end
  end
end<commit_msg>Small refactor when creating files<commit_after>require 'erb'
require 'fileutils'

module Codewars
  class TrainSpecific < Thor
    def initialize(id_or_slug)
      message = []
      api_key = Configuration.option('api_key')
      message.push 'You should set an api-key to use this command' unless api_key
      languages = Configuration.option('language')
      message.push 'You should set an default language to use this command' unless languages
      fail Thor::Error, message.join("\n") unless message.empty?

      say "Starting the '#{id_or_slug}' kata."

      client = CodewarsApi::Client.new(api_key: api_key)
      languages.split(',').each do |language|
        kata = client.train_specific_kata(language: language, id_or_slug: id_or_slug) rescue next
        handle_specific_kata(kata, language)
      end
    end

    private

    def handle_specific_kata(kata, language)
      slug = kata.slug
      write_kata(slug, language, 'description.md', description(kata, language))
      exten = LanguageExtensions.get(language)
      unless kata.tests_setup.to_s.empty?
        write_kata(slug, language, "test.#{exten}", kata.tests_setup)
      end
      write_kata(slug, language, "solution.#{exten}", kata.code_setup)
    end

    def write_kata(slug, language, file_name, content)
      dir_to_write = File.expand_path(slug)
      FileUtils.mkdir_p "#{dir_to_write}/#{language}"
      file_path = File.expand_path("#{dir_to_write}/#{language}/#{file_name}")
      relative_path = "./#{slug}/#{language}/#{file_name}"
      fail Thor::Error, "'#{relative_path}' already exists." if File.exist? file_path

      File.write(file_path, content)
      say "'#{relative_path}' has been created."
    end

    def description(kata, language)
      b = binding
      b.local_variable_set(:kata, kata)
      b.local_variable_set(:language, language)
      b.local_variable_set(:codewars_url, CODEWARS_URL)
      template_path = File.expand_path("#{File.dirname(__FILE__)}/description_template.erb")
      ERB.new(File.read template_path).result(b)
    end
  end
end
{'fraction_non_alphanumeric': 0.11868027533823879, 'fraction_numerical': 0.0, 'mean_word_length': 3.6256860592755213, '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': '7366472', 'n_tokens_mistral': 1337, 'n_tokens_neox': 1362, 'n_words': 287}