<commit_before>require 'ostruct'

class Realm < ActiveRecord::Base

  has_many :accounts
  has_many :identities
  has_many :domains
  belongs_to :primary_domain, :class_name => "Domain"

  validates_uniqueness_of :label, :allow_nil => false

  def self.find_by_url(url)
    search_strings_for_url(url) do |domain|
      result = joins(:domains).where('domains.name = ?', domain).first
      return result if result
    end
  end

  def god_sessions
    Session.where("sessions.identity_id in (select id from identities where god and realm_id = ?)", id)
  end

  def external_service_keys
    keys = YAML.load(self.service_keys)
    raise "Missing or malformed configuration for #<Realm:#{id} #{label}>" unless keys
    keys
  end

  def keys_for(provider)
    OpenStruct.new(external_service_keys.fetch(provider))
  end

  private

  # Provided a block, this method will yield variations on the host with increasing 
  # specificity: 'foo.bar.example.com' will yield "example.com", "bar.example.com",
  # "foo.bar.example.com".
  def self.search_strings_for_url(url, &block)
    segments = url[/(?:https?\:\/\/)?([^\/]+)/,1].split('.') # extract the domain and split the subdomains
    candidate = [segments.pop]
    yield(candidate.unshift(segments.pop).join('.')) until segments.empty?
  end

end<commit_msg>Catch unlikely nil error in Realm#external_service_keys.<commit_after>require 'ostruct'

class Realm < ActiveRecord::Base

  has_many :accounts
  has_many :identities
  has_many :domains
  belongs_to :primary_domain, :class_name => "Domain"

  validates_uniqueness_of :label, :allow_nil => false

  def self.find_by_url(url)
    search_strings_for_url(url) do |domain|
      result = joins(:domains).where('domains.name = ?', domain).first
      return result if result
    end
  end

  def god_sessions
    Session.where("sessions.identity_id in (select id from identities where god and realm_id = ?)", id)
  end

  def external_service_keys
    keys = YAML.load(self.service_keys) if self.service_keys.present?
    raise "Missing or malformed configuration for #<Realm:#{id} #{label}>" unless keys
    keys
  end

  def keys_for(provider)
    OpenStruct.new(external_service_keys.fetch(provider))
  end

  private

  # Provided a block, this method will yield variations on the host with increasing 
  # specificity: 'foo.bar.example.com' will yield "example.com", "bar.example.com",
  # "foo.bar.example.com".
  def self.search_strings_for_url(url, &block)
    segments = url[/(?:https?\:\/\/)?([^\/]+)/,1].split('.') # extract the domain and split the subdomains
    candidate = [segments.pop]
    yield(candidate.unshift(segments.pop).join('.')) until segments.empty?
  end

end
{'fraction_non_alphanumeric': 0.12142591904938731, 'fraction_numerical': 0.0007426661715558856, 'mean_word_length': 4.073446327683616, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, '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': '308920', 'n_tokens_mistral': 893, 'n_tokens_neox': 866, 'n_words': 220}