Module: Slimmer::Headers

Defined in:
lib/slimmer/headers.rb

Constant Summary collapse

InvalidHeader =
Class.new(RuntimeError)
HEADER_PREFIX =
"X-Slimmer"
SLIMMER_HEADER_MAPPING =
{
  alpha_label:          "Alpha-Label",
  application_name:     "Application-Name",
  beta:                 "Beta",
  beta_label:           "Beta-Label",
  format:               "Format",
  page_owner:           "Page-Owner",
  proposition:          "Proposition",
  organisations:        "Organisations",
  world_locations:      "World-Locations",
  remove_meta_viewport: "Remove-Meta-Viewport",
  result_count:         "Result-Count",
  search_parameters:    "Search-Parameters",
  section:              "Section",
  skip:                 "Skip",
  template:             "Template",
}
ALPHA_LABEL =
"#{HEADER_PREFIX}-#{SLIMMER_HEADER_MAPPING[:alpha_label]}"
APPLICATION_NAME_HEADER =
"#{HEADER_PREFIX}-#{SLIMMER_HEADER_MAPPING[:application_name]}"
ARTEFACT_HEADER =
"#{HEADER_PREFIX}-Artefact"
BETA_HEADER =
"#{HEADER_PREFIX}-#{SLIMMER_HEADER_MAPPING[:beta]}"
BETA_LABEL =
"#{HEADER_PREFIX}-#{SLIMMER_HEADER_MAPPING[:beta_label]}"
FORMAT_HEADER =
"#{HEADER_PREFIX}-#{SLIMMER_HEADER_MAPPING[:format]}"
ORGANISATIONS_HEADER =
"#{HEADER_PREFIX}-#{SLIMMER_HEADER_MAPPING[:organisations]}"
WORLD_LOCATIONS_HEADER =
"#{HEADER_PREFIX}-#{SLIMMER_HEADER_MAPPING[:world_locations]}"
PAGE_OWNER_HEADER =
"#{HEADER_PREFIX}-#{SLIMMER_HEADER_MAPPING[:page_owner]}"
REMOVE_META_VIEWPORT =
"#{HEADER_PREFIX}-#{SLIMMER_HEADER_MAPPING[:remove_meta_viewport]}"
RESULT_COUNT_HEADER =
"#{HEADER_PREFIX}-#{SLIMMER_HEADER_MAPPING[:result_count]}"
SEARCH_PATH_HEADER =
"#{HEADER_PREFIX}-Search-Path"
SEARCH_PARAMETERS_HEADER =
"#{HEADER_PREFIX}-#{SLIMMER_HEADER_MAPPING[:search_parameters]}"
SKIP_HEADER =
"#{HEADER_PREFIX}-#{SLIMMER_HEADER_MAPPING[:skip]}"
TEMPLATE_HEADER =
"#{HEADER_PREFIX}-#{SLIMMER_HEADER_MAPPING[:template]}"

Instance Method Summary collapse

Instance Method Details

#set_slimmer_artefact(artefact_input) {|artefact| ... } ⇒ Object

Yields:

  • (artefact)


49
50
51
52
53
54
55
56
57
# File 'lib/slimmer/headers.rb', line 49

def set_slimmer_artefact(artefact_input)
  if artefact_input.is_a?(Hash) or artefact_input.is_a?(OpenStruct)
    artefact = artefact_input.dup
  elsif artefact_input.respond_to?(:to_hash)
    artefact = artefact_input.to_hash
  end
  yield artefact if block_given?
  headers[ARTEFACT_HEADER] = artefact.to_json
end

#set_slimmer_artefact_overriding_section(artefact_input, details = {}) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/slimmer/headers.rb', line 59

def set_slimmer_artefact_overriding_section(artefact_input, details = {})
  set_slimmer_artefact(artefact_input) do |artefact|
    if tag = (details)
      artefact["tags"] = [tag] + (artefact["tags"] || [])
    end
  end
end

#set_slimmer_dummy_artefact(details = {}) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/slimmer/headers.rb', line 67

def set_slimmer_dummy_artefact(details = {})
  set_slimmer_artefact({}) do |artefact|
    artefact["title"] = details[:title] if details[:title]
    if tag = (details)
      artefact["tags"] = [tag]
    end
  end
end

#set_slimmer_headers(hash) ⇒ Object

Raises:



41
42
43
44
45
46
47
# File 'lib/slimmer/headers.rb', line 41

def set_slimmer_headers(hash)
  raise InvalidHeader if (hash.keys - SLIMMER_HEADER_MAPPING.keys).any?
  SLIMMER_HEADER_MAPPING.each do |hash_key, header_suffix|
    value = hash[hash_key]
    headers["#{HEADER_PREFIX}-#{header_suffix}"] = value.to_s if value
  end
end

#slimmer_section_tag_for_details(details) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/slimmer/headers.rb', line 76

def (details)
  if details[:section_name] and details[:section_link]
    tag = {
      "title" => details[:section_name],
      "details" => {"type" => "section"},
      "content_with_tag" => {"web_url" => details[:section_link]},
    }
    if details[:parent]
      tag["parent"] = (details[:parent])
    end
    tag
  end
end