Sha256: ccd0ea1c8afe781807d61f037d79ebb776a9034ba7cf8113852624c5b169a8e3

Contents?: true

Size: 1.9 KB

Versions: 3

Compression:

Stored size: 1.9 KB

Contents

module GovukPublishingComponents
  module Presenters
    class ArticleSchema
      attr_reader :page

      def initialize(page)
        @page = page
      end

      def structured_data
        # http://schema.org/Article
        {
          "@context" => "http://schema.org",
          "@type" => "Article",
          "mainEntityOfPage" => {
            "@type" => "WebPage",
            "@id" => page.canonical_url,
          },
          "headline" => page.title,
          "datePublished" => page.content_item["first_published_at"],
          "dateModified" => page.content_item["public_updated_at"],
          "description" => 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(body)
      end

    private

      attr_reader :presenter

      # Not all formats have a `body` - some have their content split over
      # multiple fields. In this case we'll skip the `articleBody` field
      def body
        return {} unless page.body.present?

        {
          "articleBody" => page.body
        }
      end

      def image_schema
        return {} unless page.has_image?

        {
          "image" => [
            page.image_url
          ]
        }
      end

      def author_schema
        return {} unless publishing_organisation

        {
          "author" => {
            "@type" => "Organization",
            "name" => publishing_organisation["title"],
            "url" => Plek.current.website_root + publishing_organisation["base_path"],
          }
        }
      end

      def publishing_organisation
        page.content_item.dig("links", "primary_publishing_organisation").to_a.first
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
govuk_publishing_components-9.1.0 lib/govuk_publishing_components/presenters/machine_readable/article_schema.rb
govuk_publishing_components-9.0.1 lib/govuk_publishing_components/presenters/machine_readable/article_schema.rb
govuk_publishing_components-9.0.0 lib/govuk_publishing_components/presenters/machine_readable/article_schema.rb