Class: GovukPublishingComponents::Presenters::HtmlPublicationSchema

Inherits:
FaqPageSchema
  • Object
show all
Defined in:
lib/govuk_publishing_components/presenters/machine_readable/html_publication_schema.rb

Instance Attribute Summary

Attributes inherited from FaqPageSchema

#page

Instance Method Summary collapse

Methods inherited from FaqPageSchema

#initialize

Constructor Details

This class inherits a constructor from GovukPublishingComponents::Presenters::FaqPageSchema

Instance Method Details

#content_between(start_xpath, stop_xpath = nil) ⇒ Object

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



59
60
61
62
63
64
65
66
67
68
# 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_occuranceObject



40
41
42
# 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_countsObject



10
11
12
13
14
15
# 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_pairsObject



44
45
46
# File 'lib/govuk_publishing_components/presenters/machine_readable/html_publication_schema.rb', line 44

def heading_pairs
  @heading_pairs ||= pairs_hash(headings)
end

#headingsObject



36
37
38
# 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?Boolean

Returns:

  • (Boolean)


17
18
19
# 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) ⇒ Object

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



50
51
52
53
54
55
# 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_answersObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 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_dataObject



4
5
6
7
8
# 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