Skip to content

Instantly share code, notes, and snippets.

@indirect
Created January 10, 2016 04:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save indirect/a1dd50bd9014c14e3b62 to your computer and use it in GitHub Desktop.
Save indirect/a1dd50bd9014c14e3b62 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'csv'
unless ARGV[0]
puts "USAGE: #{__FILE__} SHOPIFY_CSV"
end
FIELDS = ["Shipping Name", "Shipping Street", "Shipping Address1", "Shipping Address2", "Shipping Company", "Shipping City", "Shipping Zip", "Shipping Province", "Shipping Country", "Shipping Phone"]
def parse_csv(filename)
csv = CSV.new(File.open(filename), headers: :first_row)
rows = csv.each.to_a
[csv.headers, rows]
end
def copy_shipping(orders)
orders.each_cons(2) do |one, two|
FIELDS.each do |field|
two[field] ||= one[field]
end
end
end
def remove_digital(orders)
orders.reject!{|o| o["Lineitem name"] =~ /- Digital\Z/ }
end
def clean_zip_codes(orders)
orders.each{|o| o["Shipping Zip"].tr("'", "") }
end
def write_cleaned(filename, headers, orders)
basename = File.basename(filename, File.extname(filename))
CSV.open("#{basename}_cleaned.csv", "w") do |csv|
csv << headers
orders.each {|o| csv << o }
end
end
headers, orders = parse_csv(ARGV[0])
copy_shipping(orders)
remove_digital(orders)
clean_zip_codes(orders)
write_cleaned(ARGV[0], headers, orders)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment