<commit_before>require_dependency 'carto/configuration'
require_dependency 'carto/deep_freeze'

module Cartodb
  def self.get_config(*config_chain)
    current = Cartodb.config
    config_chain.each { |config_param|
      current = current[config_param]
      if current.nil?
        break
      end
    }
    current if current.present?
  end

  def self.config
    return @config if @config

    begin
      config_file_hash = Carto::Conf.new.app_config
    rescue => e
      raise "Missing or inaccessible config/app_config.yml: #{e.message}"
    end
    @config ||= config_file_hash[Rails.env].try(:to_options!)
    Carto.deep_freeze(@config)

    if @config.blank?
      raise "Can't find App configuration for #{Rails.env} environment on config/app_config.yml"
    end

    # Check if we have all the important keys on config/app_config.yml
    raise "Missing mandatory_keys key on config/app_config.yml" unless @config[:mandatory_keys].present?
    unless(@config[:mandatory_keys].map(&:to_sym) - @config.keys).blank?
      raise "Missing the following config keys on config/app_config.yml: #{(@config[:mandatory_keys].map(&:to_sym) - @config.keys).join(', ')}"
    end
    ActionDispatch::Http::URL.tld_length = @config[:session_domain].split('.').delete_if {|i| i.empty? }.length - 1

    if !@config[:mailer].nil?
      # AuthSMTP
      CartoDB::Application.config.action_mailer.delivery_method = :smtp
      CartoDB::Application.config.action_mailer.smtp_settings = {
        :address              => Cartodb.get_config(:mailer, 'address'),
        :port                 => Cartodb.get_config(:mailer, 'port'),
        :user_name            => Cartodb.get_config(:mailer, 'user_name'),
        :password             => Cartodb.get_config(:mailer, 'password'),
        :authentication       => Cartodb.get_config(:mailer, 'authentication'),
        :enable_starttls_auto => Cartodb.get_config(:mailer, 'enable_starttls_auto')] }
    end

    if !@config[:basemaps].present? || @config[:basemaps].count == 0
      raise "Missing basemaps configuration, there should be at least one basemap"
    end
  end

  def self.error_codes
    return @error_codes if @error_codes
    file_hash = YAML.load_file("#{Rails.root}/config/error_codes.yml")
    @error_codes ||= file_hash["cartodb_errors"].try(:to_options!)<commit_msg>fix initializer<commit_after>require_dependency 'carto/configuration'
require_dependency 'carto/deep_freeze'

module Cartodb
  def self.get_config(*config_chain)
    current = Cartodb.config
    config_chain.each { |config_param|
      current = current[config_param]
      if current.nil?
        break
      end
    }
    current if current.present?
  end

  def self.config
    return @config if @config

    begin
      config_file_hash = Carto::Conf.new.app_config
    rescue => e
      raise "Missing or inaccessible config/app_config.yml: #{e.message}"
    end
    @config ||= config_file_hash[Rails.env].try(:to_options!)
    Carto.deep_freeze(@config)

    if @config.blank?
      raise "Can't find App configuration for #{Rails.env} environment on config/app_config.yml"
    end

    # Check if we have all the important keys on config/app_config.yml
    raise "Missing mandatory_keys key on config/app_config.yml" unless @config[:mandatory_keys].present?
    unless(@config[:mandatory_keys].map(&:to_sym) - @config.keys).blank?
      raise "Missing the following config keys on config/app_config.yml: #{(@config[:mandatory_keys].map(&:to_sym) - @config.keys).join(', ')}"
    end
    ActionDispatch::Http::URL.tld_length = @config[:session_domain].split('.').delete_if {|i| i.empty? }.length - 1

    if !@config[:mailer].nil?
      # AuthSMTP
      CartoDB::Application.config.action_mailer.delivery_method = :smtp
      CartoDB::Application.config.action_mailer.smtp_settings = {
        :address              => Cartodb.get_config(:mailer, 'address'),
        :port                 => Cartodb.get_config(:mailer, 'port'),
        :user_name            => Cartodb.get_config(:mailer, 'user_name'),
        :password             => <PASSWORD>_config(:mailer, 'password'),
        :authentication       => Cartodb.get_config(:mailer, 'authentication'),
        :enable_starttls_auto => Cartodb.get_config(:mailer, 'enable_starttls_auto') }
    end

    if !@config[:basemaps].present? || @config[:basemaps].count == 0
      raise "Missing basemaps configuration, there should be at least one basemap"
    end
  end

  def self.error_codes
    return @error_codes if @error_codes
    file_hash = YAML.load_file("#{Rails.root}/config/error_codes.yml")
    @error_codes ||= file_hash["cartodb_errors"].try(:to_options!)
{'fraction_non_alphanumeric': 0.1259131929522991, 'fraction_numerical': 0.0008594757198109154, 'mean_word_length': 3.38737040527804, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 18, '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': '3087494', 'n_tokens_mistral': 1507, 'n_tokens_neox': 1447, 'n_words': 290}