Created
June 20, 2013 21:09
-
-
Save chrismetcalf/5826673 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
#!/usr/bin/env ruby | |
# | |
require 'httparty' | |
require 'json' | |
by_categories = Hash.new { |h,k| h[k] = Array.new } | |
page = 1 | |
total = 0 | |
collected = 0 | |
begin | |
# Grab our current page of results and accumulate | |
results = HTTParty.get("http://data.cityofchicago.org/api/search/views.json?limitTo=TABLES&limit=100&page=#{page}") | |
total = results["count"] | |
results["results"].each do |dataset| | |
dataset = dataset["view"] | |
size = dataset["columns"].collect { |c| c["cachedContents"]["null"] + c["cachedContents"]["non_null"] }.max | |
by_categories[dataset["category"]] << { | |
"url" => "https://data.cityofchicago.org/d/#{dataset["id"]}", | |
"name" => dataset["name"], | |
"size" => size, | |
"value" => 42 # Not sure how this is calculated | |
} | |
collected += 1 | |
end | |
# To the next page and beyond! | |
page += 1 | |
end while collected < total | |
# Formulate the final hash | |
output = { | |
"name" => "Chicago Data Portal", | |
"children" => by_categories.collect { |cat,ds| { "name" => cat, "children" => ds } } | |
} | |
puts JSON.pretty_generate(output) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment