Skip to content

Commit 0187eaf

Browse files
authored
Merge pull request #37 from hmrc/TRG-145
TRG-145: Support sites which use relative URLs.
2 parents b20af20 + c58003d commit 0187eaf

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

lib/notifier.rb

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require 'http'
22
require 'json'
33
require 'date'
4+
require 'uri'
45

56
require_relative './page'
67
require_relative './notification/expired'
@@ -37,7 +38,10 @@ def run
3738
end
3839

3940
def pages
40-
JSON.parse(HTTP.get(@pages_url)).map { |data| Page.new(data) }
41+
JSON.parse(HTTP.get(@pages_url)).map { |data|
42+
data['url'] = get_absolute_url(data['url'])
43+
Page.new(data)
44+
}
4145
end
4246

4347
def pages_per_channel
@@ -85,4 +89,26 @@ def message_payloads(grouped_pages)
8589
def post_to_slack?
8690
@live
8791
end
92+
93+
private
94+
95+
def get_absolute_url url
96+
target_uri = URI(url)
97+
target_path = Pathname.new(target_uri.path)
98+
source_uri = URI(@pages_url)
99+
100+
if target_path.relative?
101+
resulting_path = URI::join(source_uri, target_uri.path).path
102+
else
103+
resulting_path = target_uri.path
104+
end
105+
106+
if source_uri.scheme == 'https'
107+
URI::HTTPS.build(scheme: source_uri.scheme, port: source_uri.port, host: source_uri.host, path: resulting_path).to_s
108+
else
109+
URI::HTTP.build(scheme: source_uri.scheme, port: source_uri.port, host: source_uri.host, path: resulting_path).to_s
88110
end
111+
end
112+
end
113+
114+

0 commit comments

Comments
 (0)