Skip to content

Commit

Permalink
Add a rudimentary endpoint for vote data
Browse files Browse the repository at this point in the history
It’s a start. Toward #10.
  • Loading branch information
waldoj committed Feb 18, 2018
1 parent afcb34a commit 0a16ad7
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
1 change: 1 addition & 0 deletions htdocs/1.1/.htaccess
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ RewriteRule ^bills/([0-9]{4}).json$ bills.php?year=$1 [QSA]
RewriteRule ^bill/([0-9]{4})/([A-Za-z0-9]+).json$ bill.php?year=$1&bill=$2 [QSA]
RewriteRule ^legislators.json$ legislators.php [QSA]
RewriteRule ^legislator/([a-z]+).json$ legislator.php?shortname=$1 [QSA]
RewriteRule ^vote/([0-9]{4})/([a-zA-Z0-9]{4,12}).json$ vote.php?year=$1&lis_id=$2 [QSA]

# By default, API data expires in just one hour.
#Header set Cache-Control "max-age=360, public"
64 changes: 64 additions & 0 deletions htdocs/1.1/vote.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

/**
* Vote endpoint
*
* PURPOSE
* Displays the outcome of a vote for a given year
**/


/*
* Includes
*/
require_once $_SERVER['DOCUMENT_ROOT'].'/includes/settings.inc.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/includes/functions.inc.php';
require_once 'functions.inc.php';

/*
* Localize variables
*/
$year = mysql_escape_string($_REQUEST['year']);
$lis_id = mysql_escape_string($_REQUEST['lis_id']);
if (isset($_REQUEST['callback']))
{
$callback = $_REQUEST['callback'];
}

/*
* Send an HTTP header defining the content as JSON.
*/
header('Content-type: application/json');

/*
* Send an HTTP header allowing CORS.
*/
header("Access-Control-Allow-Origin: *");

$vote_info = new Vote;
$vote_info->lis_id = $lis_id;
$vote_info->session_year = $year;
$vote = $vote_info->get_aggregate();
if ($vote === FALSE)
{
header('HTTP/1.0 404 Not Found');
exit();
}

/*
* Get detailed information about who voted how.
*/
$vote['legislators'] = $vote_info->get_detailed();


# Send the JSON. If a callback has been specified, prefix the JSON with that callback and wrap the
# JSON in parentheses.
if (isset($callback))
{
echo $callback . ' (';
}
echo json_encode($vote);
if (isset($callback))
{
echo ');';
}

0 comments on commit 0a16ad7

Please sign in to comment.