diff --git a/lib/rumoji.rb b/lib/rumoji.rb index 2911040..f95751b 100644 --- a/lib/rumoji.rb +++ b/lib/rumoji.rb @@ -6,14 +6,16 @@ module Rumoji extend self + # Transform emoji into its cheat-sheet code def encode(str) remapped_codepoints = str.codepoints.flat_map do |codepoint| emoji = Emoji.find_by_codepoint(codepoint) - emoji ? ":#{emoji.code}:".codepoints.entries : codepoint + emoji ? emoji.code.codepoints.entries : codepoint end remapped_codepoints.pack("U*") end + # Transform a cheat-sheet code into an Emoji def decode(str) str.gsub(/:(\S?\w+):/) {|sym| Emoji.find($1.intern).to_s } end @@ -21,7 +23,7 @@ def decode(str) def encode_io(readable, writeable=StringIO.new("")) readable.each_codepoint do |codepoint| emoji = Emoji.find_by_codepoint(codepoint) - emoji_or_character = emoji ? ":#{emoji.code}:" : [codepoint].pack("U") + emoji_or_character = emoji ? emoji.code : [codepoint].pack("U") writeable.write emoji_or_character end writeable diff --git a/lib/rumoji/emoji.rb b/lib/rumoji/emoji.rb index 71d479e..5e8db3c 100644 --- a/lib/rumoji/emoji.rb +++ b/lib/rumoji/emoji.rb @@ -10,10 +10,14 @@ def initialize(string, symbols, name = nil) @name = name || @cheat_codes.first.to_s.upcase.gsub("_", " ") end - def code + def symbol @cheat_codes.first end + def code + ":#{symbol}:" + end + def include?(symbol) @cheat_codes.include? symbol.to_sym end diff --git a/spec/rumoji/emoji_spec.rb b/spec/rumoji/emoji_spec.rb index 2798baa..5811f91 100644 --- a/spec/rumoji/emoji_spec.rb +++ b/spec/rumoji/emoji_spec.rb @@ -13,7 +13,7 @@ end it("has a name") { subject.name.must_equal name } - it("has a cheat sheet code") { symbols.must_include subject.code } + it("has a cheat sheet code") { symbols.must_include subject.code[1...-1].intern } it("can test if it includes a cheat sheet code") { symbols.all?{|symbol| subject.include?(symbol) }.must_equal true } it("converts to the emoji string") { subject.to_s.must_equal poo_string } it("converts to a hex code") { subject.hex.must_equal "1F4A9" } @@ -51,7 +51,8 @@ Rumoji::Emoji.new(us_string, symbol, name) end - it("has one code") { subject.code.must_equal symbol } + it("has one symbol, representing the code") { subject.symbol.must_equal symbol } + it("has one cheat sheet code") { subject.code[1...-1].intern.must_equal symbol } it("includes the symbol") { subject.must_include symbol } it("transforms to the correct string") { subject.to_s.must_equal us_string } end @@ -66,12 +67,12 @@ end it "finds an emoji from a string" do - subject.find_by_string(smile_str).code.must_equal smile_sym + subject.find_by_string(smile_str).symbol.must_equal smile_sym end it "finds an emoji from a codepoint" do smile_str.codepoints.map do |point| - subject.find_by_codepoint(point).code.must_equal smile_sym + subject.find_by_codepoint(point).symbol.must_equal smile_sym end end end