Class: EmojiReplace::Replacer

Inherits:
Object
  • Object
show all
Defined in:
lib/emoji_replace/replacer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Replacer

Returns a new instance of Replacer.



4
5
6
7
# File 'lib/emoji_replace/replacer.rb', line 4

def initialize(args)
  @text = args.fetch(:text).dup
  @index = Emoji::Index.new
end

Instance Attribute Details

#textObject (readonly)

Returns the value of attribute text.



2
3
4
# File 'lib/emoji_replace/replacer.rb', line 2

def text
  @text
end

Instance Method Details

#back(args = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/emoji_replace/replacer.rb', line 18

def back(args = {})
  @text.gsub!(/%\{emoji:(.+?)\}/) do |moji_str|
    match = moji_str.match(/%\{emoji:(.+?)\}/)
    moji_name = match[1]
    moji = @index.find_by_name(moji_name)

    if args[:html]
      "<img alt=\"#{moji.fetch("name")}\" class=\"emoji\" src=\"#{Emoji.image_url_for_name(moji_name)}\">"
    else
      moji.fetch("moji")
    end
  end

  return self
end

#replaceObject



9
10
11
12
13
14
15
16
# File 'lib/emoji_replace/replacer.rb', line 9

def replace
  @text.gsub!(@index.unicode_moji_regex) do |moji|
    moji = @index.find_by_moji(moji)
    "%{emoji:#{moji.fetch('name')}}"
  end

  return self
end