Class: TanukiEmoji::Db::Gemojione

Inherits:
Object
  • Object
show all
Defined in:
lib/tanuki_emoji/db/gemojione.rb

Overview

Gemojione Emoji database In order to maintain compatibility with alpha_codes that have been stored in a DB originally using the gemojione codes, we change the original gemojione code to be the primary and make the unicode version to be an alias. So instead of the alpha code being โ€˜thumbs_up` based on the unicode naming, itโ€™s โ€˜thumbsup`, with an alias of `thumbs_up`

Constant Summary collapse

DATA_FILE =
'vendor/gemojione/index-3.3.0.json'
EMOJI_DIFFERENCES =

rubocop:disable Style/AsciiComments These are specific gemojione whos alpha codes map slightly differently. For example, :cow: in gemojione is ๐Ÿฎ, while in Unicode it is ๐Ÿ„, which is :cow2: in gemojione. Now :cow_face: will give ๐Ÿฎ. See gitlab.com/gitlab-org/ruby/gems/tanuki_emoji/-/merge_requests/65#note_2113986561

{
  unicode: %w[๐Ÿ“… ๐Ÿช ๐Ÿˆ ๐Ÿ„ ๐Ÿ• ๐ŸŽ ๐Ÿ โœ๏ธ ๐Ÿ– ๐Ÿ‡ ๐Ÿ›ฐ๏ธ โ˜ƒ๏ธ ๐Ÿ… ๐Ÿš† โ˜‚๏ธ ๐Ÿ‹],
  gemojione: %w[๐Ÿ“† ๐Ÿซ ๐Ÿฑ ๐Ÿฎ ๐Ÿถ ๐Ÿด ๐Ÿญ ๐Ÿ“ ๐Ÿท ๐Ÿฐ ๐Ÿ“ก โ›„ ๐Ÿฏ ๐Ÿš‹ โ˜” ๐Ÿณ]
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(index:, data_file: self.class.data_file) ⇒ Gemojione

Returns a new instance of Gemojione.



34
35
36
37
# File 'lib/tanuki_emoji/db/gemojione.rb', line 34

def initialize(index:, data_file: self.class.data_file)
  @data_file = data_file
  @index = index
end

Instance Attribute Details

#data_fileObject (readonly)

Returns the value of attribute data_file.



32
33
34
# File 'lib/tanuki_emoji/db/gemojione.rb', line 32

def data_file
  @data_file
end

Class Method Details

.data_fileObject

rubocop:enable Style/AsciiComments



28
29
30
# File 'lib/tanuki_emoji/db/gemojione.rb', line 28

def self.data_file
  File.expand_path(File.join(__dir__, '../../../', DATA_FILE))
end

Instance Method Details

#load!Object



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
# File 'lib/tanuki_emoji/db/gemojione.rb', line 39

def load!
  db = File.open(data_file, 'r:UTF-8') do |file|
    JSON.parse(file.read, symbolize_names: true)
  end

  db.each_value do |emoji_data|
    emoji = @index.find_by_codepoints(emoji_data[:moji])

    # if it's not found, don't try to add something that isn't in the
    # Unicode set.
    next unless emoji

    if emoji.alpha_code != emoji_data[:shortname]
      org_alpha_code = emoji.alpha_code
      org_alpha_code_sym = TanukiEmoji::Character.format_name(org_alpha_code).to_sym
      emoji.replace_alpha_code(emoji_data[:shortname])

      # rubocop:disable Style/AsciiComments
      # Ensure that we're not adding an alias that is part of the gemonione data.
      # For example, Unicode uses `sunglasses` for ๐Ÿ•ถ๏ธ, which is `dark_sunglasses` in gemojione.
      # `sunglasses` is ๐Ÿ˜Ž which is `smiling_face_with_sunglasses` in Unicode.
      # We don't want `sunglasses` to be added as an alias of `dark_sunglasses`, because that
      # would interfere with `sunglasses` being the primary code for `smiling_face_with_sunglasses`
      # rubocop:enable Style/AsciiComments
      emoji.add_alias(org_alpha_code) unless db.key?(org_alpha_code_sym) || EMOJI_DIFFERENCES[:unicode].include?(emoji.codepoints)
    end

    add_emoji_data(emoji, emoji_data)

    @index.update(emoji)
  end
end