From b823c6162b286f6a6d1b4a49bd3e3654268c81f3 Mon Sep 17 00:00:00 2001 From: Juanito Fatas Date: Tue, 19 May 2015 17:43:28 +0900 Subject: [PATCH] Implement Twemoji configuration --- README.md | 14 +++++++++ lib/twemoji.rb | 11 +++---- lib/twemoji/configuration.rb | 25 ++++++++++++++++ test/configuration_test.rb | 57 ++++++++++++++++++++++++++++++++++++ 4 files changed, 102 insertions(+), 5 deletions(-) create mode 100644 lib/twemoji/configuration.rb create mode 100644 test/configuration_test.rb diff --git a/README.md b/README.md index 5450a32..3cd1364 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/lib/twemoji.rb b/lib/twemoji.rb index 47f1d82..88e7d83 100644 --- a/lib/twemoji.rb +++ b/lib/twemoji.rb @@ -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. @@ -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 diff --git a/lib/twemoji/configuration.rb b/lib/twemoji/configuration.rb new file mode 100644 index 0000000..f079d9f --- /dev/null +++ b/lib/twemoji/configuration.rb @@ -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 diff --git a/test/configuration_test.rb b/test/configuration_test.rb new file mode 100644 index 0000000..ea0ee51 --- /dev/null +++ b/test/configuration_test.rb @@ -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