emoji.rb 687 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. # frozen_string_literal: true
  2. require 'singleton'
  3. class Emoji
  4. include Singleton
  5. def initialize
  6. data = Oj.load(File.open(Rails.root.join('lib', 'assets', 'emoji.json')))
  7. @map = {}
  8. data.each do |_, emoji|
  9. keys = [emoji['shortname']] + emoji['aliases']
  10. unicode = codepoint_to_unicode(emoji['unicode'])
  11. keys.each do |key|
  12. @map[key] = unicode
  13. end
  14. end
  15. end
  16. def unicode(shortcode)
  17. @map[shortcode]
  18. end
  19. def names
  20. @map.keys
  21. end
  22. private
  23. def codepoint_to_unicode(codepoint)
  24. if codepoint.include?('-')
  25. codepoint.split('-').map(&:hex).pack('U*')
  26. else
  27. [codepoint.hex].pack('U')
  28. end
  29. end
  30. end