<commit_before>module Api::ActionNetwork::Taggings
  extend Api::ActionNetwork::Import

  def self.resource
    'tagging'
  end

  def self.import!(tag_identifier, tag_name, group)
    next_uri = first_uri(tag_id: tag_identifier)

    logger.info "Api::ActionNetwork::Taggings#import! from #{next_uri}"

    while next_uri
      taggings, next_uri = request_resources_from_action_network(next_uri, group)

      taggings.each { |tagging| associate_person(tagging, tag_name, group) }
    end
  end

  def self.first_uri(params={})
    uri = "https://actionnetwork.org/api/v2/tags/#{params[:tag_id]}/taggings"

    add_uri_filter(uri, params[:synced_at])
  end

  def self.associate_person(tagging, tag_name, group)
    person = find_or_import_person(person_identifier(tagging), group)
    person.tag_list.push(tag_name)
    person.save
  end

  def self.find_or_import_person(person_uid, group)
    Person.any_identifier("action_network:#{person_uid}").first ||
      Api::ActionNetwork::Person.import!(person_uid, group)<commit_msg>dont die when person is nil<commit_after>module Api::ActionNetwork::Taggings
  extend Api::ActionNetwork::Import

  def self.resource
    'tagging'
  end

  def self.import!(tag_identifier, tag_name, group)
    next_uri = first_uri(tag_id: tag_identifier)

    logger.info "Api::ActionNetwork::Taggings#import! from #{next_uri}"

    while next_uri
      taggings, next_uri = request_resources_from_action_network(next_uri, group)

      taggings.each { |tagging| associate_person(tagging, tag_name, group) }
    end
  end

  def self.first_uri(params={})
    uri = "https://actionnetwork.org/api/v2/tags/#{params[:tag_id]}/taggings"

    add_uri_filter(uri, params[:synced_at])
  end

  def self.associate_person(tagging, tag_name, group)
    person = find_or_import_person(person_identifier(tagging), group)
    person.tag_list.push(tag_name) unless person.nil?
    #person.save #not needed because tag push saves
  end

  def self.find_or_import_person(person_uid, group)
    Person.any_identifier("action_network:#{person_uid}").first ||
      Api::ActionNetwork::Person.import!(person_uid, group)
{'fraction_non_alphanumeric': 0.11533052039381153, 'fraction_numerical': 0.0009376465072667605, 'mean_word_length': 4.911357340720222, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '1547805', 'n_tokens_mistral': 726, 'n_tokens_neox': 708, 'n_words': 122}