Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

refactorize. Close #3

  • Loading branch information...
commit d22eed1853ae41899d0cb549b5482471c1320a87 1 parent 39cc620
@mgallego authored
View
81 Adapter/Widget.php
@@ -6,47 +6,44 @@
class Widget extends WidgetHandler{
- public function getLastValues($widgetId, $count = null){
- $apiPath = $this->getPullApiPath() . $widgetId . '/last/';
- if ($count){
- $apiPath = $apiPath . "?count={$count}";
- }
- return $this->getSerializer()->decode($this->callApi($apiPath, 'GET'), 'json');
- }
-
- public function findBySeconds($widgetId, $seconds){
- $apiPath = $this->getPullApiPath() . $widgetId . "/since?seconds={$seconds}";
- return $this->getSerializer()->decode($this->callApi($apiPath, 'GET'), 'json');
- }
+
+ public function getLastValues($widgetId, $count = 3){
+ $this->callApi($this->getApiPath('pull', array($widgetId, "/last/?count={$count}")),'GET');
+ }
+
+ public function findBySeconds($widgetId, $seconds){
+ $this->callApi($this->getApiPath('pull', array($widgetId, "/since?seconds={$seconds}")),'GET');
+ }
- public function findByTimespan($widgetId, $timespan, $timezone){
- $apiPath = $this->getPullApiPath() . $widgetId . "/timespan?timespan={$timespan}&timezone={$timezone}";
- return json_decode($this->callApi($apiPath, 'GET'));
- }
-
- public function addToCounter($widgetId)
- {
- $currentValue = 0;
- $currentWidgetData = $this->getLastValues($widgetId, 1);
-
- if ($currentWidgetData['data']){
- $currentValue = $currentWidgetData['data'][0]['value'];
- }
-
- $widgetData = array($widgetId => array('value' => $currentValue + 1 ));
- $this->setData($widgetData);
- $this->push();
- }
-
- public function setData($data){
- $this->data = $data;
- }
-
- public function appendData($data){
- $this->data += $data;
- }
-
- public function getData(){
- return $this->data;
- }
+ public function findByTimespan($widgetId, $timespan, $timezone){
+ $this->callApi($this->getApiPath('pull', array($widgetId, "/timespan?timespan={$timespan}&timezone={$timezone}" )),'GET');
+ }
+
+ public function addToCounter($widgetId)
+ {
+ $currentValue = 0;
+ $this->getLastValues($widgetId, 1);
+ $currentWidgetData = $this->getArrayResponse();
+
+ if ($currentWidgetData[0]['data']){
+ $currentValue = $currentWidgetData[0]['data'][0]['value'];
+ }
+
+ $widgetData = array($widgetId => array('value' => $currentValue + 1 ));
+ $this->setData($widgetData);
+ $this->push();
+ }
+
+
+ public function setData($data){
+ $this->data = $data;
+ }
+
+ public function appendData($data){
+ $this->data += $data;
+ }
+
+ public function getData(){
+ return $this->data;
+ }
}
View
36 Adapter/WidgetHandler.php
@@ -23,6 +23,9 @@ class WidgetHandler{
private $pullApiPath = 'https://pull.ducksboard.com/values/';
protected $data;
protected $connector;
+ protected $response;
+ private $exitFormat = 'json';
+ private $inputFormat = 'json';
public function __construct(Connector $connector){
@@ -35,14 +38,12 @@ public function push(){
$response = array();
foreach ($this->data as $widgetId => $widgetData){
try{
- $retData = $this->callApi($this->getApiPath('push', array($widgetId)), 'POST', $serializer->serialize($widgetData, 'json'));
- $response[] = $serializer->decode($retData, 'json');
+ $this->callApi($this->getApiPath('push', array($widgetId)), 'POST', $serializer->serialize($widgetData, $this->inputFormat));
}
catch(\ErrorException $e){
- throw new DucksboardPushException($apiPath, $e);
+ throw new DucksboardPushException($this->getApiPath('push', array($widgetId)), $e);
}
}
- return $response;
}
@@ -56,9 +57,8 @@ private function createConnector($apiPath, $method){
public function callApi($apiPath, $method, $inputData = null){
$connector = $this->createConnector($apiPath, $method);
- $response = $this->sendData($connector, $inputData);
+ $this->setResponse($this->sendData($connector, $inputData));
$connector->close();
- return $response;
}
@@ -99,4 +99,28 @@ public function getApiPath($method = 'push', $parameters){
return $apiPath;
}
+
+ public function getRawResponse(){
+ return $this->response;
+ }
+
+
+ public function getArrayResponse(){
+ $arrayResponse = array();
+ foreach ($this->response as $response){
+ $arrayResponse[] = $this->getSerializer()->decode($response, $this->exitFormat);
+ }
+ return $arrayResponse;
+ }
+
+
+ public function clearResponse(){
+ unset($this->response);
+ }
+
+
+ public function setResponse($response){
+ $this->response[] = $response;
+ }
+
}
View
32 Tests/Adapter/WidgetHandlerTest.php
@@ -2,18 +2,38 @@
namespace SFM\DucksboardBundle\Tests\Adapter;
-use SFM\DucksboardBundle\Adapter\WidgetHandler;
-use SFM\DucksboardBundle\Connection\Connector;
-
class WidgetHanlderTest extends \PHPUnit_Framework_TestCase
{
+ private $widget;
+ private $connector;
+
+ public function setUp(){
+ $this->connector = $this->getMock('SFM\DucksboardBundle\Connection\Connector');
+ $this->widget = $this->getMockForAbstractClass('SFM\DucksboardBundle\Adapter\Widget', array($this->connector));
+ }
+
public function testGetApiPath(){
- $widgetHandler = new WidgetHandler($connector = new Connector());
$parameters = array('1');
- $this->assertEquals($widgetHandler->getApiPath('push', $parameters), 'https://push.ducksboard.com/values/1');
+ $this->assertEquals($this->widget->getApiPath('push', $parameters), 'https://push.ducksboard.com/values/1');
$parameters = array('2','/last/?counter=1');
- $this->assertEquals($widgetHandler->getApiPath('pull', $parameters), 'https://pull.ducksboard.com/values/2/last/?counter=1');
+ $this->assertEquals($this->widget->getApiPath('pull', $parameters), 'https://pull.ducksboard.com/values/2/last/?counter=1');
+
+ }
+
+
+ public function testGetRawResponse(){
+ $this->widget->setResponse('{"response": "ok"}');
+ $response = array('{"response": "ok"}');
+ $this->assertEquals($this->widget->getRawResponse(), $response);
+ }
+
+
+ public function testGetArrayResponse(){
+ $this->widget->setResponse('{"response": "ok"}');
+ $response = array(array("response" => "ok"));
+ $this->assertEquals($this->widget->getArrayResponse(), $response);
+
}
View
23 Tests/Adapter/WidgetTest.php
@@ -7,24 +7,29 @@
class WidgetTest extends \PHPUnit_Framework_TestCase
{
-
+
+ private $widget;
+ private $connector;
+
+ public function setUp(){
+ $this->connector = $this->getMock('SFM\DucksboardBundle\Connection\Connector');
+ $this->widget = $this->getMockForAbstractClass('SFM\DucksboardBundle\Adapter\Widget', array($this->connector));
+ }
+
public function testSetData(){
- $widget = new Widget($connector = new Connector());
$data = array(1, array('value'=>1));
-
- $widget->setData($data);
- $this->assertEquals($data, $widget->getData());
+ $this->widget->setData($data);
+ $this->assertEquals($data, $this->widget->getData());
}
public function testAppendData(){
- $widget = new Widget($connector = new Connector());
$dataOne = array(1, array('value'=>1));
$dataTwo = array(2, array('value'=>2));
- $widget->setData($dataOne);
- $widget->appendData($dataTwo);
+ $this->widget->setData($dataOne);
+ $this->widget->appendData($dataTwo);
- $this->assertEquals($dataOne+$dataTwo, $widget->getData());
+ $this->assertEquals($dataOne+$dataTwo, $this->widget->getData());
}
Please sign in to comment.
Something went wrong with that request. Please try again.