Skip to content

Instantly share code, notes, and snippets.

@christophermanning
Last active September 29, 2015 18:57
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 christophermanning/1649906 to your computer and use it in GitHub Desktop.
Save christophermanning/1649906 to your computer and use it in GitHub Desktop.
Extract JSON from ChicagoLobbyists

Rudimentary method of extracting JSON data from the ChicagoLobbyists sinatra app for a force directed graph

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