Created
June 10, 2016 11:20
-
-
Save esaborit4code/69ec1538d2a7fb0a3d39fb02e51893d9 to your computer and use it in GitHub Desktop.
Redirection parser
This file contains hidden or 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
LOG_FILENAME = 'heroku-logs' | |
REQUEST_START_REGEX = /(.+)Started (.+) \"(.+)\"/ | |
REDIRECT_REGEX = /(.+)Redirected to (.+barkibu.com)(.+)/ | |
log_lines = File.readlines(LOG_FILENAME) | |
redirects = [] | |
last_request = {} | |
log_lines.each do |line| | |
line.match(REQUEST_START_REGEX) do |match_data| | |
last_request = { method: match_data[2], url: match_data[3]} | |
next | |
end | |
line.match(REDIRECT_REGEX) do |match_data| | |
redirect_url = match_data[3] | |
if redirects.any? && (redirects.last.last == last_request[:url]) | |
redirects.last << redirect_url | |
else | |
redirects << [last_request[:url], redirect_url] | |
end | |
end | |
end | |
def print_url(url, deepness) | |
prefix = "#{'>' * 4 * deepness} " if deepness > 0 | |
puts "#{prefix}#{url}" | |
end | |
redirects.each do |redirect| | |
redirect.each_with_index do |url, index| | |
print_url url, index | |
end | |
puts | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment