Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
davedevelopment committed Feb 26, 2012
0 parents commit c3f585f
Show file tree
Hide file tree
Showing 6 changed files with 600 additions and 0 deletions.
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
Sismo Geckoboard Notifier
=========================

What is it?
-----------

I had a couple of hours to spare, so I thought I'd have a crack at creating a
[Sismo](https://github.com/fabpot/Sismo) notifier for the first [Ibuildings
Challenge](http://ibuildings.com/challenge) of 2012. It's a simple notifier that
pushes a message up to a widget on your [Geckoboard](http://geckoboard.com)

I've used a hacky way to send the HTTP POST in order to keep this dependency
free.

Usage
-----

First up, you need a Geckoboard account, one with the Push API enabled (I had to
ask, I assume at some point it will be the default). Add a custom text widget to
your board, select Push as the method and give it an API key. The system should
provide you a URL to use for the widget.

In your Sismo config

``` php
<?php
$notifer = new Davedevelopment\Sismo\GeckoboardNotifier(
"your_api_key",
"your_widget_url"
);

```

Try running sismo

You can customise the display by passing a third parameter to the constructor,
either a string or a callback that would take a `Sismo\Commit` instance

Todo
----

* <del>Could optionally take a widget url rather than key, in case they change things</del>
* <del>Take Buzz out</del>

Copyright
---------

Copyright (c) 2012 Dave Marshall. See LICENCE for further details
14 changes: 14 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"require": {
"php": ">=5.3.2"
},

"suggest": {
"sismo/sismo": "dev-master"
},

"autoload": {
"psr-0": { "Davedevelopment\\Sismo": "src/" }
}

}
90 changes: 90 additions & 0 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>

<!-- http://www.phpunit.de/manual/current/en/appendixes.configuration.html -->
<phpunit
backupGlobals = "false"
backupStaticAttributes = "false"
colors = "true"
convertErrorsToExceptions = "true"
convertNoticesToExceptions = "true"
convertWarningsToExceptions = "true"
processIsolation = "false"
stopOnFailure = "false"
syntaxCheck = "false"
bootstrap = "vendor/.composer/autoload.php" >

<testsuites>
<testsuite name="Sismo Geckoboard Notifier Suite">
<directory>tests/</directory>
</testsuite>
</testsuites>

<!--
<logging>
<log type="coverage-clover" target="build/logs/clover.xml" />
<log type="coverage-html" target="build/coverage" title="Sismo Geckoboard Notifier" />
<log type="junit" target="build/logs/junit.xml" />
</logging>
<filter>
<whitelist>
<directory>src</directory>
</whitelist>
</filter>
-->

</phpunit>
196 changes: 196 additions & 0 deletions src/Davedevelopment/Sismo/GeckoboardNotifier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
<?php

namespace Davedevelopment\Sismo;

use Sismo\Notifier;
use Sismo\Commit;
use Buzz\Browser;

class GeckoboardNotifier extends Notifier
{
/**
* @var string
*/
protected $apiKey;

/**
* @var string
*/
protected $widgetKey;

/**
* @var String|Callable
*/
protected $format = null;

/**
* @var callable
*
* Just for testing...
*/
protected $poster = null;

/**
* Constructor
*
* @param string $apiKey
* @param string $widget
* @param string|callable $format
*/
public function __construct($apiKey, $widget, $format = null)
{
$this->apiKey = $apiKey;
if (0 !== strpos($widget, 'http')) {
$widget = "https://push.geckoboard.com/v1/send/" . $widget;
}
$this->widgetUrl = $widget;

if ($format !== null) {
$this->setFormat($format);
}
}

/**
* {@inheritDoc}
*/
public function notify(Commit $commit)
{
$type = $commit->getStatus() == 'failed' ? 1 : 2;

$data = array(
"apiKey" => $this->apiKey,
"data" => array(
"item" => array(
array(
"text" => $this->getMessage($commit),
"type" => $type,
),
),
),
);

$response = $this->send($this->widgetUrl, array('Content-type' => 'application/json'), json_encode($data));
return;
}

/**
* Send a request
*
* @param string $url
* @param array $headers
* @param string $data
*
* @return
*/
protected function send($url, array $headers = array(), $data)
{
if ($this->poster !== null) {
return call_user_func($this->poster, $url, $headers, $data);
}

/**
* See
* http://wezfurlong.org/blog/2006/nov/http-post-from-php-without-curl/
*/
$params = array('http' => array(
'method' => 'POST',
'content' => $data
));
if (!empty($headers)) {
array_walk($headers, function(&$value, $key) {
$value = $key . ':' . $value;
});
$params['http']['header'] = implode("\n", $headers);
}
$ctx = stream_context_create($params);
$fp = @fopen($url, 'rb', false, $ctx);
if (!$fp) {
return;
}
$response = @stream_get_contents($fp);

return;
}


/**
* Get format
*
* @return string|callable $format
*/
public function getFormat()
{
return $this->format;
}

/**
* Set format. A custom string (with placeholders, as described in
* Sismo\Notifier, or a callback, taking a Commit as it's only parameter
*
* @param string|callable $format
* @return GeckoboardNotifier
*/
public function setFormat($format)
{
if (!is_string($format) && !is_callable($format)) {
throw new \InvalidArgumentException(
sprintf("\$format should be string or callable, %s given", gettype($format))
);
}

$this->format = $format;
return $this;
}

/**
* Set poster, only really here for testing so we can avoid an actual HTTP
* post
*
* @param callable $poster
* @return GeckoboardNotifier
*/
public function setPoster($poster)
{
if (!is_callable($poster)) {
throw new \InvalidArgumentException(
sprintf("\$poster should be callable, %s given", gettype($poster))
);
}

$this->poster = $poster;
return $this;
}

/**
* Get Message
*
* @param Commit $commit
* @return string
*/
protected function getMessage(Commit $commit)
{
if ($this->format == null) {
return $this->getDefaultMessage($commit);
}

if (is_string($this->format)) {
return $this->format($this->format, $commit);
}

return call_user_func($this->format, $commit);
}

/**
* Get default message
*
* @param Commit $commit
* @return string
*/
protected function getDefaultMessage(Commit $commit)
{
return $this->format("[%STATUS%]\n%message%\n%author%", $commit);
}

}


Loading

0 comments on commit c3f585f

Please sign in to comment.