Module: Marfa::Helpers::HTTP::Vary

Defined in:
lib/marfa/helpers/http/vary.rb

Overview

Helpers to set the HTTP “‘Vary`” header.

Constant Summary collapse

VARY_HEADER =
'Vary'.freeze
VARY_UNSPECIFIED =
'*'.freeze

Instance Method Summary collapse

Instance Method Details

#vary!(hdr_name) ⇒ Object

Sets the HTTP “‘Vary`” header in Sinatra’s response ‘headers`.

Parameters:

  • hdr_name (String)

    The HTTP header name.

Returns:

  • The updated ‘headers`.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/marfa/helpers/http/vary.rb', line 19

def vary!(hdr_name)
  hdr_name = hdr_name.to_s if !hdr_name.kind_of?(::String)

  # Shortcut to avoid expensive splitting in the
  # simple "*" case:
  if hdr_name == VARY_UNSPECIFIED
    headers[VARY_HEADER] = VARY_UNSPECIFIED.dup unless (headers[VARY_HEADER] == VARY_UNSPECIFIED)
    # Normal operation:
  else
    vary_hdrs = headers[VARY_HEADER].to_s.split(/\s*(?:,\s*)+/)

    if vary_hdrs.include?(VARY_UNSPECIFIED)
      headers[VARY_HEADER] = VARY_UNSPECIFIED.dup
    else
      headers[VARY_HEADER] =
        vary_hdrs
          .push(hdr_name)
          .uniq(& :downcase)
          .join(',')
    end
  end

  headers[VARY_HEADER]
end