module Ballast::Emoji::Utils

General utility methods.

@attribute url_mapper

@return [Proc] The current URL mapper or a default one (which will return the relative URL unmodified).

Attributes

url_mapper[RW]

Public Instance Methods

enumerate(keys_method: :markup, values_method: :html, **options) click to toggle source

Lists all the emoji known in a hash.

@param keys_method [Symbol] The method to use for keys. @param values_method [Symbol] The method to use for values. @param options [Hash] The options to pass to all methods. @return [Hash] A hash of all known emojis.

# File lib/ballast/emoji.rb, line 40
def enumerate(keys_method: :markup, values_method: :html, **options)
  tester = ::Emoji::Character.new(nil)
  keys_method = :markup unless keys_method && tester.respond_to?(keys_method)
  values_method = :html unless values_method && tester.respond_to?(values_method)

  ::Emoji.all.reduce({}) do |accu, icon|
    accu[invoke(icon, keys_method, options)] = invoke(icon, values_method, options)
    accu
  end
end
replace(text, mode: :html, **options) click to toggle source

Replaces all the emojis in the text using the requested mod.

@param text [String] The text to manipulate. @param mode [Symbol] The method to use when replacing icons. @param options [Hash] The options to pass to the replacing method. @return [String] The text with all emojis replaced.

# File lib/ballast/emoji.rb, line 29
def replace(text, mode: :html, **options)
  mode = :markup unless mode && ::Emoji::Character.new(nil).respond_to?(mode)
  text.ensure_string.gsub(replace_regex) { invoke(::Emoji.find_by_unicode(Regexp.last_match[1]), mode, options) }
end
replace_regex() click to toggle source

Returns the regular expression which matches all the known emojis.

@return [Regexp] The regular expression which matches all the known emojis.

# File lib/ballast/emoji.rb, line 19
def replace_regex
  @replace_regex ||= /(#{::Emoji.send(:unicodes_index).keys.join("|")})/
end
url_for(image) click to toggle source

Returns a absolute URL for a emoji image.

@param image [String] The relative URL of the emoji filename. @return [String] The absolute URL of the emoji filename.

# File lib/ballast/emoji.rb, line 62
def url_for(image)
  url_mapper.call(image)
end