Rudimentary method of extracting JSON data from the ChicagoLobbyists sinatra app for a force directed graph
Last active
September 29, 2015 18:57
-
-
Save christophermanning/1649906 to your computer and use it in GitHub Desktop.
Extract JSON from ChicagoLobbyists
This file contains 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
get "/force_directed.json" do | |
content_type :json | |
json_nodes = [] | |
json_links = [] | |
@lobbyists = Lobbyist.list_by_compensation | |
@lobbyists.sort{ |a,b| b[3] <=> a[3] }.take(params[:limit].to_i).each do |l| | |
lobbyist = Lobbyist.get l[0] | |
lobbyist_total_compensation = lobbyist.total_compensation.to_i | |
json_nodes << { :id => lobbyist.id, :name => lobbyist.full_name, :slug => lobbyist.slug, :type => 1, :value => lobbyist_total_compensation } | |
lobbyist_index = json_nodes.length - 1 | |
lobbyist.clients.each do |client| | |
if client.total_compensation.to_i > 0 | |
json_nodes << { :id => client.id, :name => client.name, :slug => client.slug, :type => 2, :value => client.total_compensation.to_i } if json_nodes.index{|f| f[:type] == 2 and f[:id] == client.id }.nil? | |
json_links << { :source => lobbyist_index, :target => json_nodes.index{|f| f[:type] == 2 and f[:id] == client.id }} | |
end | |
end | |
lobbyist.agencies.each do |agency| | |
json_nodes << { :id => agency.id, :name => agency.name, :slug => agency.slug, :type => 3, :value => 0 } if json_nodes.index{|f| f[:type] == 3 and f[:id] == agency.id }.nil? | |
json_links << { :source => lobbyist_index, :target => json_nodes.index{|f| f[:type] == 3 and f[:id] == agency.id }, :value => 0 } | |
end | |
lobbyist.actions.each do |action| | |
agency_index = json_nodes.index{|f| f[:type] == 3 and f[:id] == action.agency.id } | |
agency_link_index = json_links.index{|f| f[:source] == lobbyist_index and f[:target] == agency_index } | |
json_links[agency_link_index][:value] += 1 | |
end | |
end | |
{ :nodes => json_nodes, :links => json_links }.to_json | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment