2017-07-21 02:27:40 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'singleton'
|
|
|
|
|
|
|
|
class Emoji
|
|
|
|
include Singleton
|
|
|
|
|
|
|
|
def initialize
|
2017-07-31 13:19:13 +00:00
|
|
|
data = Oj.load(File.open(Rails.root.join('lib', 'assets', 'emoji.json')))
|
2017-07-21 02:27:40 +00:00
|
|
|
|
|
|
|
@map = {}
|
|
|
|
|
|
|
|
data.each do |_, emoji|
|
|
|
|
keys = [emoji['shortname']] + emoji['aliases']
|
|
|
|
unicode = codepoint_to_unicode(emoji['unicode'])
|
|
|
|
|
|
|
|
keys.each do |key|
|
|
|
|
@map[key] = unicode
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def unicode(shortcode)
|
|
|
|
@map[shortcode]
|
|
|
|
end
|
|
|
|
|
|
|
|
def names
|
|
|
|
@map.keys
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def codepoint_to_unicode(codepoint)
|
|
|
|
if codepoint.include?('-')
|
2017-07-26 23:02:15 +00:00
|
|
|
codepoint.split('-').map(&:hex).pack('U*')
|
2017-07-21 02:27:40 +00:00
|
|
|
else
|
|
|
|
[codepoint.hex].pack('U')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|