class GovukPublishingComponents::Presenters::CreativeWorkSchema

Attributes

page[R]
presenter[R]

Public Class Methods

new(page) click to toggle source
# File lib/govuk_publishing_components/presenters/machine_readable/creative_work_schema.rb, line 6
def initialize(page)
  @page = page
  @pages = {}
end

Public Instance Methods

structured_data() click to toggle source
# File lib/govuk_publishing_components/presenters/machine_readable/creative_work_schema.rb, line 11
def structured_data
  # http://schema.org/CreativeWork
  {
    "@context" => "http://schema.org",
    "@type" => "CreativeWork",
    "mainEntityOfPage" => {
      "@type" => "WebPage",
      "@id" => page.canonical_url,
    },
    "name" => page.title,
    "datePublished" => page.content_item["first_published_at"],
    "dateModified" => page.content_item["public_updated_at"],
    "text" => page.description,
    "publisher" => {
      "@type" => "Organization",
      "name" => "GOV.UK",
      "url" => "https://www.gov.uk",
      "logo" => {
        "@type" => "ImageObject",
        "url" => page.logo_url,
      },
    },
  }.merge(image_schema).merge(author_schema).merge(is_part_of).merge(about).merge(has_part)
end

Private Instance Methods

about() click to toggle source
# File lib/govuk_publishing_components/presenters/machine_readable/creative_work_schema.rb, line 100
def about
  return {} unless live_taxons.any?

  {
    "about" => linked_taxons,
  }
end
author_schema() click to toggle source
# File lib/govuk_publishing_components/presenters/machine_readable/creative_work_schema.rb, line 46
def author_schema
  return {} unless publishing_organisation

  {
    "author" => {
      "@type" => "Organization",
      "name" => publishing_organisation["title"],
      "url" => Plek.current.website_root + publishing_organisation["base_path"],
    },
  }
end
collection_pages(linked_type) click to toggle source
# File lib/govuk_publishing_components/presenters/machine_readable/creative_work_schema.rb, line 92
def collection_pages(linked_type)
  @pages[linked_type] ||= fetch_collection_pages(linked_type)
end
document_collections() click to toggle source
# File lib/govuk_publishing_components/presenters/machine_readable/creative_work_schema.rb, line 87
def document_collections
  @document_collections ||= collection_pages("document_collections")
                                .map { |document| IsPartOfSchema.new(document).structured_data }
end
fetch_collection_pages(linked_type) click to toggle source
# File lib/govuk_publishing_components/presenters/machine_readable/creative_work_schema.rb, line 96
def fetch_collection_pages(linked_type)
  page.content_item.dig("links", linked_type).to_a.map { |document| document["web_url"] }
end
has_part() click to toggle source
# File lib/govuk_publishing_components/presenters/machine_readable/creative_work_schema.rb, line 79
def has_part
  return {} unless collection_pages("documents").any?

  {
    "hasPart" => collection_pages("documents").map { |document| HasPartSchema.new(document).structured_data },
  }
end
image_schema() click to toggle source
# File lib/govuk_publishing_components/presenters/machine_readable/creative_work_schema.rb, line 40
def image_schema
  {
    "image" => page.has_image? ? [page.image_url] : page.image_placeholders,
  }
end
is_part_of() click to toggle source
# File lib/govuk_publishing_components/presenters/machine_readable/creative_work_schema.rb, line 62
def is_part_of
  return {} unless document_collections.any?

  {
    "isPartOf" => document_collections,
  }
end
linked_page(step_by_step) click to toggle source
# File lib/govuk_publishing_components/presenters/machine_readable/creative_work_schema.rb, line 70
def linked_page(step_by_step)
  Page.new(
    content_item: step_by_step,
    schema: :article,
    logo_url: page.logo_url,
    image_placeholders: page.image_placeholders,
  )
end
linked_taxons() click to toggle source
# File lib/govuk_publishing_components/presenters/machine_readable/creative_work_schema.rb, line 115
def linked_taxons
  live_taxons.map do |taxon|
    {
      "@context" => "http://schema.org",
      "@type" => "Thing",
      "sameAs" => taxon["web_url"],
    }
  end
end
live_taxons() click to toggle source
# File lib/govuk_publishing_components/presenters/machine_readable/creative_work_schema.rb, line 108
def live_taxons
  taxons = page.content_item.dig("links", "taxons")
  return [] unless taxons

  taxons.select { |taxon| taxon["phase"] == "live" }
end
publishing_organisation() click to toggle source
# File lib/govuk_publishing_components/presenters/machine_readable/creative_work_schema.rb, line 58
def publishing_organisation
  page.content_item.dig("links", "primary_publishing_organisation").to_a.first
end