Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Twemoji configuration #15

Merged
merged 1 commit into from
May 19, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,20 @@ List of image attributes for the `img` tag. Optional.
=> /(:smile:|:laughing:| ... |:womens:|:x:|:zero:)/
```

## Configuration

`Twemoji.parse` options can be given in configure block:

```ruby
Twemoji.configure do |config|
config.asset_root = "https://twemoji.awesomecdn.com/"
config.file_ext = ".svg"
config.image_size = nil # only png need to set size
config.class_name = "twemoji"
config.img_attr = "style='height: 1.3em;'"
end
```

## Attribution Requirements

Please follow the [Attribution Requirements](https://github.com/twitter/twemoji#attribution-requirements) as stated on the official Twemoji (JS) repo.
Expand Down
11 changes: 6 additions & 5 deletions lib/twemoji.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require "twemoji/version"
require "twemoji/map"
require "twemoji/configuration"

# Twemoji is a Ruby implementation, parses your text, replace emoji text
# with corresponding emoji image. Default emoji images are from Twiiter CDN.
Expand Down Expand Up @@ -102,11 +103,11 @@ def self.render_unicode(text_or_code)
#
# @return [String] Original text with all occurrences of emoji text
# replaced by emoji image according to given options.
def self.parse(text, asset_root: "https://twemoji.maxcdn.com/",
file_ext: ".png",
image_size: "16x16",
class_name: "emoji",
img_attr: nil)
def self.parse(text, asset_root: Twemoji.configuration.asset_root,
file_ext: Twemoji.configuration.file_ext,
image_size: Twemoji.configuration.image_size,
class_name: Twemoji.configuration.class_name,
img_attr: Twemoji.configuration.img_attr)

options[:asset_root] = asset_root
options[:file_ext] = file_ext
Expand Down
25 changes: 25 additions & 0 deletions lib/twemoji/configuration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module Twemoji
def self.configuration
@configuration ||= Configuration.new
end

def self.configuration=(configuration)
@configuration = configuration
end

def self.configure
yield configuration
end

class Configuration
attr_accessor :asset_root, :file_ext, :image_size, :class_name, :img_attr

def initialize
@asset_root = "https://twemoji.maxcdn.com/".freeze
@file_ext = ".png".freeze
@image_size = "16x16".freeze
@class_name = "emoji".freeze
@img_attr = nil
end
end
end
57 changes: 57 additions & 0 deletions test/configuration_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
require "test_helper"

class TwemojiConfigurationTest < Minitest::Test
def teardown
Twemoji.configuration = nil
Twemoji.configure {}
end

def test_configuration_defaults
assert_equal "https://twemoji.maxcdn.com/", Twemoji.configuration.asset_root
assert_equal ".png", Twemoji.configuration.file_ext
assert_equal "16x16", Twemoji.configuration.image_size
assert_equal "emoji", Twemoji.configuration.class_name
assert_equal nil, Twemoji.configuration.img_attr
end

def test_configuration_asset_root
Twemoji.configure do |config|
config.asset_root = "https://twemoji.awesomecdn.com/"
end

assert_equal "https://twemoji.awesomecdn.com/", Twemoji.configuration.asset_root
end

def test_configuration_file_ext
Twemoji.configure do |config|
config.file_ext = ".svg"
end

assert_equal ".svg", Twemoji.configuration.file_ext
end

def test_configuration_image_size
Twemoji.configure do |config|
config.file_ext = ".png"
config.image_size = "36x36"
end

assert_equal "36x36", Twemoji.configuration.image_size
end

def test_configuration_class_name
Twemoji.configure do |config|
config.class_name = "twemoji"
end

assert_equal "twemoji", Twemoji.configuration.class_name
end

def test_configuration_img_attr
Twemoji.configure do |config|
config.img_attr = "style='height: 1.3em;'"
end

assert_equal "style='height: 1.3em;'", Twemoji.configuration.img_attr
end
end