module Emoji

Constants

FEMALE_SYMBOL
MALE_SYMBOL
TEXT_GLYPHS

Chars from Apple's palette which must have VARIATION_SELECTOR_16 to render:

VARIATION_SELECTOR_16
ZERO_WIDTH_JOINER

Public Instance Methods

all() click to toggle source
# File lib/emoji.rb, line 20
def all
  return @all if defined? @all
  @all = []
  parse_data_file
  @all
end
apple_palette() click to toggle source
# File lib/emoji.rb, line 27
def apple_palette
  return @apple_palette if defined? @apple_palette
  data = File.open(apple_palette_file, 'r:UTF-8') { |f| JSON.parse(f.read) }
  @apple_palette = data.fetch('EmojiDataArray').each_with_object({}) do |group, all|
    title = group.fetch('CVDataTitle').split('-', 2)[1]
    all[title] = group.fetch('CVCategoryData').fetch('Data').split(',').map do |raw|
      TEXT_GLYPHS.include?(raw) ? raw + VARIATION_SELECTOR_16 : raw
    end
  end
end
apple_palette_file() click to toggle source
# File lib/emoji.rb, line 12
def apple_palette_file
  File.expand_path('../../db/Category-Emoji.json', __FILE__)
end
create(name) { |emoji| ... } click to toggle source

Public: Initialize an Emoji::Character instance and yield it to the block. The character is added to the `Emoji.all` set.

# File lib/emoji.rb, line 40
def create(name)
  emoji = Emoji::Character.new(name)
  self.all << edit_emoji(emoji) { yield emoji if block_given? }
  emoji
end
data_file() click to toggle source
# File lib/emoji.rb, line 8
def data_file
  File.expand_path('../../db/emoji.json', __FILE__)
end
edit_emoji(emoji) { |emoji| ... } click to toggle source

Public: Yield an emoji to the block and update the indices in case its aliases or unicode_aliases lists changed.

# File lib/emoji.rb, line 48
def edit_emoji(emoji)
  @names_index ||= Hash.new
  @unicodes_index ||= Hash.new

  yield emoji

  emoji.aliases.each do |name|
    @names_index[name] = emoji
  end
  emoji.unicode_aliases.each do |unicode|
    @unicodes_index[unicode] = emoji
  end

  emoji
end
find_by_alias(name) click to toggle source

Public: Find an emoji by its aliased name. Return nil if missing.

# File lib/emoji.rb, line 65
def find_by_alias(name)
  names_index[name]
end
find_by_unicode(unicode) click to toggle source

Public: Find an emoji by its unicode character. Return nil if missing.

# File lib/emoji.rb, line 70
def find_by_unicode(unicode)
  unicodes_index[unicode]
end
images_path() click to toggle source
# File lib/emoji.rb, line 16
def images_path
  File.expand_path("../../images", __FILE__)
end

Private Instance Methods

names_index() click to toggle source
# File lib/emoji.rb, line 119
def names_index
  all unless defined? @all
  @names_index
end
parse_data_file() click to toggle source
# File lib/emoji.rb, line 83
def parse_data_file
  data = File.open(data_file, 'r:UTF-8') { |file| JSON.parse(file.read) }
  data.each do |raw_emoji|
    self.create(nil) do |emoji|
      raw_emoji.fetch('aliases').each { |name| emoji.add_alias(name) }
      if raw = raw_emoji['emoji']
        unicodes = [raw, raw.sub(VARIATION_SELECTOR_16, '') + VARIATION_SELECTOR_16].uniq
        unicodes.each { |uni| emoji.add_unicode_alias(uni) }
      end
      raw_emoji.fetch('tags').each { |tag| emoji.add_tag(tag) }

      emoji.category = raw_emoji['category']
      emoji.description = raw_emoji['description']
      emoji.unicode_version = raw_emoji['unicode_version']
      emoji.ios_version = raw_emoji['ios_version']
    end
  end

  # Add an explicit gendered variant to emoji that historically imply a gender
  data.each do |raw_emoji|
    raw = raw_emoji['emoji']
    next unless raw
    no_gender = raw.sub(/(#{VARIATION_SELECTOR_16})?#{ZERO_WIDTH_JOINER}(#{FEMALE_SYMBOL}|#{MALE_SYMBOL})/, '')
    next unless $2
    emoji = find_by_unicode(no_gender)
    next unless emoji
    edit_emoji(emoji) do
      emoji.add_unicode_alias(
        $2 == FEMALE_SYMBOL ?
          raw.sub(FEMALE_SYMBOL, MALE_SYMBOL) :
          raw.sub(MALE_SYMBOL, FEMALE_SYMBOL)
      )
    end
  end
end
unicodes_index() click to toggle source
# File lib/emoji.rb, line 124
def unicodes_index
  all unless defined? @all
  @unicodes_index
end