-
Notifications
You must be signed in to change notification settings - Fork 0
/
emoji_machine.rb
40 lines (32 loc) · 866 Bytes
/
emoji_machine.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# frozen_string_literal: true
class EmojiMachine
attr_accessor :figlet, :text_input
FIGLET_BODY_DEFAULT = ':smile:'
FIGLET_BACKGROUND_DEFAULT = ':cloud:'
REGEX = /:[\w-]+:/
def initialize(text)
font = Figlet::Font.new(font_path('3x5'))
@figlet = Figlet::Typesetter.new(font)
@text_input = text
end
def generate_text
figlet[text]
.gsub!('#', emoji[0] || FIGLET_BODY_DEFAULT)
.gsub!(' ', emoji[1] || FIGLET_BACKGROUND_DEFAULT)
end
def text
text_input.dup.tap do |t|
# keep the text only by removing all emoji
t.gsub!(REGEX, '')
t.squish!
end
end
def emoji
# the first emoji given becomes text body, the second one background
text_input.scan(REGEX).flatten
end
private
def font_path(font_name)
File.join(File.dirname(__FILE__), 'fonts', "#{font_name}.flf")
end
end