Class: Agave::Utils::FaviconTagsBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/agave/utils/favicon_tags_builder.rb

Constant Summary collapse

APPLE_TOUCH_ICON_SIZES =
[57, 60, 72, 76, 114, 120, 144, 152, 180].freeze
ICON_SIZES =
[16, 32, 96, 192].freeze
WINDOWS_SIZES =
[[70, 70], [150, 150], [310, 310], [310, 150]].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site, theme_color) ⇒ FaviconTagsBuilder

Returns a new instance of FaviconTagsBuilder.



11
12
13
14
# File 'lib/agave/utils/favicon_tags_builder.rb', line 11

def initialize(site, theme_color)
  @site = site
  @theme_color = theme_color
end

Instance Attribute Details

#siteObject (readonly)

Returns the value of attribute site.



5
6
7
# File 'lib/agave/utils/favicon_tags_builder.rb', line 5

def site
  @site
end

#theme_colorObject (readonly)

Returns the value of attribute theme_color.



5
6
7
# File 'lib/agave/utils/favicon_tags_builder.rb', line 5

def theme_color
  @theme_color
end

Instance Method Details

#build_app_name_tagObject



59
60
61
# File 'lib/agave/utils/favicon_tags_builder.rb', line 59

def build_app_name_tag
  meta_tag('application-name', site.name)
end

#build_apple_icon_tagsObject



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/agave/utils/favicon_tags_builder.rb', line 26

def build_apple_icon_tags
  return unless site.favicon

  APPLE_TOUCH_ICON_SIZES.map do |size|
    link_tag(
      'apple-touch-icon',
      url(size),
      sizes: "#{size}x#{size}"
    )
  end
end

#build_color_tagsObject



63
64
65
66
67
68
69
70
# File 'lib/agave/utils/favicon_tags_builder.rb', line 63

def build_color_tags
  return unless theme_color

  [
    meta_tag('theme-color', theme_color),
    meta_tag('msapplication-TileColor', theme_color)
  ]
end

#build_icon_tagsObject



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/agave/utils/favicon_tags_builder.rb', line 38

def build_icon_tags
  return unless site.favicon

  ICON_SIZES.map do |size|
    link_tag(
      'icon',
      url(size),
      sizes: "#{size}x#{size}",
      type: "image/#{site.favicon.format}"
    )
  end
end

#build_windows_tagsObject



51
52
53
54
55
56
57
# File 'lib/agave/utils/favicon_tags_builder.rb', line 51

def build_windows_tags
  return unless site.favicon

  WINDOWS_SIZES.map do |(w, h)|
    meta_tag("msapplication-square#{w}x#{h}logo", url(w, h))
  end
end


80
81
82
# File 'lib/agave/utils/favicon_tags_builder.rb', line 80

def link_tag(rel, href, attrs = {})
  { tag_name: 'link', attributes: attrs.merge(rel: rel, href: href) }
end

#meta_tag(name, value) ⇒ Object



76
77
78
# File 'lib/agave/utils/favicon_tags_builder.rb', line 76

def meta_tag(name, value)
  { tag_name: 'meta', attributes: { name: name, content: value } }
end

#meta_tagsObject



16
17
18
19
20
21
22
23
24
# File 'lib/agave/utils/favicon_tags_builder.rb', line 16

def meta_tags
  [
    build_icon_tags,
    build_apple_icon_tags,
    build_windows_tags,
    build_color_tags,
    build_app_name_tag
  ].flatten.compact
end

#url(width, height = width) ⇒ Object



72
73
74
# File 'lib/agave/utils/favicon_tags_builder.rb', line 72

def url(width, height = width)
  site.favicon.url(w: width, h: height)
end