Skip to content

Commit

Permalink
Merge pull request #7 from texdc/feature-namespace
Browse files Browse the repository at this point in the history
Add namespace and test
  • Loading branch information
chrismetcalf committed Oct 23, 2014
2 parents da79e13 + 83dde81 commit febe86e
Show file tree
Hide file tree
Showing 9 changed files with 364 additions and 221 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
.DS_Store

/vendor/
/vendor
/composer.lock
/phpunit.xml
/build

# Ignore eclipse project files
.settings
Expand Down
42 changes: 34 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,63 @@ Updated 2014-07-22, Chris Metcalf, chris.metcalf (at) socrata.com

This library provides a simple wrapper for accessing some of the features of the Socrata Open Data API from PHP. Currently it supports HTTP GET, POST, and PUT operations.

The library is very simple. To access the Socrata API, you first instantiate a "Socrata" object, passing in the API base URL for the data site you wish to access. The Base URL is always the URL for the root of the datasite (ex: http://www.socrata.com or http://data.medicare.gov). Then you can use its included methods to make simple API calls:
## Installation

Add the following to your project's `composer.json`:

```json
"require": {
"socrata/soda-php": "*"
}
```

If not using composer, simply require the `socrata\soda\Client` class:
```php
$socrata = new Socrata("http://data.medicare.gov");
$response = $socrata->get("/resource/abcd-2345.json");
require '{install_dir}/src/Client.php';
```

The library is very simple. To access the Socrata API, you first instantiate a "socrata\soda\Client" object, passing in the API base URL for the data site you wish to access. The Base URL is always the URL for the root of the datasite (ex: http://www.socrata.com or http://data.medicare.gov). Then you can use its included methods to make simple API calls:

```php
use socrata\soda\Client;
$sodaClient = new Client("http://data.medicare.gov");
$response = $sodaClient->get("/resource/abcd-2345.json");
```

## Querying

[Simple filters](http://dev.socrata.com/docs/filtering.html) and [SoQL Queries](http://dev.socrata.com/docs/queries.html) can be passed as a parameter to the `get` function:

```php
$socrata = new Socrata("https://data.austintexas.gov", $app_token);
use socrata\soda\Client;
$sodaClient = new Client("https://data.austintexas.gov", $app_token);

$params = array("\$where" => "within_circle(location, $latitude, $longitude, $range)");

$response = $socrata->get("/resource/$view_uid.json", $params);
$response = $sodaClient->get("/resource/$view_uid.json", $params);
```

## Publishing

To use the library to publish data you can use the PUT (replace) or POST (upsert) methods:

```php
$socrata = new Socrata("https://data.medicare.gov", $app_token, $user_name, $password);
use socrata\soda\Client;
$sodaClient = new Client("https://data.medicare.gov", $app_token, $user_name, $password);

// Publish data via 'upsert'
$response = $socrata->post("/resource/abcd-2345.json", $data_as_json);
$response = $sodaClient->post("/resource/abcd-2345.json", $data_as_json);

// Publish data via 'replace'
$response = $socrata->put("/resource/abcd-2345.json", $data_as_json);
$response = $sodaClient->put("/resource/abcd-2345.json", $data_as_json);
```

The library also includes a simple example application, which retrieves rows from a dataset and dumps them in a simple table.

## Development and Testing

Unit testing uses the standard `PHPUnit` library. Please add a failing test before making any modifications. Run tests with the following command:

```bash
$ vendor/bin/phpunit
```
21 changes: 18 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
{
"name": "socrata/soda-php",
"description": "A simple library to make it easier to access SODA services from PHP",
"keywords": ["SODA", "socrata"],
"keywords": ["SODA", "socrata", "open data", "sdk"],
"homepage": "http://dev.socrata.com",
"license": "Apache-2.0",
"require": {
"php": "~5.3",
"ext-curl": "*"
"ext-curl": "*",
"ext-json": "*"
},
"require-dev": {
"phpunit/phpunit": "@stable"
},
"autoload": {
"files": ["public/socrata.php"]
"psr-4": {
"socrata\\soda\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"socrata\\soda\\test\\": "test"
}
},
"archive": {
"exclude": ["public", "test"]
}
}
32 changes: 32 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://phpunit.de/phpunit.xsd"
bootstrap="./test/config/bootstrap.php" backupGlobals="false" verbose="true" colors="true">

<testsuites>
<testsuite name="soda">
<directory suffix="Test.php">./test</directory>
</testsuite>
</testsuites>

<logging>
<log type="coverage-text" target="php://stdout" showUncoveredFiles="false"
lowUpperBound="50" highLowerBound="80" />
<log type="coverage-clover" target="./build/logs/clover.xml" />
</logging>

<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
<exclude>
<directory>./vendor</directory>
<directory>./test</directory>
</exclude>
</whitelist>
</filter>

<php>
<const name="PHPUNIT_TESTSUITE" value="true" />
</php>

</phpunit>
84 changes: 42 additions & 42 deletions public/index.php
Original file line number Diff line number Diff line change
@@ -1,67 +1,67 @@
<?php
require_once("socrata.php");
if (file_exists("../vendor/autoload.php")) {
require_once "../vendor/autoload.php";
} else {
require_once "../src/Client.php";
}

$view_uid = "ykw4-j3aj";
$root_url = "https://data.austintexas.gov";
$app_token = "B0ixMbJj4LuQVfYnz95Hfp3Ni";
$response = NULL;
use socrata\soda\Client;

$latitude = array_get("latitude", $_POST);
$longitude = array_get("longitude", $_POST);
$range = array_get("range", $_POST);
// Convenience functions
function array_get($needle, array $haystack, $default = NULL) {
return isset($haystack[$needle]) ? $haystack[$needle] : $default;
}

if($latitude != NULL && $longitude != NULL && $range != NULL) {
// Create a new unauthenticated client
$socrata = new Socrata($root_url, $app_token);
function pre_dump($var) {
echo "<pre>" . print_r($var) . "</pre>";
}

$params = array("\$where" => "within_circle(location, $latitude, $longitude, $range)");
$view_uid = "y9us-9xdf";
$root_url = "https://data.medicare.gov";
$footnote = array_get("footnote", $_POST);

$response = $socrata->get("/resource/$view_uid.json", $params);
}
// Create a new unauthenticated client
$sodaClient = new Client($root_url);
$params = array();

if (!empty($footnote)) {
$params['$where'] = "footnote=$footnote";
}

$response = $sodaClient->get("/resource/{$view_uid}.json", $params);
?>
<html>
<head>
<title>Austin Dangerous Dogs</title>
<title>Medicare Footnote Crosswalk</title>
</head>
<body>
<h1>Austin Dangerous Dogs</h1>

<?php if($response == NULL) { ?>
<form action="index.php" method="POST">
<p>Try 30.27898, -97.68351 with a range of 1000 meters</p>

<label for="latitude">Latitude</label>
<input type="text" name="latitude" size="10" value="30.27898"/><br/>

<label for="longitude">Longitude</label>
<input type="text" name="longitude" size="10" value="-97.68351"/><br/>

<label for="range">Range</label>
<input type="text" name="range" size="10" value="1000"/><br/>

<input type="submit" value="Submit"/>
</form>
<?php } else { ?>
<h1>Medicare Footnote Crosswalk</h1>
<p><?= $root_url . "/resource/{$view_uid}.json" ?></p>
<form method="POST">
<label>Footnote: <input type="number" name="footnote" value="<?= $footnote ?>" /></label>
<input type="submit" value="Search" />
</form>
<?php if (!isset($response['error'])) : ?>
<h2>Results</h2>

<?# Create a table for our actual data ?>
<table border="1">
<tr>
<th>Description</th>
<th>Address</th>
<th>Footnote</th>
<th>Footnote Text</th>
</tr>
<?# Print rows ?>
<?php foreach($response as $row) { ?>
<?php foreach($response as $row) : ?>
<tr>
<td><?= $row["description_of_dog"] ?></td>
<td><?= $row["address"] ?></td>
<td><?= $row["footnote"] ?></td>
<td><?= $row["footnote_text"] ?></td>
</tr>
<?php } ?>
<?php endforeach; ?>
</table>
<?php endif; ?>

<h3>Raw Response</h3>
<pre><?= var_dump($response) ?></pre>
<?php } ?>
<h3>Raw Response</h3>
<pre><?= var_dump($response) ?></pre>
</body>
</html>

Loading

0 comments on commit febe86e

Please sign in to comment.