class GovukTechDocs::TableOfContents::HeadingsBuilder

Public Class Methods

new(html, url) click to toggle source
# File lib/govuk_tech_docs/table_of_contents/headings_builder.rb, line 4
def initialize(html, url)
  @html = html
  @url = url
end

Public Instance Methods

headings() click to toggle source
# File lib/govuk_tech_docs/table_of_contents/headings_builder.rb, line 9
def headings
  heading_elements.map do |element|
    Heading.new(
      element_name: element.node_name,
      text: element.content,
      attributes: convert_nokogiri_attr_objects_to_hashes(element.attributes),
      page_url: @url
    )
  end
end

Private Instance Methods

convert_nokogiri_attr_objects_to_hashes(attributes) click to toggle source
# File lib/govuk_tech_docs/table_of_contents/headings_builder.rb, line 30
def convert_nokogiri_attr_objects_to_hashes(attributes)
  attributes.tap do |hash|
    hash.each do |k, v|
      hash[k] = v.value
    end
  end
end
heading_elements() click to toggle source
# File lib/govuk_tech_docs/table_of_contents/headings_builder.rb, line 26
def heading_elements
  page.search('h1, h2, h3, h4, h5, h6')
end
page() click to toggle source
# File lib/govuk_tech_docs/table_of_contents/headings_builder.rb, line 22
def page
  @_page ||= Nokogiri::HTML(@html)
end