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



24
25
26
27
28
29
# File 'app/src/page_structured_data/breadcrumbs.rb', line 24

def json_ld(current_page_title:)
  node = to_h(current_page_title: current_page_title)
  return if node[:itemListElement].size < 2

  JsonLd.script_tag(node)
end

#titlesObject



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

def titles
  hierarchy.pluck(:title)
end

#to_h(current_page_title:) ⇒ Object

rubocop:disable Metrics/MethodLength



16
17
18
19
20
21
22
# File 'app/src/page_structured_data/breadcrumbs.rb', line 16

def to_h(current_page_title:) # rubocop:disable Metrics/MethodLength
  {
    '@context': 'https://schema.org',
    '@type': 'BreadcrumbList',
    'itemListElement': item_list_elements(current_page_title: current_page_title),
  }
end