Module: Emoji

Defined in:
lib/emoji.rb,
lib/emoji/index.rb,
lib/emoji/railtie.rb,
lib/emoji/version.rb

Defined Under Namespace

Classes: Index, Railtie

Constant Summary collapse

VERSION =
"1.0.2"

Class Method Summary collapse

Class Method Details

.alt_tag_for_moji(moji) ⇒ Object



88
89
90
91
92
# File 'lib/emoji.rb', line 88

def self.alt_tag_for_moji(moji)
  return moji unless use_plaintext_alt_tags
  emoji = index.find_by_moji(moji)
  emoji['name']
end

.asset_hostObject



22
23
24
# File 'lib/emoji.rb', line 22

def self.asset_host
  @asset_host || 'http://localhost:3000'
end

.asset_host=(asset_host) ⇒ Object



26
27
28
# File 'lib/emoji.rb', line 26

def self.asset_host=(asset_host)
  @asset_host = parse_and_validate_asset_host(asset_host)
end

.asset_pathObject



63
64
65
# File 'lib/emoji.rb', line 63

def self.asset_path
  @asset_path || '/'
end

.asset_path=(path) ⇒ Object



67
68
69
# File 'lib/emoji.rb', line 67

def self.asset_path=(path)
  @asset_path = path
end

.escape_html(string) ⇒ Object



117
118
119
# File 'lib/emoji.rb', line 117

def self.escape_html(string)
  @escaper.escape_html(string)
end

.extract_port_string(uri) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/emoji.rb', line 55

def self.extract_port_string(uri)
  return nil unless uri.port
  return nil if uri.port == 80 && uri.scheme == 'http'
  return nil if uri.port == 443 && uri.scheme == 'https'

  return ":#{uri.port}"
end

.extract_uri_scheme_string(asset_host_spec, uri) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/emoji.rb', line 43

def self.extract_uri_scheme_string(asset_host_spec, uri)
  # Special Case for Protocol Relative Scheme: //hostname.com/
  if asset_host_spec.size >= 2 && asset_host_spec[0..1] == '//'
    return '//'
  end

  # Extract Protocol from asset_host_spec URI or default to HTTP
  scheme = uri.scheme || 'http'

  "#{ scheme }://"
end

.image_url_for_name(name) ⇒ Object



79
80
81
# File 'lib/emoji.rb', line 79

def self.image_url_for_name(name)
  "#{asset_host}#{ File.join(asset_path, name) }.png"
end

.image_url_for_unicode_moji(moji) ⇒ Object



83
84
85
86
# File 'lib/emoji.rb', line 83

def self.image_url_for_unicode_moji(moji)
  emoji = index.find_by_moji(moji)
  image_url_for_name(emoji['name'])
end

.indexObject



121
122
123
# File 'lib/emoji.rb', line 121

def self.index
  @index ||= Index.new
end

.parse_and_validate_asset_host(asset_host_spec) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/emoji.rb', line 30

def self.parse_and_validate_asset_host(asset_host_spec)
  begin
    uri = URI.parse(asset_host_spec)
    scheme_string = extract_uri_scheme_string(asset_host_spec, uri)
    hostname = uri.hostname || uri.path
    port_string = extract_port_string(uri)
    
    return "#{ scheme_string }#{ hostname }#{ port_string }"
  rescue URI::InvalidURIError
    raise 'Invalid Emoji.asset_host, should be a hostname or URL prefix'
  end
end

.replace_unicode_moji_with_images(string) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/emoji.rb', line 94

def self.replace_unicode_moji_with_images(string)
  return string unless string
  unless string.match(index.unicode_moji_regex)
    return safe_string(string)
  end

  safe_string = safe_string(string.dup)
  safe_string.gsub!(index.unicode_moji_regex) do |moji|
    %Q{<img alt="#{alt_tag_for_moji(moji)}" class="emoji" src="#{ image_url_for_unicode_moji(moji) }">}
  end
  safe_string = safe_string.html_safe if safe_string.respond_to?(:html_safe)

  safe_string
end

.safe_string(string) ⇒ Object



109
110
111
112
113
114
115
# File 'lib/emoji.rb', line 109

def self.safe_string(string)
  if string.respond_to?(:html_safe?) && string.html_safe?
    string
  else
    escape_html(string)
  end
end

.use_plaintext_alt_tagsObject



71
72
73
# File 'lib/emoji.rb', line 71

def self.use_plaintext_alt_tags
  @use_plaintext_alt_tags || false
end

.use_plaintext_alt_tags=(bool) ⇒ Object



75
76
77
# File 'lib/emoji.rb', line 75

def self.use_plaintext_alt_tags=(bool)
  @use_plaintext_alt_tags = bool
end