class GovukPublishingComponents::Presenters::FaqPageSchema
Constants
- QUESTION_TAG
we use H2 tags as the “question” and the html between them as the “answer”
Attributes
page[R]
Public Class Methods
new(page)
click to toggle source
# File lib/govuk_publishing_components/presenters/machine_readable/faq_page_schema.rb, line 6 def initialize(page) @page = page end
Public Instance Methods
structured_data()
click to toggle source
# File lib/govuk_publishing_components/presenters/machine_readable/faq_page_schema.rb, line 10 def structured_data # http://schema.org/FAQPage data = CreativeWorkSchema.new(@page).structured_data .merge(main_entity) data["@type"] = "FAQPage" data end
Private Instance Methods
answer_is_unset?(q_and_as, question)
click to toggle source
# File lib/govuk_publishing_components/presenters/machine_readable/faq_page_schema.rb, line 79 def answer_is_unset?(q_and_as, question) !q_and_as[question].key?(:answer) end
doc()
click to toggle source
# File lib/govuk_publishing_components/presenters/machine_readable/faq_page_schema.rb, line 71 def doc @doc ||= Nokogiri::HTML(page.body) end
main_entity()
click to toggle source
# File lib/govuk_publishing_components/presenters/machine_readable/faq_page_schema.rb, line 20 def main_entity { "mainEntity" => questions_and_answers_markup, } end
page_url()
click to toggle source
# File lib/govuk_publishing_components/presenters/machine_readable/faq_page_schema.rb, line 96 def page_url Plek.new.website_root + page.base_path end
question_and_answers()
click to toggle source
Generates a hash of questions and associated information:
-
question: the text in the h2 tag preceding other markup. Questions are
used to key the hash. The page title is set as the default, as there is often a preamble in guides before any h2 is set.
-
:answer: the markup that is not an h2 tag. It is associated with the
preceding h2 header.
-
:anchor: the id of the h2 (autogenerated by the markdown converter).
This is used to build links directly to the section in question
# File lib/govuk_publishing_components/presenters/machine_readable/faq_page_schema.rb, line 54 def question_and_answers question = page.title doc.xpath("html/body").children.each_with_object({}) do |element, q_and_as| if question_element?(element) question = element.text q_and_as[question] = { anchor: element["id"] } elsif question_hash_is_unset?(q_and_as, question) q_and_as[question] = { answer: element.to_s } elsif answer_is_unset?(q_and_as, question) q_and_as[question][:answer] = element.to_s else q_and_as[question][:answer] << element.to_s end end end
question_element?(element)
click to toggle source
# File lib/govuk_publishing_components/presenters/machine_readable/faq_page_schema.rb, line 86 def question_element?(element) element.name == QUESTION_TAG end
question_hash_is_unset?(q_and_as, question)
click to toggle source
# File lib/govuk_publishing_components/presenters/machine_readable/faq_page_schema.rb, line 75 def question_hash_is_unset?(q_and_as, question) q_and_as[question].nil? end
questions_and_answers_markup()
click to toggle source
# File lib/govuk_publishing_components/presenters/machine_readable/faq_page_schema.rb, line 26 def questions_and_answers_markup question_and_answers.select { |_, value| value[:answer].present? } .map do |question, value| q_and_a_url = section_url(value[:anchor]) { "@type" => "Question", "name" => question, "url" => q_and_a_url, "acceptedAnswer" => { "@type" => "Answer", "url" => q_and_a_url, "text" => value[:answer], }, } end end
section_url(anchor)
click to toggle source
# File lib/govuk_publishing_components/presenters/machine_readable/faq_page_schema.rb, line 90 def section_url(anchor) return "#{page_url}##{anchor}" if anchor.present? page_url end