summaryrefslogtreecommitdiff
path: root/lib/award_emoji.rb
blob: 4fc3443ac681381049d887b79d8df426239d079f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
class AwardEmoji
  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

  def self.normilize_emoji_name(name)
    aliases[name] || name
  end

  def self.emoji_by_category
    unless @emoji_by_category
      @emoji_by_category = {}

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

        @emoji_by_category[data["category"]] ||= []
        @emoji_by_category[data["category"]] << data
      end

      @emoji_by_category = @emoji_by_category.sort.to_h
    end

    @emoji_by_category
  end

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

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

  # 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
end