# download csv of data sources
require 'open-uri'
require 'csv'
require 'json'

url =  "https://docs.google.com/spreadsheets/d/1v1q73chka6HSgKtl2znJtTswuKZprEOlGY8r7RYuv94/export?format=csv"

download = open(url)
IO.copy_stream(download, 'sources.csv')

# for each row

sources = CSV.open('sources.csv', headers: true).readlines

sources.each do |source|
  file_url = source['CSV']
  print source
  download = open(file_url)
  # download csv
  IO.copy_stream(download, 'source.csv') 
  lines = CSV.open('source.csv').readlines
  keys = lines.delete lines.first
  keys.map! { |k| k.nil? ? "ID" : k.capitalize.gsub(/_(\w)/){$1.upcase}.gsub(/%/, "Percent")  }
  # export to json with file name
  File.open(source['Output']+'.json', 'w') do |f|
    data = lines.map do |values|
      Hash[keys.zip(values)]
    end
    f.puts JSON.pretty_generate(data)
  end
end


