A Ruby gem. For emoji. For everyone. ❤️
This gem exposes the Phantom Open Emoji library unicode/image assets and APIs for working with them.
Easily lookup emoji name, unicode character, or image assets and convert emoji representations.
Add this line to your application's Gemfile:
gem 'emoji'
And then execute:
$ bundle
Or install it yourself as:
$ gem install emoji
Install emoji image library assets:
$ rake emoji:install_assets
====================================================================
= emoji image assets install
= Target: /Users/user/src/rails-app/app/assets/images/emoji
= Source: /Users/user/src/emoji/assets/images
====================================================================
- Creating /Users/user/src/rails-app/app/assets/images/emoji...
- Installing assets...
You can use this gem to replace unicode emoji characters with img tags linking to the appropriate emoji image.
Image Replacement APIs:
> Emoji.replace_unicode_moji_with_images('I ❤ Emoji')
=> "I <img alt=\"❤\" class=\"emoji\" src=\"http://localhost:3000/assets/emoji/heart.png\"> Emoji"
> Emoji.image_url_for_unicode_moji('❤')
=> "http://localhost:3000/assets/emoji/heart.png"
> Emoji.image_url_for_name('heart')
=> "http://localhost:3000/assets/emoji/heart.png"
Emoji Library Index APIs:
> index = Emoji::Index.new
> index.find_by_name('heart')
=> {"moji"=>"❤", "name"=>"heart", "name-ja"=>"ハート", "category"=>"abstract", "unicode"=>"2764"}
> index.find_by_moji('❤')
=> {"moji"=>"❤", "name"=>"heart", "name-ja"=>"ハート", "category"=>"abstract", "unicode"=>"2764"}
> index.find_by_unicode('2764')
=> {"moji"=>"❤", "name"=>"heart", "name-ja"=>"ハート", "category"=>"abstract", "unicode"=>"2764"}
Default configuration integrates with Rails, but you can change it with an initializer:
# config/initializers/emoji.rb
Emoji.asset_host = "emoji.cdn.com"
Emoji.asset_path = '/assets/emoji'
Emoji.use_plaintext_alt_tags = true
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 alt=\"❤\" 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"}
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.
The emoji gem will try to use escape_utils
if it's available, but does not require it. Benchmarks show a 10x-100x improvement in HTML escaping performance, based on the size of the string being processed.
To enable native HTML escaping, add this line to your application's Gemfile:
gem 'escape_utils'
- @ryan-orr: Granted the official
emoji
rubygems account - @mikowitz:
String
ext helpers - @semanticart: Cleanup/Ruby 1.9.3 support
- @parndt: README doc fixes
- @neuegram: XSS Security Audit
- @tumes: Plaintext Emoji Alt Tags
- Fork it
- Bundle Install (
bundle install
) - Run the Tests (
rake test
) - Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request