Class: TeamApi::TagCanonicalizer

Inherits:
Object
  • Object
show all
Defined in:
lib/team_api/tag_canonicalizer.rb

Overview

Contains utility functions for canonicalizing names and the order of data.

Class Method Summary collapse

Class Method Details

.canonicalize_categories(site_data, tag_categories) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/team_api/tag_canonicalizer.rb', line 8

def self.canonicalize_categories(site_data, tag_categories)
  tag_categories.each do |category|
    xrefs = site_data[category]
    canonicalize_tag_category xrefs
    site_data['team'].values.each do |member|
      canonicalize_tags_for_item category, xrefs, member
    end
  end
end

.canonicalize_tag_category(tags_xrefs) ⇒ Object

Consolidate tags entries that are not exactly the same. Selects the lexicographically smaller version of the tag as a standard.

In the future, we may just consider raising an error if there are two different strings for the same thing.



23
24
25
26
27
28
29
# File 'lib/team_api/tag_canonicalizer.rb', line 23

def self.canonicalize_tag_category(tags_xrefs)
  return if tags_xrefs.nil? || tags_xrefs.empty?
  tags_xrefs.replace(LambdaMapReduce.map_reduce(
    tags_xrefs.values,
    ->(xref) { [[xref['slug'], xref]] },
    method(:consolidate_xrefs)).to_h)
end

.canonicalize_tags_for_item(category, xrefs, item) ⇒ Object



41
42
43
44
45
# File 'lib/team_api/tag_canonicalizer.rb', line 41

def self.canonicalize_tags_for_item(category, xrefs, item)
  return if item[category].nil?
  item[category].each { |tag| tag['name'] = xrefs[tag['slug']]['name'] }
    .sort_by! { |tag| tag['name'] }
end