Class: PageStructuredData::PageTypes::BlogPosting

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

Overview

Basic page metadata for any page

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(headline:, published_at:, updated_at:, images: [], authors: []) ⇒ BlogPosting

Returns a new instance of BlogPosting.



9
10
11
12
13
14
15
# File 'app/src/page_structured_data/page_types/blog_posting.rb', line 9

def initialize(headline:, published_at:, updated_at:, images: [], authors: [])
  @headline = headline
  @images = images
  @published_at = published_at
  @updated_at = updated_at
  @authors = authors
end

Instance Attribute Details

#authorsObject (readonly)

Returns the value of attribute authors.



7
8
9
# File 'app/src/page_structured_data/page_types/blog_posting.rb', line 7

def authors
  @authors
end

#headlineObject (readonly)

Returns the value of attribute headline.



7
8
9
# File 'app/src/page_structured_data/page_types/blog_posting.rb', line 7

def headline
  @headline
end

#imagesObject (readonly)

Returns the value of attribute images.



7
8
9
# File 'app/src/page_structured_data/page_types/blog_posting.rb', line 7

def images
  @images
end

#published_atObject (readonly)

Returns the value of attribute published_at.



7
8
9
# File 'app/src/page_structured_data/page_types/blog_posting.rb', line 7

def published_at
  @published_at
end

#updated_atObject (readonly)

Returns the value of attribute updated_at.



7
8
9
# File 'app/src/page_structured_data/page_types/blog_posting.rb', line 7

def updated_at
  @updated_at
end

Instance Method Details

#json_ldObject

rubocop:disable Metrics/MethodLength



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
# File 'app/src/page_structured_data/page_types/blog_posting.rb', line 17

def json_ld # rubocop:disable Metrics/MethodLength
  node = {
    '@context': 'https://schema.org',
    '@type': 'BlogPosting',
  }

  node[:headline] = headline
  node[:image] = images
  node[:datePublished] = published_at
  node[:dateModified] = updated_at

  author_hash = authors.map do |author|
    {
      '@type': 'Person',
      name: author[:name],
      url: author[:url],
    }
  end

  node[:author] = author_hash

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