<commit_before>require 'gds_api/rummager'

class SlugMigration < ActiveRecord::Base
  validates :slug, uniqueness: true
  validates :redirect_to, presence: true
  validate :is_not_already_completed

  before_save do |object|
    object.content_id = SecureRandom.uuid
  end

  def has_search_document?
    @search_client ||= GdsApi::Rummager.new(Plek.current.find('rummager'), disable_cache: true)
    begin
      @search_client.get_content!(slug)
    rescue GdsApi::HTTPNotFound => e
      false
    rescue GdsApi::HTTPServerError => e
      false
    end
  end

  def delete_search_document!
    @search_client ||= GdsApi::Rummager.new(Plek.current.find('rummager'), disable_cache: true)
    @search_client.delete_content!(slug)
  end

  private<commit_msg>Catch ancestor class of GdsApi errors, instead of catching subclasses<commit_after>require 'gds_api/rummager'

class SlugMigration < ActiveRecord::Base
  validates :slug, uniqueness: true
  validates :redirect_to, presence: true
  validate :is_not_already_completed

  before_save do |object|
    object.content_id = SecureRandom.uuid
  end

  def has_search_document?
    @search_client ||= GdsApi::Rummager.new(Plek.current.find('rummager'), disable_cache: true)
    begin
      @search_client.get_content!(slug)
    rescue GdsApi::HTTPErrorResponse => e
      false
    end
  end

  def delete_search_document!
    @search_client ||= GdsApi::Rummager.new(Plek.current.find('rummager'), disable_cache: true)
    @search_client.delete_content!(slug)
  end

  private
{'fraction_non_alphanumeric': 0.09639344262295083, 'fraction_numerical': 0.0, 'mean_word_length': 4.589743589743589, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, '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': '6584684', 'n_tokens_mistral': 512, 'n_tokens_neox': 508, 'n_words': 100}