<commit_before>require "resolv"

module CartoDB

  begin
    CARTODB_REV = File.read("#{Rails.root}/REVISION").strip
  rescue
    CARTODB_REV = nil
  end

  DEFAULT_DB_SCHEMA = 'public'
  PUBLIC_DB_USER  = 'publicuser'
  PUBLIC_DB_USER_PASSWORD  = '<PASSWORD>'
  TILE_DB_USER    = 'tileuser'
  SRID            = 4326

  SURROGATE_NAMESPACE_VISUALIZATION = 'rv'
  SURROGATE_NAMESPACE_PUBLIC_PAGES = 'rp'
  SURROGATE_NAMESPACE_VIZJSON = 'rj'

  RESERVED_COLUMN_NAMES = %w(FORMAT CONTROLLER ACTION oid tableoid xmin cmin xmax cmax ctid ogc_fid).freeze

  LAST_BLOG_POSTS_FILE_PATH = "#{Rails.root}/public/system/last_blog_posts.html"

  # Helper method to encapsulate Rails full URL generation compatible with our subdomainless mode
  # @param context ActionController::Base or a View or something that holds a request
  # @param path String Rails route name
  # @param params Hash Parameters to send to the url (Optional)
  # @param user ::User (Optional) If not sent will use subdomain or /user/xxx from controller request
  def self.url(context, path, params={}, user = nil)
    # Must clean user_domain or else polymorphic_path will use it and generate again /u/xxx/user/xxx
    CartoDB.base_url_from_request(context.request, user) + context.polymorphic_path(path, params.merge({user_domain:nil}))
  end

  # Helper method to encapsulate Rails base URL generation compatible with our subdomainless mode
  # @param request A request to extract subdomain and parameters from
  # @param user ::User (Optional) If not sent will use subdomain or /user/xxx from controller request
  def self.base_url_from_request(request, user = nil)
    if user.nil?
      subdomain = self.extract_subdomain(request)
      org_username = nil
    else
      subdomain = user.subdomain
      org_username = user.organization_username
    end
    CartoDB.base_url(subdomain, org_username)
  end

  # Helper method to encapsulate Rails URL path generation compatible with our subdomainless mode
  # @param controller ActionController::Base
  # @param path String Rails route name
  # @param params Hash Parameters to send to the url (Optional)
  def self.path(controller, path, params={})
    controller.polymorphic_path(path, params)
  end

  # "Smart" subdomain extraction from the request, depending on configuration and /u/xxx url fragment
  def self.extract_subdomain(request)
    user_domain = self.username_from_request(request)
    if user_domain.nil?
      if subdomainless_urls? && ip?(request.host)
        ''
      else
        subdomain_from_request(request)
      end
    else<commit_msg>Hound<commit_after>require "resolv"

module CartoDB

  begin
    CARTODB_REV = File.read("#{Rails.root}/REVISION").strip
  rescue
    CARTODB_REV = nil
  end

  DEFAULT_DB_SCHEMA = 'public'
  PUBLIC_DB_USER  = 'publicuser'
  PUBLIC_DB_USER_PASSWORD  = '<PASSWORD>'
  TILE_DB_USER    = 'tileuser'
  SRID            = 4326

  SURROGATE_NAMESPACE_VISUALIZATION = 'rv'
  SURROGATE_NAMESPACE_PUBLIC_PAGES = 'rp'
  SURROGATE_NAMESPACE_VIZJSON = 'rj'

  RESERVED_COLUMN_NAMES = %w(FORMAT CONTROLLER ACTION oid tableoid xmin cmin xmax cmax ctid ogc_fid).freeze

  LAST_BLOG_POSTS_FILE_PATH = "#{Rails.root}/public/system/last_blog_posts.html"

  # Helper method to encapsulate Rails full URL generation compatible with our subdomainless mode
  # @param context ActionController::Base or a View or something that holds a request
  # @param path String Rails route name
  # @param params Hash Parameters to send to the url (Optional)
  # @param user ::User (Optional) If not sent will use subdomain or /user/xxx from controller request
  def self.url(context, path, params={}, user = nil)
    # Must clean user_domain or else polymorphic_path will use it and generate again /u/xxx/user/xxx
    CartoDB.base_url_from_request(context.request, user) + context.polymorphic_path(path, params.merge(user_domain: nil))
  end

  # Helper method to encapsulate Rails base URL generation compatible with our subdomainless mode
  # @param request A request to extract subdomain and parameters from
  # @param user ::User (Optional) If not sent will use subdomain or /user/xxx from controller request
  def self.base_url_from_request(request, user = nil)
    if user.nil?
      subdomain = extract_subdomain(request)
      org_username = nil
    else
      subdomain = user.subdomain
      org_username = user.organization_username
    end
    CartoDB.base_url(subdomain, org_username)
  end

  # Helper method to encapsulate Rails URL path generation compatible with our subdomainless mode
  # @param controller ActionController::Base
  # @param path String Rails route name
  # @param params Hash Parameters to send to the url (Optional)
  def self.path(controller, path, params={})
    controller.polymorphic_path(path, params)
  end

  # "Smart" subdomain extraction from the request, depending on configuration and /u/xxx url fragment
  def self.extract_subdomain(request)
    user_domain = self.username_from_request(request)
    if user_domain.nil?
      if subdomainless_urls? && ip?(request.host)
        ''
      else
        subdomain_from_request(request)
      end
    else
{'fraction_non_alphanumeric': 0.07253986775573706, 'fraction_numerical': 0.0015558148580318942, 'mean_word_length': 4.0077896786757545, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '5978214', 'n_tokens_mistral': 1636, 'n_tokens_neox': 1583, 'n_words': 486}