Skip to content

Commit

Permalink
Merge pull request #11 from wpeterson/feature/string_helper
Browse files Browse the repository at this point in the history
Add opt-in String helper methods
  • Loading branch information
steveklabnik committed Aug 13, 2013
2 parents 0a7257d + 77df60a commit 797464c
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
20 changes: 20 additions & 0 deletions README.md
Expand Up @@ -59,7 +59,27 @@ Default configuration integrates with Rails, but you can change it with an initi
# config/initializers/emoji.rb
Emoji.asset_host = "emoji.cdn.com"
Emoji.asset_path = '/assets/emoji'

String Helper Methods:

You can also

include 'emoji/string_ext'

and call methods directly on your string to return the same results:

> 'I ❤ Emoji'.with_emoji_images
=> "I <img class=\"emoji\" src=\"http://localhost:3000/assets/emoji/heart.png\"> Emoji"

> 'heart'.image_url
> '❤'.image_url
=> "http://localhost:3000/assets/emoji.heart.png"

> 'heart'.emoji_data
> '❤'.emoji_data
=> {"moji"=>"❤", "name"=>"heart", "name-ja"=>"ハート", "category"=>"abstract", "unicode"=>"2764"}


## HTML Safety and Performance

This gem uses pure ruby code for compatibility with different Ruby virtual machines. However, there can be significant performance gains to escaping incoming HTML strings using optimized, native code in the `escape_utils` gem.
Expand Down
14 changes: 14 additions & 0 deletions lib/emoji/string_ext.rb
@@ -0,0 +1,14 @@
class String
def with_emoji_images
Emoji.replace_unicode_moji_with_images(self)
end

def image_url
Emoji.image_url_for_name(self.emoji_data['name'])
end

def emoji_data
index = Emoji.index
index.find_by_moji(self) || index.find_by_name(self)
end
end
29 changes: 29 additions & 0 deletions test/string_ext_test.rb
@@ -0,0 +1,29 @@
# encoding: UTF-8

require 'emoji/string_ext'

describe String, 'with Emoji extensions' do
describe '#with_emoji_images' do
it 'should replace unicode moji with an img tag' do
base_string = "I ❤ Emoji"
replaced_string = base_string.with_emoji_images
assert_equal "I <img class=\"emoji\" src=\"http://localhost:3000/heart.png\"> Emoji", replaced_string
end
end

describe '#image_url' do
it 'should generate image_url' do
assert_equal 'http://localhost:3000/cyclone.png', '🌀'.image_url
assert_equal 'http://localhost:3000/cyclone.png', 'cyclone'.image_url
end
end

describe '#emoji_data' do
it 'should find data for a name or a moji' do
data_from_moji = '❤'.emoji_data
data_from_string = 'heart'.emoji_data

assert_equal data_from_moji, data_from_string
end
end
end

0 comments on commit 797464c

Please sign in to comment.