Class: PageStructuredData::PageTypes::Article

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

Overview

Shared structured data for schema.org article-like page types.

Direct Known Subclasses

BlogPosting, NewsArticle

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Article.



9
10
11
12
13
14
15
# File 'app/src/page_structured_data/page_types/article.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/article.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/article.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/article.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/article.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/article.rb', line 7

def updated_at
  @updated_at
end

Instance Method Details

#json_ldObject



35
36
37
38
39
40
41
# File 'app/src/page_structured_data/page_types/article.rb', line 35

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

#to_hObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/src/page_structured_data/page_types/article.rb', line 17

def to_h
  {
    '@context': 'https://schema.org',
    '@type': schema_type,
    headline: headline,
    image: images,
    datePublished: published_at,
    dateModified: updated_at,
    author: authors.map do |author|
      {
        '@type': 'Person',
        name: author[:name],
        url: author[:url],
      }
    end,
  }
end