Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TRG-145: Support sites which use relative URLs. #37

Merged
merged 1 commit into from
Mar 14, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion lib/notifier.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'http'
require 'json'
require 'date'
require 'uri'

require_relative './page'
require_relative './notification/expired'
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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