<commit_before>class RegistrationsController < ApplicationController
  respond_to :json

  def create
    structure = normalise_names(JSON.parse(params[:resource]))
    
    artefact = find_or_build_artefact(structure)

    if artefact.save!
      Rummageable.index(rummageable_params(structure))
    end

    respond_with artefact
  end

  protected
  def find_or_build_artefact(structure)
    Artefact.find_or_initialize_by_slug(structure[:slug]).tap do |artefact|
      artefact.attributes = structure.slice(*Artefact.attribute_names)
    end
  end

  # A translation layer because we have inconsistent language.
  # TODO: Make language consistent so this isn't needed any more
  def normalise_names(structure)
    structure["name"] ||= structure["title"]
    structure["kind"] ||= structure["format"]
    structure["kind"] = 'smart-answer' if structure["kind"] == 'smart_answer'

    structure
  end

  def rummageable_params(structure)
    structure.slice(Rummageable::VALID_KEYS)
  end
end
<commit_msg>Add a little documentation for RegistrationsController#create<commit_after>class RegistrationsController < ApplicationController
  respond_to :json

  # We're presuming that all registrations are for published items
  # that should be stored in panopticon and registered in search.
  # Items are matched on 'slug' and new details will replace any
  # previous item with that slug
  def create
    structure = normalise_names(JSON.parse(params[:resource]))
    
    artefact = find_or_build_artefact(structure)

    if artefact.save!
      Rummageable.index(rummageable_params(structure))
    end

    respond_with artefact
  end

  protected
  def find_or_build_artefact(structure)
    Artefact.find_or_initialize_by_slug(structure[:slug]).tap do |artefact|
      artefact.attributes = structure.slice(*Artefact.attribute_names)
    end
  end

  # A translation layer because we have inconsistent language.
  # TODO: Make language consistent so this isn't needed any more
  def normalise_names(structure)
    structure["name"] ||= structure["title"]
    structure["kind"] ||= structure["format"]
    structure["kind"] = 'smart-answer' if structure["kind"] == 'smart_answer'

    structure
  end

  def rummageable_params(structure)
    structure.slice(Rummageable::VALID_KEYS)
  end
end

{'fraction_non_alphanumeric': 0.08583877995642701, 'fraction_numerical': 0.0, 'mean_word_length': 4.519230769230769, 'pattern_counts': {'":': 0, '<': 5, '<?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': '7048223', 'n_tokens_mistral': 675, 'n_tokens_neox': 662, 'n_words': 168}