Skip to content

Instantly share code, notes, and snippets.

@draveness
Created April 10, 2017 01:20
Show Gist options
  • Save draveness/2c6b8f47f6ebda49081eb7b925274d2f to your computer and use it in GitHub Desktop.
Save draveness/2c6b8f47f6ebda49081eb7b925274d2f to your computer and use it in GitHub Desktop.
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