-
Notifications
You must be signed in to change notification settings - Fork 7
/
headers.rb
65 lines (59 loc) · 2.31 KB
/
headers.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
module Slimmer
module Headers
InvalidHeader = Class.new(RuntimeError)
HEADER_PREFIX = "X-Slimmer"
SLIMMER_HEADER_MAPPING = {
campaign_notification:"Campaign-Notification",
format: "Format",
need_id: "Need-ID",
proposition: "Proposition",
remove_meta_viewport: "Remove-Meta-Viewport",
result_count: "Result-Count",
section: "Section",
skip: "Skip",
template: "Template",
}
ARTEFACT_HEADER = "#{HEADER_PREFIX}-Artefact"
FORMAT_HEADER = "#{HEADER_PREFIX}-Format"
REMOVE_META_VIEWPORT = "#{HEADER_PREFIX}-Remove-Meta-Viewport"
RESULT_COUNT_HEADER = "#{HEADER_PREFIX}-Result-Count"
SEARCH_INDEX_HEADER = "#{HEADER_PREFIX}-Search-Index"
SEARCH_PATH_HEADER = "#{HEADER_PREFIX}-Search-Path"
SKIP_HEADER = "#{HEADER_PREFIX}-Skip"
TEMPLATE_HEADER = "#{HEADER_PREFIX}-Template"
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
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
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
end
end