<commit_before>require 'yaml'

module Trackler
  # Problem is a language-independent definition of an exercise.
  class Problem
    attr_reader :slug, :root
    def initialize(slug, root)
      @slug = slug
      @root = root
    end

    def exists?
      !!description && !!metadata
    end

    def name
      slug.split('-').map(&:capitalize).join(' ')
    end

    def description
      @description ||= File.read(common_metadata_path(description_path)) if !!description_path
    end

    def source_markdown
      text = [source, markdown_link(source_url)].reject(&:empty?).join(" ")
      text.empty? ? text : "## Source\n\n#{text}"
    end

    ######
    # Deprecated methods
    # TODO: remove external references to these methods.
    # found in: x-api
    # NOT in: exercism.io, cli
    # Anywhere else we need to look?
    # Should this output a warning or raise an error?
    def md_url
      description_url
    end

    def json_url
      canonical_data_url
    end

    def yaml_url
      metadata_url
    end
    # End deprecated methods
    ######

    def description_url
      repo_url(description_path)
    end

    def canonical_data_url
      repo_url(canonical_data_path)
    end

    def metadata_url
      repo_url(metadata_path)
    end

    def blurb
      metadata['blurb'].to_s.strip
    end

    def source
      metadata['source'].to_s.strip
    end

    def source_url
      metadata['source_url'].to_s.strip
    end

    private

    def canonical_data_path
      [
        "exercises/%s/canonical-data.json" % slug,
      ].find { |filename| File.exist?(common_metadata_path(filename)) }
    end

    def metadata_path
      [
        "exercises/%s/metadata.yml" % slug,
      ].find { |filename| File.exist?(common_metadata_path(filename)) }
    end

    def description_path
      [
        "exercises/%s/description.md" % slug,
      ].find { |filename| File.exist?(common_metadata_path(filename)) }
    end

    def repo_url(path)
      "https://github.com/exercism/x-common/blob/master/#{path}" unless path.nil?
    end

    def metadata
      @metadata ||= YAML.load(File.read(common_metadata_path(metadata_path))) if !!metadata_path
    end

    def common_metadata_path(path)
      File.join(root, "common", path)
    end

    def markdown_link(url)
      url.empty? ? url : format("[%s](%s)", url, url)
    end
  end<commit_msg>Move file existence checks to a more relevant place.<commit_after>require 'yaml'

module Trackler
  # Problem is a language-independent definition of an exercise.
  class Problem
    attr_reader :slug, :root
    def initialize(slug, root)
      @slug = slug
      @root = root
    end

    def exists?
      !!description && !!metadata
    end

    def name
      slug.split('-').map(&:capitalize).join(' ')
    end

    def description
      @description ||= File.read(common_metadata_path(description_path)) if File.exists?(common_metadata_path(description_path))
    end

    def source_markdown
      text = [source, markdown_link(source_url)].reject(&:empty?).join(" ")
      text.empty? ? text : "## Source\n\n#{text}"
    end

    ######
    # Deprecated methods
    # TODO: remove external references to these methods.
    # found in: x-api
    # NOT in: exercism.io, cli
    # Anywhere else we need to look?
    # Should this output a warning or raise an error?
    def md_url
      description_url
    end

    def json_url
      canonical_data_url
    end

    def yaml_url
      metadata_url
    end
    # End deprecated methods
    ######

    def description_url
      repo_url(description_path)
    end

    def canonical_data_url
      repo_url(canonical_data_path) if File.exists?(common_metadata_path(canonical_data_path))
    end

    def metadata_url
      repo_url(metadata_path)
    end

    def blurb
      metadata.nil? ? '' :  metadata['blurb'].to_s.strip
    end

    def source
      metadata.nil? ? '' : metadata['source'].to_s.strip
    end

    def source_url
      metadata.nil? ? '' : metadata['source_url'].to_s.strip
    end

    private

    def canonical_data_path
      "exercises/%s/canonical-data.json" % slug
    end

    def metadata_path
      "exercises/%s/metadata.yml" % slug
    end

    def description_path
      "exercises/%s/description.md" % slug
    end

    def repo_url(path)
      "https://github.com/exercism/x-common/blob/master/#{path}" unless path.nil?
    end

    def metadata
      @metadata ||= if File.exists?(common_metadata_path(metadata_path))
                      YAML.load(File.read(common_metadata_path(metadata_path)))
                    end
    end

    def common_metadata_path(path)
      File.join(root, "common", path)
    end

    def markdown_link(url)
      url.empty? ? url : format("[%s](%s)", url, url)
    end
  end
{'fraction_non_alphanumeric': 0.10622250157133878, 'fraction_numerical': 0.0, 'mean_word_length': 2.5, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '4476514', 'n_tokens_mistral': 1533, 'n_tokens_neox': 1473, 'n_words': 350}