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

Methods included from SchemaNode

#valid?

Constructor Details

#initialize(headline:, published_at:, updated_at:, images: [], authors: [], image: nil, article_body: nil, text: nil, description: nil, url: nil, main_entity_of_page: nil, publisher: nil, article_section: nil, keywords: nil, word_count: nil, in_language: nil, interaction_statistics: [], likes_count: nil, comments_count: nil, shares_count: nil) ⇒ Article

Returns a new instance of Article.



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

def initialize(headline:, published_at:, updated_at:, images: [], authors: [], image: nil, article_body: nil, text: nil,
               description: nil, url: nil, main_entity_of_page: nil, publisher: nil, article_section: nil,
               keywords: nil, word_count: nil, in_language: 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)
  @description = description
  @article_body = article_body || text
  @url = url
  @main_entity_of_page = main_entity_of_page
  @publisher = publisher
  @article_section = article_section
  @keywords = keywords
  @word_count = word_count
  @in_language = in_language
  @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

#article_sectionObject (readonly)

Returns the value of attribute article_section.



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

def article_section
  @article_section
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

#descriptionObject (readonly)

Returns the value of attribute description.



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

def description
  @description
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

#in_languageObject (readonly)

Returns the value of attribute in_language.



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

def in_language
  @in_language
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

#keywordsObject (readonly)

Returns the value of attribute keywords.



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

def keywords
  @keywords
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

#main_entity_of_pageObject (readonly)

Returns the value of attribute main_entity_of_page.



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

def main_entity_of_page
  @main_entity_of_page
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

#publisherObject (readonly)

Returns the value of attribute publisher.



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

def publisher
  @publisher
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

#word_countObject (readonly)

Returns the value of attribute word_count.



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

def word_count
  @word_count
end

Instance Method Details

#json_ldObject



62
63
64
65
66
67
68
# File 'app/src/page_structured_data/page_types/article.rb', line 62

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

#to_hObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/src/page_structured_data/page_types/article.rb', line 37

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[:description] = description if description.present?
  node[:articleBody] = article_body if article_body.present?
  node[:url] = url if url.present?
  node[:mainEntityOfPage] = object_to_h(main_entity_of_page) if main_entity_of_page.present?
  node[:publisher] = object_to_h(publisher) if publisher.present?
  node[:articleSection] = article_section if article_section.present?
  node[:keywords] = keywords if keywords.present?
  node[:wordCount] = word_count if word_count.present?
  node[:inLanguage] = object_to_h(in_language) if in_language.present?
  node[:interactionStatistic] = interaction_statistics_to_h if interaction_statistics_to_h.any?

  node
end

#warningsObject



70
71
72
73
74
75
76
# File 'app/src/page_structured_data/page_types/article.rb', line 70

def warnings
  required_attribute_warnings(
    headline: headline,
    published_at: published_at,
    updated_at: updated_at
  ) + author_warnings + publisher_warnings + interaction_statistic_warnings
end