Created
June 10, 2016 12:06
-
-
Save esaborit4code/ad0827e36bacbadb931fb8dd990f1294 to your computer and use it in GitHub Desktop.
Locale redirections 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| | |
url = match_data[3] | |
next unless url | |
locale = url.split('?').first.split('/')[1] | |
last_request = { method: match_data[2], url: url, locale: locale} | |
next | |
end | |
line.match(REDIRECT_REGEX) do |match_data| | |
redirect_url = match_data[3] | |
locale = redirect_url.split('?').first.split('/')[1] | |
if redirects.any? && (redirects.last.last == last_request[:url]) | |
redirects.last << redirect_url | |
else | |
next if locale == last_request[:locale] | |
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