Module: Rumoji

Extended by:
Rumoji
Included in:
Rumoji
Defined in:
lib/rumoji.rb,
lib/rumoji/emoji.rb,
lib/rumoji/version.rb,
lib/rumoji/emoji/nature.rb,
lib/rumoji/emoji/people.rb,
lib/rumoji/emoji/places.rb,
lib/rumoji/emoji/objects.rb,
lib/rumoji/emoji/symbols.rb

Defined Under Namespace

Classes: Emoji

Constant Summary collapse

VERSION =
"0.4.1"

Instance Method Summary collapse

Instance Method Details

#decode(str) ⇒ Object

Transform a cheat-sheet code into an Emoji



16
17
18
# File 'lib/rumoji.rb', line 16

def decode(str)
  str.gsub(/:([^s:]?[\w-]+):/) {|sym| (Emoji.find($1.intern) || sym).to_s }
end

#decode_io(readable, writeable = StringIO.new("")) ⇒ Object



57
58
59
60
61
62
# File 'lib/rumoji.rb', line 57

def decode_io(readable, writeable=StringIO.new(""))
  readable.each_line do |line|
    writeable.write decode(line)
  end
  writeable
end

#encode(str) ⇒ Object

Transform emoji into its cheat-sheet code



10
11
12
13
# File 'lib/rumoji.rb', line 10

def encode(str)
  io = StringIO.new(str)
  encode_io(io).string
end

#encode_io(readable, writeable = StringIO.new("")) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rumoji.rb', line 20

def encode_io(readable, writeable=StringIO.new(""))
  previous_codepoints = []

  readable.each_codepoint do |codepoint|
    possible_emoji = Emoji.select_by_codepoint(codepoint)

    sequence =  if possible_emoji.empty?
                  # If this codepoint is not part of an emoji
                  # just write it back out, along with any previously stored codepoints.
                  dump_codepoints_to_string(previous_codepoints << codepoint)
                elsif !possible_emoji.any?(&:multiple?)
                  # If this codepoint is part of an emoji, but not one
                  # that contains multiple codepoints, write out the
                  # first possiblity's cheat code and the previously stored codepoints
                  dump_codepoints_to_string(previous_codepoints) << possible_emoji.first.code
                else
                  # This codepoint is a part of an emoji with multiple
                  # codepoints, so push it onto the stack.
                  previous_codepoints.push(codepoint)
                  # Check and see if this completes the emoji, otherwise,
                  # write out an empty string.
                  if found = possible_emoji.find {|e| e.codepoints.eql?(previous_codepoints) }
                    previous_codepoints.clear and found.code
                  else
                    ""
                  end
                end

    writeable.write sequence
  end

  # Write any remaining codepoints.
  writeable.write dump_codepoints_to_string(previous_codepoints) if previous_codepoints.any?

  writeable
end