award_emoji.rb 2.05 KB
Newer Older
Valery Sizov's avatar
Valery Sizov committed
1
class AwardEmoji
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
  CATEGORIES = {
    other: "Other",
    objects: "Objects",
    places: "Places",
    travel_places: "Travel",
    emoticons: "Emoticons",
    objects_symbols: "Symbols",
    nature: "Nature",
    celebration: "Celebration",
    people: "People",
    activity: "Activity",
    flags: "Flags",
    food_drink: "Food"
  }.with_indifferent_access

Valery Sizov's avatar
Valery Sizov committed
17 18 19 20 21 22
  CATEGORY_ALIASES = {
    symbols: "objects_symbols",
    foods: "food_drink",
    travel: "travel_places"
  }.with_indifferent_access

Valery Sizov's avatar
Valery Sizov committed
23
  def self.normilize_emoji_name(name)
24
    aliases[name] || name
Valery Sizov's avatar
Valery Sizov committed
25
  end
26 27 28

  def self.emoji_by_category
    unless @emoji_by_category
Valery Sizov's avatar
Valery Sizov committed
29
      @emoji_by_category = Hash.new { |h, key| h[key] = [] }
30

31 32
      emojis.each do |emoji_name, data|
        data["name"] = emoji_name
33

Valery Sizov's avatar
Valery Sizov committed
34 35 36 37 38 39
        # Skip Fitzpatrick(tone) modifiers
        next if data["category"] == "modifier"

        category = CATEGORY_ALIASES[data["category"]] || data["category"]

        @emoji_by_category[category] << data
40 41 42 43 44 45 46
      end

      @emoji_by_category = @emoji_by_category.sort.to_h
    end

    @emoji_by_category
  end
47 48 49 50 51 52 53 54

  def self.emojis
    @emojis ||= begin
      json_path = File.join(Rails.root, 'fixtures', 'emojis', 'index.json' )
      JSON.parse(File.read(json_path))
    end
  end

55
  def self.unicode
56
    @unicode ||= emojis.map {|key, value| { key => emojis[key]["unicode"] } }.inject(:merge!)
57 58
  end

59 60 61 62 63 64
  def self.aliases
    @aliases ||= begin
      json_path = File.join(Rails.root, 'fixtures', 'emojis', 'aliases.json' )
      JSON.parse(File.read(json_path))
    end
  end
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83

  # Returns an Array of Emoji names and their asset URLs.
  def self.urls
    @urls ||= begin
      path = File.join(Rails.root, 'fixtures', 'emojis', 'digests.json')
      prefix = Gitlab::Application.config.assets.prefix
      digest = Gitlab::Application.config.assets.digest

      JSON.parse(File.read(path)).map do |hash|
        if digest
          fname = "#{hash['unicode']}-#{hash['digest']}"
        else
          fname = hash['unicode']
        end

        { name: hash['name'], path: "#{prefix}/#{fname}.png" }
      end
    end
  end
Valery Sizov's avatar
Valery Sizov committed
84
end