Class: EmojiDatasource::Emoji

Inherits:
Object
  • Object
show all
Defined in:
lib/emoji_datasource/emoji.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, variation: nil) ⇒ Emoji

Returns a new instance of Emoji.



7
8
9
10
11
# File 'lib/emoji_datasource/emoji.rb', line 7

def initialize(data, variation: nil)
  @data = data
  @variation = variation
  @unified = variation ? @data.dig('skin_variations', variation, 'unified') : @data['unified']
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *arguments, &block) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/emoji_datasource/emoji.rb', line 69

def method_missing(method_name, *arguments, &block)
  if @data.key?(method_name.to_s)
    @data[method_name.to_s]
  else
    super
  end
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



5
6
7
# File 'lib/emoji_datasource/emoji.rb', line 5

def data
  @data
end

#unifiedObject (readonly)

Returns the value of attribute unified.



5
6
7
# File 'lib/emoji_datasource/emoji.rb', line 5

def unified
  @unified
end

Instance Method Details

#as_jsonObject



91
92
93
# File 'lib/emoji_datasource/emoji.rb', line 91

def as_json
  @data
end

#baseObject

Base emoji, without variations applied. E.g. for ‘:+1::skin-tone-2:` base will be just `:+1:`



63
64
65
66
67
# File 'lib/emoji_datasource/emoji.rb', line 63

def base
  return self unless @variation

  EmojiDatasource.find_by_unified(@data['unified'])
end

#inspectObject



83
84
85
# File 'lib/emoji_datasource/emoji.rb', line 83

def inspect
  "#{self.class.name}: :#{short_name}: (#{to_char})"
end

#nameObject

Official emoji name (pretty long)



19
20
21
22
23
# File 'lib/emoji_datasource/emoji.rb', line 19

def name
  return @data['name'] unless @variation

  "#{@data['name']} (#{variation_emojis.map(&:name).join(', ')})"
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
80
81
# File 'lib/emoji_datasource/emoji.rb', line 77

def respond_to_missing?(method_name, include_private = false)
  return true if @data.key?(method_name.to_s)

  super
end

#short_nameObject

Short code that often can be used to find emoji in pickers and chat apps



26
27
28
29
30
# File 'lib/emoji_datasource/emoji.rb', line 26

def short_name
  return @data['short_name'] unless @variation

  "#{@data['short_name']}::#{variation_emojis.map(&:short_name).join('::')}"
end

#short_namesObject

All known short names (base short_name and maybe some aliases)



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/emoji_datasource/emoji.rb', line 33

def short_names
  return @data['short_names'] unless @variation

  @short_names ||= @data['short_names'].flat_map do |short_name|
    full_short_name = "#{short_name}::#{variation_emojis.map(&:short_name).join('::')}"

    # Allow to find complex emojis with multiple equal skin tone codes by single skin tone
    # For some emojis (like :women_holding_hands:) there are special single skin tone emojis.
    # but for others (like :people_holding_hands:) there are no such versions.
    # However, some implementations in the wild (e.g. Slack and EmojiMart) understand codes
    # containing only single skin tone (e.g. `:people_holding_hands::skin-tone-N:`)
    if variation_emojis.size > 1 && variation_emojis.uniq.size == 1
      abbr_short_name = "#{short_name}::#{variation_emojis.first.short_name}"
    end

    [full_short_name, abbr_short_name].compact
  end
end

#to_charObject

Raw emoji string



14
15
16
# File 'lib/emoji_datasource/emoji.rb', line 14

def to_char
  EmojiDatasource.unified_to_char(unified)
end

#to_json(**args) ⇒ Object



95
96
97
# File 'lib/emoji_datasource/emoji.rb', line 95

def to_json(**args)
  @data.to_json(**args)
end

#to_sObject



87
88
89
# File 'lib/emoji_datasource/emoji.rb', line 87

def to_s
  to_char
end

#variationsObject

All known skin tone variations



53
54
55
56
57
58
59
# File 'lib/emoji_datasource/emoji.rb', line 53

def variations
  return @variations = [] if @variation

  @variations ||= @data.fetch('skin_variations', {}).each_key.map do |key|
    self.class.new(data, variation: key)
  end
end