-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1087 from alphagov/faq-pages
Add FAQPage schema
- Loading branch information
Showing
6 changed files
with
116 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
lib/govuk_publishing_components/presenters/machine_readable/faq_page_schema.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
module GovukPublishingComponents | ||
module Presenters | ||
class FaqPageSchema | ||
attr_reader :page | ||
|
||
def initialize(page) | ||
@page = page | ||
end | ||
|
||
def structured_data | ||
# http://schema.org/FAQPage | ||
data = CreativeWorkSchema.new(@page).structured_data | ||
.merge(main_entity) | ||
data["@type"] = "FAQPage" | ||
data | ||
end | ||
|
||
private | ||
|
||
def main_entity | ||
{ | ||
"mainEntity" => questions_and_answers | ||
} | ||
end | ||
|
||
def questions_and_answers | ||
page.parts.each_with_index.map do |part, index| | ||
part_url = part_url(part, index) | ||
|
||
{ | ||
"@type" => "Question", | ||
"name" => part['title'], | ||
"url" => part_url, | ||
"acceptedAnswer" => { | ||
"@type" => "Answer", | ||
"url" => part_url, | ||
"text" => part['body'] | ||
} | ||
} | ||
end | ||
end | ||
|
||
def part_url(part, index) | ||
if index.zero? | ||
page.canonical_url | ||
else | ||
page.canonical_url + "/" + part["slug"] | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters