-
Notifications
You must be signed in to change notification settings - Fork 17
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 #2123 from alphagov/brexit_hub_analytics
Add some analytics to the brexit child taxon pages
- Loading branch information
Showing
8 changed files
with
268 additions
and
9 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
class BodyParser | ||
def initialize(body) | ||
@body = body | ||
end | ||
|
||
def title_and_link_sections | ||
if raw_title_and_link_sections.any? | ||
raw_title_and_link_sections.map do |section| | ||
s = parse(section) | ||
{ | ||
title: parsed_title(s), | ||
links: parsed_links(s), | ||
} | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
attr_reader :body | ||
|
||
def parse(html) | ||
Nokogiri::HTML.parse(html) | ||
rescue Nokogiri::XML::SyntaxError | ||
"" | ||
end | ||
|
||
def parsed_body | ||
@parsed_body ||= parse(body) | ||
end | ||
|
||
def raw_body_content | ||
if parsed_body.present? | ||
parsed_body.search("div.govspeak").children | ||
end | ||
end | ||
|
||
def raw_title_and_link_sections | ||
if raw_body_content.any? | ||
raw_body_content.to_s.split(/(?=<h2 )/) | ||
end | ||
end | ||
|
||
def parsed_links(section) | ||
raw_links = section.present? ? section.css("a") : [] | ||
if raw_links.any? | ||
raw_links.map do |link| | ||
{ | ||
path: format_path(link["href"]), | ||
text: link.content, | ||
} | ||
end | ||
else | ||
[] | ||
end | ||
end | ||
|
||
def parsed_title(section) | ||
raw_title = section.present? ? section.at_css("h2") : "" | ||
raw_title.present? ? raw_title.content : "" | ||
end | ||
|
||
def format_path(raw_url) | ||
uri = URI.parse(raw_url) | ||
uri.host == "www.gov.uk" ? uri.path : raw_url | ||
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
28 changes: 28 additions & 0 deletions
28
app/views/content_items/detailed_guide/_brexit_links.html.erb
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,28 @@ | ||
<div class="govuk-grid-row"> | ||
<% @content_item.title_and_link_sections.each do |section| %> | ||
<% if section[:title].present? %> | ||
<%= render "govuk_publishing_components/components/heading", { | ||
text: section[:title], | ||
heading_level: 2, | ||
font_size: "m", | ||
margin_bottom: 6, | ||
} %> | ||
<% end %> | ||
<% if section[:links].present? %> | ||
<% track_category = brexit_link[:track_category] %> | ||
<% links = section[:links].map do |link| | ||
link_to(link[:text], link[:path], class: "govuk-link", data: { | ||
track_action: link[:path], | ||
track_category: track_category, | ||
track_label: section[:title] || "", | ||
module: 'gem-track-click', | ||
}) | ||
end | ||
%> | ||
<%= render "govuk_publishing_components/components/list", { | ||
items: links, | ||
visible_counters: true, | ||
} %> | ||
<% end %> | ||
<% end %> | ||
</div> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
require "test_helper" | ||
|
||
class BodyParserTest < ActiveSupport::TestCase | ||
def html | ||
'<div class="govspeak">'\ | ||
'<h2 id="travel-to-the-eu">Travel to the EU</h2>\n \n'\ | ||
'<ul>\n'\ | ||
'<li><a rel="external" href="https://www.gov.uk/foreign-travel-advice" '\ | ||
'class="govuk-link">Foreign travel advice</a></li>\n'\ | ||
'<li><a rel="external" href="https://www.gov.uk/visit-eu"'\ | ||
'class="govuk-link">Travelling to the EU'\ | ||
'</a></li>\n</ul>\n'\ | ||
'<h2 id="travel-to-the-uk">Travel to the UK</h2>\n \n'\ | ||
'<ul>\n'\ | ||
'<li><a rel="external" href="https://www.gov.uk/local-travel-advice" '\ | ||
'class="govuk-link">Local travel advice</a></li>\n'\ | ||
'<li><a rel="external" href="https://www.gov.uk/visit-uk"'\ | ||
'class="govuk-link">Travelling to the UK'\ | ||
'</a></li>\n</ul>\n'\ | ||
"</div>" | ||
end | ||
|
||
def html_missing_section_headings | ||
'<div class="govspeak">'\ | ||
'<ul>\n'\ | ||
'<li><a rel="external" href="https://www.gov.uk/foreign-travel-advice" '\ | ||
'class="govuk-link">Foreign travel advice</a></li>\n'\ | ||
'<li><a rel="external" href="https://www.gov.uk/visit-eu"'\ | ||
'class="govuk-link">Travelling to the EU'\ | ||
'</a></li>\n</ul>\n'\ | ||
'<ul>\n'\ | ||
'<li><a rel="external" href="https://www.gov.uk/local-travel-advice" '\ | ||
'class="govuk-link">Local travel advice</a></li>\n'\ | ||
'<li><a rel="external" href="https://www.gov.uk/visit-uk"'\ | ||
'class="govuk-link">Travelling to the UK'\ | ||
'</a></li>\n</ul>\n'\ | ||
"</div>" | ||
end | ||
|
||
def subject | ||
@subject ||= BodyParser.new(html) | ||
end | ||
|
||
test "#title_and_link_sections" do | ||
expected = [ | ||
{ | ||
title: "Travel to the EU", | ||
links: [ | ||
{ | ||
path: "/foreign-travel-advice", | ||
text: "Foreign travel advice", | ||
}, | ||
{ | ||
path: "/visit-eu", | ||
text: "Travelling to the EU", | ||
}, | ||
], | ||
}, | ||
{ | ||
title: "Travel to the UK", | ||
links: [ | ||
{ | ||
path: "/local-travel-advice", | ||
text: "Local travel advice", | ||
}, | ||
{ | ||
path: "/visit-uk", | ||
text: "Travelling to the UK", | ||
}, | ||
], | ||
}, | ||
] | ||
|
||
assert_equal expected, subject.title_and_link_sections | ||
end | ||
|
||
test "when parsing html missing the section headings" do | ||
subject = BodyParser.new(html_missing_section_headings) | ||
expected = [ | ||
{ | ||
title: "", | ||
links: [ | ||
{ | ||
path: "/foreign-travel-advice", | ||
text: "Foreign travel advice", | ||
}, | ||
{ | ||
path: "/visit-eu", | ||
text: "Travelling to the EU", | ||
}, | ||
{ | ||
path: "/local-travel-advice", | ||
text: "Local travel advice", | ||
}, | ||
{ | ||
path: "/visit-uk", | ||
text: "Travelling to the UK", | ||
}, | ||
], | ||
}, | ||
] | ||
assert_equal expected, subject.title_and_link_sections | ||
end | ||
end |