Created
April 10, 2017 01:20
-
-
Save draveness/2c6b8f47f6ebda49081eb7b925274d2f to your computer and use it in GitHub Desktop.
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
require 'csv' | |
require 'uri' | |
require 'pry' | |
csv_text = File.read('1.csv') | |
csv = CSV.parse(csv_text, :headers => true) | |
hashes = [] | |
csv.each do |row| | |
hash = row.to_hash | |
old = hash["old"] | |
uri = URI(old) | |
uri.scheme = 'http' if uri.scheme != 'http' | |
uri.host = 'draveness.me' | |
uri.port = 80 | |
uri.query = nil | |
resource = uri.path.split('/').last | |
unless resource =~ /\.html$/ | |
resources = uri.path.split('/') | |
resources.pop | |
resources = [] if resources.nil? | |
resources += [resource + '.html'] unless resource.nil? | |
uri.path = resources.join('/') | |
end | |
hash['new'] = uri.to_s | |
hashes += [{ "old": old, "new": uri.to_s }] | |
end | |
puts hashes | |
CSV.open("3.csv", "wb") { |csv| | |
hashes.each do |hash| | |
csv << hash.values | |
end | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment