Class: PageStructuredData::PageTypes::Article

Inherits:
Object
  • Object
show all
Includes:
SchemaNode
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, DiscussionForumPosting, NewsArticle

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(headline:, published_at:, updated_at:, images: [], authors: [], image: nil, article_body: nil, text: nil, url: nil, interaction_statistics: [], likes_count: nil, comments_count: nil, shares_count: nil) ⇒ Article

Returns a new instance of Article.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/src/page_structured_data/page_types/article.rb', line 12

def initialize(headline:, published_at:, updated_at:, images: [], authors: [], image: nil, article_body: nil, text: nil,
               url: nil, interaction_statistics: [], likes_count: nil, comments_count: nil, shares_count: nil)
  @headline = headline
  @images = image.present? ? Array(image) : Array(images)
  @published_at = published_at
  @updated_at = updated_at
  @authors = Array(authors)
  @article_body = article_body || text
  @url = url
  @interaction_statistics = Array(interaction_statistics)
  @likes_count = likes_count
  @comments_count = comments_count
  @shares_count = shares_count
end

Instance Attribute Details

#article_bodyObject (readonly)

Returns the value of attribute article_body.



9
10
11
# File 'app/src/page_structured_data/page_types/article.rb', line 9

def article_body
  @article_body
end

#authorsObject (readonly)

Returns the value of attribute authors.



9
10
11
# File 'app/src/page_structured_data/page_types/article.rb', line 9

def authors
  @authors
end

#comments_countObject (readonly)

Returns the value of attribute comments_count.



9
10
11
# File 'app/src/page_structured_data/page_types/article.rb', line 9

def comments_count
  @comments_count
end

#headlineObject (readonly)

Returns the value of attribute headline.



9
10
11
# File 'app/src/page_structured_data/page_types/article.rb', line 9

def headline
  @headline
end

#imagesObject (readonly)

Returns the value of attribute images.



9
10
11
# File 'app/src/page_structured_data/page_types/article.rb', line 9

def images
  @images
end

#interaction_statisticsObject (readonly)

Returns the value of attribute interaction_statistics.



9
10
11
# File 'app/src/page_structured_data/page_types/article.rb', line 9

def interaction_statistics
  @interaction_statistics
end

#likes_countObject (readonly)

Returns the value of attribute likes_count.



9
10
11
# File 'app/src/page_structured_data/page_types/article.rb', line 9

def likes_count
  @likes_count
end

#published_atObject (readonly)

Returns the value of attribute published_at.



9
10
11
# File 'app/src/page_structured_data/page_types/article.rb', line 9

def published_at
  @published_at
end

#shares_countObject (readonly)

Returns the value of attribute shares_count.



9
10
11
# File 'app/src/page_structured_data/page_types/article.rb', line 9

def shares_count
  @shares_count
end

#updated_atObject (readonly)

Returns the value of attribute updated_at.



9
10
11
# File 'app/src/page_structured_data/page_types/article.rb', line 9

def updated_at
  @updated_at
end

#urlObject (readonly)

Returns the value of attribute url.



9
10
11
# File 'app/src/page_structured_data/page_types/article.rb', line 9

def url
  @url
end

Instance Method Details

#json_ldObject



45
46
47
48
49
50
51
# File 'app/src/page_structured_data/page_types/article.rb', line 45

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

#to_hObject



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/article.rb', line 27

def to_h
  node = {
    '@context': 'https://schema.org',
    '@type': schema_type,
    headline: headline,
    image: images,
    datePublished: published_at,
    dateModified: updated_at,
    author: authors.map { |author| author_to_h(author) },
  }

  node[:articleBody] = article_body if article_body.present?
  node[:url] = url if url.present?
  node[:interactionStatistic] = interaction_statistics_to_h if interaction_statistics_to_h.any?

  node
end