class GovukPublishingComponents::Presenters::DatasetSchema

Attributes

page[R]

Public Class Methods

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

Public Instance Methods

structured_data() click to toggle source
# File lib/govuk_publishing_components/presenters/machine_readable/dataset_schema.rb, line 10
def structured_data
  # http://schema.org/Dataset
  data = CreativeWorkSchema.new(@page).structured_data
    .merge(distribution)
    .merge(description)
    .merge(name)
  data["@type"] = "Dataset"
  data
end

Private Instance Methods

description() click to toggle source
# File lib/govuk_publishing_components/presenters/machine_readable/dataset_schema.rb, line 30
def description
  descr = page.body || page.description
  return {} unless descr

  {
    "description" => descr.slice(0..4999),
  }
end
distribution() click to toggle source
# File lib/govuk_publishing_components/presenters/machine_readable/dataset_schema.rb, line 22
def distribution
  return {} unless page.attachments

  {
    "distribution" => page.attachments.map { |a| present_attachment(a.with_indifferent_access) }.compact,
  }
end
name() click to toggle source
# File lib/govuk_publishing_components/presenters/machine_readable/dataset_schema.rb, line 39
def name
  {
    "name" => page.title,
  }
end
present_attachment(attachment) click to toggle source
# File lib/govuk_publishing_components/presenters/machine_readable/dataset_schema.rb, line 45
def present_attachment(attachment)
  title = attachment[:title]
  url = attachment[:url]
  return unless title
  return unless url

  case attachment[:attachment_type]
  when "external", "html"
    {
      "name" => title,
      "url" => url,
    }
  when "file"
    {
      "name" => title,
      "contentUrl" => url,
      "encodingFormat" => attachment[:content_type],
    }.compact
  end
end