class GovukPublishingComponents::Presenters::HtmlPublicationSchema

Public Instance Methods

content_between(start_xpath, stop_xpath = nil) click to toggle source

From: stackoverflow.com/a/7816046/1014251 If `stop_xpath` is `nil` gets text to end of content

# File lib/govuk_publishing_components/presenters/machine_readable/html_publication_schema.rb, line 59
def content_between(start_xpath, stop_xpath = nil)
  node = doc.at_xpath(start_xpath).next_element
  stop = stop_xpath && doc.at_xpath(stop_xpath)
  [].tap do |content|
    while node && node != stop
      content << node
      node = node.next_element
    end
  end
end
first_heading_type_with_more_than_one_occurance() click to toggle source
# File lib/govuk_publishing_components/presenters/machine_readable/html_publication_schema.rb, line 40
def first_heading_type_with_more_than_one_occurance
  heading_counts.detect { |_k, v| v > 1 }.first
end
heading_counts() click to toggle source
# File lib/govuk_publishing_components/presenters/machine_readable/html_publication_schema.rb, line 10
def heading_counts
  @heading_counts ||= (1..5).each_with_object({}) do |n, hash|
    heading = "h#{n}"
    hash[heading] = doc.xpath("//*[@class=\"govspeak\"]//#{heading}").count
  end
end
heading_pairs() click to toggle source
# File lib/govuk_publishing_components/presenters/machine_readable/html_publication_schema.rb, line 44
def heading_pairs
  @heading_pairs ||= pairs_hash(headings)
end
headings() click to toggle source
# File lib/govuk_publishing_components/presenters/machine_readable/html_publication_schema.rb, line 36
def headings
  @headings ||= doc.xpath("//*[@class=\"govspeak\"]//#{first_heading_type_with_more_than_one_occurance}")
end
less_than_two_headings_of_any_one_type?() click to toggle source
# File lib/govuk_publishing_components/presenters/machine_readable/html_publication_schema.rb, line 17
def less_than_two_headings_of_any_one_type?
  heading_counts.values.max < 2
end
pairs_hash(array) click to toggle source

Converts [:a, :b, :c] into {:a => :b, :b => :c}

# File lib/govuk_publishing_components/presenters/machine_readable/html_publication_schema.rb, line 50
def pairs_hash(array)
  all_but_last = array[0..-2]
  all_but_first = array[1..]
  pairs = [all_but_last, all_but_first].transpose
  Hash[pairs]
end
question_and_answers() click to toggle source
# File lib/govuk_publishing_components/presenters/machine_readable/html_publication_schema.rb, line 21
def question_and_answers
  headings.each_with_object({}) do |heading, hash|
    question = heading.text

    next_heading = heading_pairs[heading]
    next_heading_path = next_heading && next_heading.path
    answer = content_between(heading.path, next_heading_path)

    hash[question] = {
      answer: answer.map(&:to_s).join,
      anchor: heading.attr(:id),
    }
  end
end
structured_data() click to toggle source
# File lib/govuk_publishing_components/presenters/machine_readable/html_publication_schema.rb, line 4
def structured_data
  return ArticleSchema.new(page).structured_data if less_than_two_headings_of_any_one_type?

  super
end