Commit e7b6c0d9 by Mark Wunsch

Read emoji/codes in and out of IO

parent 8e8e7652
# -*- encoding: utf-8 -*-
require "rumoji/version"
require 'stringio'
module Rumoji
extend self
......@@ -20,6 +21,27 @@ module Rumoji
duplicate
end
def encode_io(input, output=StringIO.new(""))
input.each_codepoint do |codepoint|
emoji = codepoint.to_s(16).upcase
emoji_or_character = EMOJI_NAME_TO_CODEPOINT.has_value?(emoji) ? ":#{EMOJI_NAME_TO_CODEPOINT.key(emoji)}:" : [codepoint].pack("U")
output.write emoji_or_character
end
output.rewind
output
end
def decode_io(input, output=StringIO.new(""))
input.each do |word|
EMOJI_NAME_TO_CODEPOINT.each_pair do |key,value|
word.gsub!(":#{key}:", [value.to_i(16)].pack("U"))
end
output.write(word)
end
output.rewind
output
end
EMOJI_NAME_TO_CODEPOINT = {
# PEOPLE
smile: "1F604",
......
......@@ -5,20 +5,34 @@ require 'minitest/autorun'
describe Rumoji do
before do
@emoji_map = Hash[Rumoji::EMOJI_NAME_TO_CODEPOINT.map {|k,v| [k, [v.to_i(16)].pack("U")] }]
@poop = "💩"
@smile = "😄"
end
describe "#encode" do
it "transforms emoji into cheat-sheet form" do
key = :smile
Rumoji.encode("#{@emoji_map[key]}").must_equal ":#{key}:"
Rumoji.encode(@smile).must_equal ":smile:"
end
end
describe "#decode" do
it "transforms a cheat-sheet code into an emoji" do
poop_emoji = "💩"
Rumoji.decode(":poop:").must_equal "#{poop_emoji}"
Rumoji.decode(":poop:").must_equal @poop
end
end
describe "#encode_io" do
it "reads emoji from one stream and outputs a stream of cheat-sheet codes" do
io = StringIO.new("#{@smile}")
Rumoji.encode_io(io).read.must_equal ":smile:"
end
end
describe "#decode_io" do
it "reads a cheat-sheet code from one stream and outputs a stream of emoji" do
io = StringIO.new(":poop:")
Rumoji.decode_io(io).read.must_equal @poop
end
end
end
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment