Module: Slimmer::Headers
- Defined in:
- lib/slimmer/headers.rb
Constant Summary collapse
- InvalidHeader =
Class.new(RuntimeError)
- HEADER_PREFIX =
"X-Slimmer"
- SLIMMER_HEADER_MAPPING =
{ application_name: "Application-Name", beta: "Beta", campaign_notification:"Campaign-Notification", format: "Format", need_id: "Need-ID", page_owner: "Page-Owner", proposition: "Proposition", organisations: "Organisations", remove_meta_viewport: "Remove-Meta-Viewport", result_count: "Result-Count", section: "Section", skip: "Skip", template: "Template", }
- APPLICATION_NAME_HEADER =
"#{HEADER_PREFIX}-#{SLIMMER_HEADER_MAPPING[:application_name]}"
- 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]}"
- 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_INDEX_HEADER =
"#{HEADER_PREFIX}-Search-Index"
- SEARCH_PATH_HEADER =
"#{HEADER_PREFIX}-Search-Path"
- SKIP_HEADER =
"#{HEADER_PREFIX}-#{SLIMMER_HEADER_MAPPING[:skip]}"
- TEMPLATE_HEADER =
"#{HEADER_PREFIX}-#{SLIMMER_HEADER_MAPPING[:template]}"
Instance Method Summary collapse
- #set_slimmer_artefact(artefact_input) {|artefact| ... } ⇒ Object
- #set_slimmer_artefact_overriding_section(artefact_input, details = {}) ⇒ Object
- #set_slimmer_dummy_artefact(details = {}) ⇒ Object
- #set_slimmer_headers(hash) ⇒ Object
- #slimmer_section_tag_for_details(details) ⇒ Object
Instance Method Details
#set_slimmer_artefact(artefact_input) {|artefact| ... } ⇒ Object
45 46 47 48 49 50 51 52 53 |
# File 'lib/slimmer/headers.rb', line 45 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
55 56 57 58 59 60 61 |
# File 'lib/slimmer/headers.rb', line 55 def set_slimmer_artefact_overriding_section(artefact_input, details = {}) set_slimmer_artefact(artefact_input) do |artefact| if tag = slimmer_section_tag_for_details(details) artefact["tags"] = [tag] + (artefact["tags"] || []) end end end |
#set_slimmer_dummy_artefact(details = {}) ⇒ Object
63 64 65 66 67 68 69 70 |
# File 'lib/slimmer/headers.rb', line 63 def set_slimmer_dummy_artefact(details = {}) set_slimmer_artefact({}) do |artefact| artefact["title"] = details[:title] if details[:title] if tag = slimmer_section_tag_for_details(details) artefact["tags"] = [tag] end end end |
#set_slimmer_headers(hash) ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/slimmer/headers.rb', line 37 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
72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/slimmer/headers.rb', line 72 def slimmer_section_tag_for_details(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"] = slimmer_section_tag_for_details(details[:parent]) end tag end end |