Sha256: c12b39f311944a31df41157bea699764790043ea13e492cee4dd3bd357b7ac9b

Contents?: true

Size: 1.75 KB

Versions: 5

Compression:

Stored size: 1.75 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).merge(search_action)
      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

      def search_action
        PotentialSearchActionSchema.new(organisation_facet_params).structured_data
      end

      def slug
        uri = URI.parse(page.canonical_url)
        File.basename(uri.path)
      end

      def organisation_facet_params
        { organisations: [slug] }
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
govuk_publishing_components-16.27.0 lib/govuk_publishing_components/presenters/machine_readable/organisation_schema.rb
govuk_publishing_components-16.26.0 lib/govuk_publishing_components/presenters/machine_readable/organisation_schema.rb
govuk_publishing_components-16.25.0 lib/govuk_publishing_components/presenters/machine_readable/organisation_schema.rb
govuk_publishing_components-16.24.0 lib/govuk_publishing_components/presenters/machine_readable/organisation_schema.rb
govuk_publishing_components-16.23.0 lib/govuk_publishing_components/presenters/machine_readable/organisation_schema.rb