Skip to content

Commit 0d0e9f3

Browse files
committed
Initial commit.
Rewrote my simple Sifter API class (https://gist.github.com/mloberg/3836369) into a library. It's a lot more powerful and easier to use.
0 parents  commit 0d0e9f3

File tree

12 files changed

+332
-0
lines changed

12 files changed

+332
-0
lines changed

.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Things you might find on a Mac
2+
.DS_Store
3+
._*
4+
.Spotlight-V100
5+
.Trashes
6+
7+
# Composer
8+
vendor
9+
composer.phar
10+
composer.lock

LICENSE

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright (c) 2013 Matthew Loberg
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Sifter PHP
2+
3+
Sifter PHP is an API wrapper for the Sifter API written in PHP.
4+
5+
## Installing
6+
7+
Sifter PHP is available through Packagist.
8+
9+
"require": {
10+
"sifter/sifter": "version"
11+
}
12+
13+
## Usage
14+
15+
See *examples* for usage.
16+
17+
## Documentation
18+
19+
Coming soon.

composer.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "sifter/sifter",
3+
"type": "library",
4+
"description": "Sifter API",
5+
"keywords": ["sifter"],
6+
"require": {
7+
"php": ">=5.3.0"
8+
},
9+
"authors": [
10+
{
11+
"name": "Matthew Loberg",
12+
"email": "[email protected]"
13+
}
14+
],
15+
"autoload": {
16+
"psr-0": { "Sifter": "lib/" }
17+
}
18+
}

examples/basic.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
require 'vendor/autoload.php';
4+
5+
$host = 'http://example.sifterapp.com';
6+
$token = 'abc123';
7+
8+
$sifter = new Sifter\Sifter($host, $token);
9+
10+
$projects = $sifter->projects();
11+
12+
foreach ($projects->open() as $project) {
13+
echo $project->id() . ": " . $project->name() . "\n";
14+
15+
echo " Milestones:\n";
16+
foreach ($project->milestones() as $milestone) {
17+
echo " - " . $milestone->name() . "\n";
18+
}
19+
20+
echo " Categories:\n";
21+
foreach ($project->categories() as $category) {
22+
echo " - " . $category->name() . "\n";
23+
}
24+
25+
echo " Poeple:\n";
26+
foreach ($project->people() as $person) {
27+
echo " - " . $person->username() . "\n";
28+
}
29+
30+
echo " Issues:\n";
31+
foreach ($project->issues()->open()->high()->get() as $issue) {
32+
echo " - " . $issue->subject() . "\n";
33+
}
34+
}

lib/Sifter/Issues.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace Sifter;
4+
5+
use Sifter\Resource\Resource;
6+
7+
class Issues
8+
{
9+
10+
private $id;
11+
private $params = array();
12+
13+
public function __construct($id)
14+
{
15+
$this->id = $id;
16+
}
17+
18+
public function get($params = array())
19+
{
20+
$endpoint = "/api/projects/" . $this->id . "/issues";
21+
$issues = Request::make($endpoint, $this->params + $params);
22+
$this->params = array();
23+
return Resource::make($issues['issues']);
24+
}
25+
26+
public function __call($name, $args)
27+
{
28+
$statuses = array(
29+
'open' => 1, 'reopened' => 2,
30+
'resolved' => 3, 'closed' => 4
31+
);
32+
$priorities = array(
33+
'critical' => 1, 'high' => 2, 'normal' => 3,
34+
'low' => 4, 'trivial' => 5
35+
);
36+
if (in_array($name, array_keys($statuses))) {
37+
$status = $statuses[$name];
38+
if (isset($this->params['s'])) {
39+
$status = $this->params['s'] . '-' . $status;
40+
}
41+
$this->params['s'] = $status;
42+
} elseif (in_array($name, array_keys($priorities))) {
43+
$priority = $priorities[$name];
44+
if (isset($this->params['p'])) {
45+
$priority = $this->params['p'] . '-' . $priority;
46+
}
47+
$this->params['p'] = $priority;
48+
} elseif ($name === 'search') {
49+
$this->params['q'] = $args[0];
50+
} else {
51+
$this->params[$name] = $args[0];
52+
}
53+
return $this;
54+
}
55+
56+
}

lib/Sifter/Projects.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Sifter;
4+
5+
use Sifter\Resource\Project;
6+
7+
class Projects
8+
{
9+
10+
public function open()
11+
{
12+
$endpoint = '/api/projects';
13+
$projects = Request::make($endpoint);
14+
return Project::make($projects['projects']);
15+
}
16+
17+
public function all()
18+
{
19+
$endpoint = '/api/projects';
20+
$projects = Request::make($endpoint, array('all' => true));
21+
return Project::make($projects['projects']);
22+
}
23+
24+
public function get($id)
25+
{
26+
$endpoint = '/api/projects/' . $id;
27+
return new Project(Request::make($endpoint));
28+
}
29+
30+
}

lib/Sifter/Request.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace Sifter;
4+
5+
class RequestException extends \Exception { }
6+
7+
class Request
8+
{
9+
10+
private static $host;
11+
private static $token;
12+
13+
public static function config($host, $token)
14+
{
15+
static::$host = $host;
16+
static::$token = $token;
17+
}
18+
19+
public static function make($endpoint, $params = array())
20+
{
21+
$headers = array(
22+
'X-Sifter-Token: ' . static::$token,
23+
'Accept: application/json',
24+
);
25+
26+
$query = http_build_query($params);
27+
$request_url = 'https://' . static::$host.$endpoint . '?' . $query;
28+
29+
$ch = curl_init();
30+
curl_setopt($ch, CURLOPT_URL, $request_url);
31+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
32+
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
33+
34+
$raw = curl_exec($ch);
35+
curl_close($ch);
36+
$response = json_decode($raw, true);
37+
38+
if (is_null($response)) {
39+
throw new RequestException(strip_tags($raw));
40+
} elseif (isset($response['error'])) {
41+
throw new RequestException($response['error'] . ': ' . $response['detail']);
42+
}
43+
return $response;
44+
}
45+
46+
}

lib/Sifter/Resource/Issue.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Sifter\Resource;
4+
5+
class Issue extends Resource
6+
{
7+
8+
public function id()
9+
{
10+
return basename($this->data['url']);
11+
}
12+
13+
public function details()
14+
{
15+
$endpoint = parse_url($this->data['api_url'], PHP_URL_PATH);
16+
$details = Request::make($endpoint);
17+
return Resource::make($details['issue']);
18+
}
19+
20+
}

lib/Sifter/Resource/Project.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace Sifter\Resource;
4+
5+
use Sifter\Request;
6+
use Sifter\Issues;
7+
8+
class Project extends Resource
9+
{
10+
11+
public function id()
12+
{
13+
return basename($this->data['url']);
14+
}
15+
16+
public function milestones()
17+
{
18+
$endpoint = "/api/projects/" . $this->id() . "/milestones";
19+
$milestones = Request::make($endpoint);
20+
return Resource::make($milestones['milestones']);
21+
}
22+
23+
public function categories()
24+
{
25+
$endpoint = "/api/projects/" . $this->id() . "/categories";
26+
$categories = Request::make($endpoint);
27+
return Resource::make($categories['categories']);
28+
}
29+
30+
public function people()
31+
{
32+
$endpoint = "/api/projects/" . $this->id() . "/people";
33+
$people = Request::make($endpoint);
34+
return Resource::make($people['people']);
35+
}
36+
37+
public function issues($params = array())
38+
{
39+
return new Issues($this->id());
40+
}
41+
42+
}

0 commit comments

Comments
 (0)