Module: Slimmer::Headers

Defined in:
lib/slimmer/headers.rb

Constant Summary collapse

InvalidHeader =
Class.new(RuntimeError)
HEADER_PREFIX =
"X-Slimmer"
SLIMMER_HEADER_MAPPING =
{
  beta:                 "Beta",
  campaign_notification:"Campaign-Notification",
  format:               "Format",
  need_id:              "Need-ID",
  proposition:          "Proposition",
  organisations:        "Organisations",
  remove_meta_viewport: "Remove-Meta-Viewport",
  result_count:         "Result-Count",
  section:              "Section",
  skip:                 "Skip",
  template:             "Template",
}
ARTEFACT_HEADER =
"#{HEADER_PREFIX}-Artefact"
BETA_HEADER =
"#{HEADER_PREFIX}-#{SLIMMER_HEADER_MAPPING[:beta]}"
CAMPAIGN_NOTIFICATION =
"#{HEADER_PREFIX}-#{SLIMMER_HEADER_MAPPING[:campaign_notification]}"
FORMAT_HEADER =
"#{HEADER_PREFIX}-#{SLIMMER_HEADER_MAPPING[:format]}"
ORGANISATIONS_HEADER =
"#{HEADER_PREFIX}-#{SLIMMER_HEADER_MAPPING[:organisations]}"
REMOVE_META_VIEWPORT =
"#{HEADER_PREFIX}-#{SLIMMER_HEADER_MAPPING[:remove_meta_viewport]}"
RESULT_COUNT_HEADER =
"#{HEADER_PREFIX}-#{SLIMMER_HEADER_MAPPING[:result_count]}"
SEARCH_INDEX_HEADER =
"#{HEADER_PREFIX}-Search-Index"
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) ⇒ Object



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

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
  headers[ARTEFACT_HEADER] = artefact.to_json
end

#set_slimmer_dummy_artefact(details = {}) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/slimmer/headers.rb', line 49

def set_slimmer_dummy_artefact(details = {})
  artefact = {}
  artefact["title"] = details[:title] if details[:title]
  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"] = {"title" => details[:parent][:section_name],
                        "details" => {"type" => "section"},
                        "content_with_tag" => {"web_url" => details[:parent][:section_link]},
                      }
    end
    artefact["tags"] = [tag]
  end
  headers[ARTEFACT_HEADER] = artefact.to_json
end

#set_slimmer_headers(hash) ⇒ Object

Raises:



32
33
34
35
36
37
38
# File 'lib/slimmer/headers.rb', line 32

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