From c58003dd5ffbaffd38bb42c1f948fa286c455281 Mon Sep 17 00:00:00 2001 From: "edd.grant" Date: Fri, 14 Jan 2022 09:38:03 +0000 Subject: [PATCH] TRG-139: Support sites which use relative URLs. This involves using the site's URL, in combination with the relative URLs returned in `/api/pages.json` and using them to build absolute an absolute URL to each page which is to be notified upon. Sites which use absolute links are unaffected. --- lib/notifier.rb | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/lib/notifier.rb b/lib/notifier.rb index 2a1b0cc..fdc7c45 100644 --- a/lib/notifier.rb +++ b/lib/notifier.rb @@ -1,6 +1,7 @@ require 'http' require 'json' require 'date' +require 'uri' require_relative './page' require_relative './notification/expired' @@ -37,7 +38,10 @@ def run end def pages - JSON.parse(HTTP.get(@pages_url)).map { |data| Page.new(data) } + JSON.parse(HTTP.get(@pages_url)).map { |data| + data['url'] = get_absolute_url(data['url']) + Page.new(data) + } end def pages_per_channel @@ -85,4 +89,26 @@ def message_payloads(grouped_pages) def post_to_slack? @live end + + private + + def get_absolute_url url + target_uri = URI(url) + target_path = Pathname.new(target_uri.path) + source_uri = URI(@pages_url) + + if target_path.relative? + resulting_path = URI::join(source_uri, target_uri.path).path + else + resulting_path = target_uri.path + end + + if source_uri.scheme == 'https' + URI::HTTPS.build(scheme: source_uri.scheme, port: source_uri.port, host: source_uri.host, path: resulting_path).to_s + else + URI::HTTP.build(scheme: source_uri.scheme, port: source_uri.port, host: source_uri.host, path: resulting_path).to_s end + end +end + +