Sha256: e3f53dce70a236b3b6bae2fb221a0830fa5fda4bcefd83379ba253ca507d5b4b

Contents?: true

Size: 1.01 KB

Versions: 1

Compression:

Stored size: 1.01 KB

Contents

require 'emojimmy/version'

module Emojimmy
  # Load emoji data from config/emoji.txt and build the `text_to_emoji`
  # and `emoji_to_text` hash tables
  def self.initialize!
    emoji = File.read(File.expand_path('../../data/emoji.txt', __FILE__)).each_line.to_a

    @text_to_emoji = emoji.inject({}) do |memo, item|
      item = item.chomp.split("\t")
      memo.merge "{#{item[0]}}" => eval('"' + item[1] + '"')
    end

    @emoji_to_text = emoji.inject({}) do |memo, item|
      item = item.chomp.split("\t")
      memo.merge eval('"' + item[1] + '"') => "{#{item[0]}}"
    end
  end

  # Loop through all emoji and replace them with
  # their matching text equivalent
  def self.emoji_to_text(content)
    @emoji_to_text.each_pair do |emoji, text|
      content = content.gsub(emoji, text)
    end

    content
  end

  # Loop through each {U+...} part in the string and
  # convert it to the matching emoji
  def self.text_to_emoji(content)
    content.gsub /({U\+[^}]+})/ do |data|
      @text_to_emoji[data]
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
emojimmy-0.1 lib/emojimmy.rb