Class: EnvStyle::Favicon

Inherits:
Object
  • Object
show all
Defined in:
lib/env_style/favicon.rb

Overview

Ties the pieces together: resolves the source and environment, decides the output format, and returns the bytes to serve — tinted for non-production environments, verbatim otherwise.

Tinting happens on demand. The result is deterministic, so the strong ETag lets browsers and proxies cache it and revalidate with a 304 — there is no server-side cache to keep coherent across workers.

Constant Summary collapse

SVG =
:svg
PNG =
:png
CONTENT_TYPES =
{
  SVG => SvgTinter::CONTENT_TYPE,
  PNG => RasterTinter::CONTENT_TYPE
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(config: EnvStyle.config, environment: Environment.new(config: config), root: Source.rails_root) ⇒ Favicon

Returns a new instance of Favicon.



21
22
23
24
25
26
# File 'lib/env_style/favicon.rb', line 21

def initialize(config: EnvStyle.config, environment: Environment.new(config: config),
               root: Source.rails_root)
  @config = config
  @environment = environment
  @source = Source.new(config: config, root: root, env_name: environment.name)
end

Instance Method Details

#available?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/env_style/favicon.rb', line 28

def available?
  @source.found?
end

#bytesObject

Bytes to serve: tinted when the environment calls for it, otherwise the untouched source (zero image work).



67
68
69
70
71
# File 'lib/env_style/favicon.rb', line 67

def bytes
  return @bytes if defined?(@bytes)

  @bytes = tinted? ? render : source_bytes
end

#content_typeObject

The content type of the bytes actually served: the tint-output type when tinted, otherwise the source's own media type. A non-PNG raster served untouched (say a real favicon.ico) is labeled with its true type rather than the .png route it happens to travel.



45
46
47
# File 'lib/env_style/favicon.rb', line 45

def content_type
  tinted? ? CONTENT_TYPES.fetch(format) : source_content_type
end

#etagObject

A strong validator for the served bytes. exclude_colors and its matching tolerance are folded in for raster output: both change which pixels are preserved.



61
62
63
# File 'lib/env_style/favicon.rb', line 61

def etag
  @etag ||= Digest::SHA256.hexdigest(etag_inputs.join("|"))
end

#formatObject

The format of the bytes actually served. An untouched favicon streams the source verbatim, so its format (and content type) follow the source — never the tint-output format, which would mislabel the bytes.



35
36
37
38
39
# File 'lib/env_style/favicon.rb', line 35

def format
  return @format if defined?(@format)

  @format = tinted? ? tinted_format : source_format
end

#source_bytesObject

The untouched source bytes, used both for passthrough and as the graceful fallback when tinting fails.



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

def source_bytes
  @source.read
end

#source_content_typeObject

The media type of the untouched source, used on the degradation path so source bytes are never mislabeled with the tint-output type.



51
52
53
# File 'lib/env_style/favicon.rb', line 51

def source_content_type
  @source.content_type
end

#tinted?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/env_style/favicon.rb', line 55

def tinted?
  @environment.tint?
end