Sha256: 6fcdc67ce6c8c8795aae102779007be63a6a5e352866b840215994dae5aade3a

Contents?: true

Size: 1.44 KB

Versions: 42

Compression:

Stored size: 1.44 KB

Contents

module GovukPublishingComponents
  module Presenters
    class OrganisationSchema
      attr_reader :page

      def initialize(page)
        @page = page
      end

      def structured_data
        # http://schema.org/GovernmentOrganization
        {
          "@context" => "http://schema.org",
          "@type" => "GovernmentOrganization",
          "mainEntityOfPage" => {
            "@type" => "WebPage",
            "@id" => page.canonical_url,
          },
          "name" => page.title,
          "description" => page.description || page.body
        }.merge(parent_organisations).merge(sub_organisations)
      end

    private

      def parent_organisations
        related_organisations("ordered_parent_organisations", "parentOrganization")
      end

      def sub_organisations
        related_organisations("ordered_child_organisations", "subOrganization")
      end

      def related_organisations(link_type, schema_name)
        organisations = page.content_item.dig("links", link_type)

        return {} unless organisations.present?

        related_orgs = organisations.map do |org|
          page = Page.new(content_item: org)
          linked_org(page.canonical_url)
        end

        {
          schema_name => related_orgs
        }
      end

      def linked_org(url)
        {
          "@context" => "http://schema.org",
          "@type" => "GovernmentOrganization",
          "sameAs" => url
        }
      end
    end
  end
end

Version data entries

42 entries across 42 versions & 1 rubygems

Version Path
govuk_publishing_components-13.5.2 lib/govuk_publishing_components/presenters/machine_readable/organisation_schema.rb
govuk_publishing_components-13.5.1 lib/govuk_publishing_components/presenters/machine_readable/organisation_schema.rb