Skip to content

Instantly share code, notes, and snippets.

@shaneshifflett
Created February 21, 2014 23:37
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 shaneshifflett/9145985 to your computer and use it in GitHub Desktop.
Save shaneshifflett/9145985 to your computer and use it in GitHub Desktop.
convert a google spreadsheet to a json file that can be read by timeline.js
task :jsonify_timeline => :environment do
require 'csv'
require 'json'
require 'open-uri'
def scrub(text)
if text.include? '--'
text = text.gsub(/--/, '—')
end
return text
end
url = 'https://docs.google.com/spreadsheet/pub?key=URKEYE&output=csv'
page = open(url)
contents = page.read()
dump = {
:timeline => {
:headline => "",
:type => "default",
:text => "",
:asset => {
:media => "",
:credit => "",
:caption => ""
},
:date => []
}
}
CSV.parse(contents) do |row|
date_obj = {
:startDate => row[0],
:endDate => row[1],
:headline => row[2],
:text => scrub(row[3]),
:asset => {
:media => row[4],
:credit => row[5],
:caption => row[6]
}
}
dump[:timeline][:date].push(date_obj)
end
File.open("#{Rails.root}/timeline-data.json", 'w') { |file| file.write(dump.to_json) }
puts dump.to_json
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment