<commit_before>require 'gds_api/content_api'

module Indexer
  class TagLookup
    def prepare_tags(doc_hash)
      artefact = artefact_for_link(doc_hash["link"])
      if artefact.nil?
        return doc_hash
      end

      from_content_api = tags_from_artefact(artefact)
      doc_hash.merge(merge_tags(doc_hash, from_content_api))
    end

  private

    def artefact_for_link(link)
      if link.match(/\Ahttps?:\/\//)
        # We don't support tags for things which are external links.
        return nil
      end
      link = link.sub(/\A\//, '')
      begin
        content_api.artefact!(link)
      rescue GdsApi::HTTPNotFound, GdsApi::HTTPGone
        nil
      end
    end

    def tags_from_artefact(artefact)
      tags = Hash.new { [] }
      artefact.tags.each do |tag|
        slug = tag.slug
        type = tag.details.type
        case type
        when "organisation"
          tags["organisations"] <<= slug
        when "section"
          tags["mainstream_browse_pages"] <<= slug
        when "specialist_sector"
          tags["specialist_sectors"] <<= slug
        end
      end
      tags
    end

    def merge_tags(doc_hash, extra_tags)
      merged_tags = {}
      %w{specialist_sectors mainstream_browse_pages organisations}.each do |tag_type|
        merged_tags[tag_type] = doc_hash.fetch(tag_type, []).concat(extra_tags[tag_type]).uniq
      end
      merged_tags
    end

    def content_api
      @content_api ||= GdsApi::ContentApi.new(Plek.find("contentapi"))
    end
  end
end<commit_msg>Refactor TagLookup<commit_after>require 'gds_api/content_api'

module Indexer
  class TagLookup
    def prepare_tags(doc_hash)
      artefact = find_document_from_content_api(doc_hash["link"])
      return doc_hash unless artefact
      add_tags_from_artefact(artefact, doc_hash)
    end

  private

    def find_document_from_content_api(link)
      # We don't support tags for things which are external links.
      return if link.match(/\Ahttps?:\/\//)

      link_without_trailing_slash = link.sub(/\A\//, '')
      begin
        content_api.artefact!(link_without_trailing_slash)
      rescue GdsApi::HTTPNotFound, GdsApi::HTTPGone
        nil
      end
    end

    def add_tags_from_artefact(artefact, doc_hash)
      doc_hash["organisations"] ||= []
      doc_hash["mainstream_browse_pages"] ||= []
      doc_hash["specialist_sectors"] ||= []

      artefact.tags.each do |tag|
        case tag.details.type
        when "organisation"
          doc_hash["organisations"] << tag.slug
        when "section"
          doc_hash["mainstream_browse_pages"] << tag.slug
        when "specialist_sector"
          doc_hash["specialist_sectors"] << tag.slug
        end
      end

      doc_hash["organisations"].uniq!
      doc_hash["mainstream_browse_pages"].uniq!
      doc_hash["specialist_sectors"].uniq!

      doc_hash
    end

    def content_api
      @content_api ||= GdsApi::ContentApi.new(Plek.find("contentapi"))
    end
  end
end
{'fraction_non_alphanumeric': 0.1009421265141319, 'fraction_numerical': 0.0, 'mean_word_length': 2.6036363636363635, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 3, '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': '325546', 'n_tokens_mistral': 977, 'n_tokens_neox': 968, 'n_words': 179}