Class: PageStructuredData::Breadcrumbs

Inherits:
Object
  • Object
show all
Defined in:
app/src/page_structured_data/breadcrumbs.rb

Overview

Basic page metadata for any page

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hierarchy: []) ⇒ Breadcrumbs

Returns a new instance of Breadcrumbs.



8
9
10
# File 'app/src/page_structured_data/breadcrumbs.rb', line 8

def initialize(hierarchy: [])
  @hierarchy = hierarchy
end

Instance Attribute Details

#hierarchyObject (readonly)

Returns the value of attribute hierarchy.



6
7
8
# File 'app/src/page_structured_data/breadcrumbs.rb', line 6

def hierarchy
  @hierarchy
end

Instance Method Details

#json_ld(current_page_title:) ⇒ Object

rubocop:disable Metrics/MethodLength



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/src/page_structured_data/breadcrumbs.rb', line 16

def json_ld(current_page_title:) # rubocop:disable Metrics/MethodLength
  node = {
    '@context': 'https://schema.org',
    '@type': 'BreadcrumbList',
    itemListElement: [],
  }

  items = []
  count = 0
  @hierarchy.each do |page|
    items << {
      '@type': 'ListItem',
      position: (count += 1),
      name: page[:title],
      item: page[:href],
    }
  end

  items << {
    '@type': 'ListItem',
    position: (count += 1),
    name: current_page_title,
  }

  node['itemListElement'] = items

  %(
  <script type="application/ld+json">
    #{node.to_json}
    </script>
  )
end

#titlesObject



12
13
14
# File 'app/src/page_structured_data/breadcrumbs.rb', line 12

def titles
  hierarchy.pluck(:title)
end