Module: Emojimmy

Defined in:
lib/emojimmy.rb,
lib/emojimmy/mixin.rb,
lib/emojimmy/version.rb

Defined Under Namespace

Modules: Mixin

Constant Summary collapse

DATA_FILE =
File.expand_path('../../data/emoji.txt', __FILE__)
TOKEN_REGEXP =
/({U\+[^}]+})/
VERSION =
'0.1.3'

Class Method Summary collapse

Class Method Details

.emoji_to_token(content) ⇒ Object

Loop through all emoji and replace them with their matching token



24
25
26
27
28
29
30
31
32
# File 'lib/emojimmy.rb', line 24

def self.emoji_to_token(content)
  return content unless content.present?

  content.dup.tap do |content|
    @emoji_to_token.each_pair do |emoji, token|
      content.gsub!(emoji, token)
    end
  end
end

.initialize!Object

Load emoji data from config/emoji.txt and build the ‘token_to_emoji` and `emoji_to_token` hash tables



17
18
19
20
# File 'lib/emojimmy.rb', line 17

def self.initialize!
  content = File.read(DATA_FILE).each_line.to_a
  build_hash_tables(content)
end

.token_to_emoji(content) ⇒ Object

Loop through each U+… token in the string and convert it to the matching emoji



36
37
38
39
40
# File 'lib/emojimmy.rb', line 36

def self.token_to_emoji(content)
  return content unless content.present?

  content.gsub(TOKEN_REGEXP) { |data| @token_to_emoji[data] }
end