Skip to content

Commit

Permalink
An emoji can print its own cheat sheet code
Browse files Browse the repository at this point in the history
  • Loading branch information
mwunsch committed Jan 31, 2013
1 parent 325970f commit ca7af06
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
6 changes: 4 additions & 2 deletions lib/rumoji.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,24 @@
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

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
Expand Down
6 changes: 5 additions & 1 deletion lib/rumoji/emoji.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions spec/rumoji/emoji_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit ca7af06

Please sign in to comment.