|
| 1 | +class NewsArticleStructured |
| 2 | + def initialize(presenter) |
| 3 | + @presenter = presenter |
| 4 | + end |
| 5 | + |
| 6 | + def structured_data |
| 7 | + return {} unless enough_structured_data? |
| 8 | + |
| 9 | + # http://schema.org/NewsArticle |
| 10 | + { |
| 11 | + "@context" => "http://schema.org", |
| 12 | + "@type" => "NewsArticle", |
| 13 | + "mainEntityOfPage" => { |
| 14 | + "@type" => "WebPage", |
| 15 | + "@id" => page_url, |
| 16 | + }, |
| 17 | + "headline" => presenter.title, |
| 18 | + "datePublished" => presenter.first_public_at, |
| 19 | + "dateModified" => presenter.public_updated_at, |
| 20 | + "description" => presenter.description, |
| 21 | + "publisher" => { |
| 22 | + "@type" => "Organization", |
| 23 | + "name" => "GOV.UK", |
| 24 | + "url" => "https://www.gov.uk", |
| 25 | + "logo" => { |
| 26 | + "@type" => "ImageObject", |
| 27 | + # TODO: change this to a better image, without the URL hard coded. |
| 28 | + "url" => "https://assets.publishing.service.gov.uk/static/opengraph-image-a1f7d89ffd0782738b1aeb0da37842d8bd0addbd724b8e58c3edbc7287cc11de.png", |
| 29 | + }, |
| 30 | + }, |
| 31 | + "image" => [ |
| 32 | + image["url"], |
| 33 | + ], |
| 34 | + "author" => { |
| 35 | + "@type" => "Organization", |
| 36 | + "name" => publishing_organisation["title"], |
| 37 | + "url" => Plek.current.website_root + publishing_organisation["base_path"], |
| 38 | + }, |
| 39 | + } |
| 40 | + end |
| 41 | + |
| 42 | +private |
| 43 | + |
| 44 | + attr_reader :presenter |
| 45 | + |
| 46 | + def enough_structured_data? |
| 47 | + # The author (for which we use the publishing org) and image are required |
| 48 | + # fields. If the news article doesn't have them, don't use structured data |
| 49 | + # at all. |
| 50 | + publishing_organisation && image |
| 51 | + end |
| 52 | + |
| 53 | + def publishing_organisation |
| 54 | + presenter.content_item.dig("links", "primary_publishing_organisation").to_a.first |
| 55 | + end |
| 56 | + |
| 57 | + def page_url |
| 58 | + Plek.current.website_root + presenter.content_item["base_path"] |
| 59 | + end |
| 60 | + |
| 61 | + def image |
| 62 | + presenter.image |
| 63 | + end |
| 64 | +end |
0 commit comments