Skip to content

Commit fdd61bf

Browse files
committed
Merge pull request #9 from databox/last-push-update
"Lastpushes" update
2 parents 7b61a55 + 8fd284c commit fdd61bf

3 files changed

Lines changed: 35 additions & 12 deletions

File tree

example.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@
44

55
use Databox\Client;
66

7-
$c = new Client('adxg1kq5a4g04k0wk0s4wkssow8osw84');
7+
$token = getenv("DATABOX_PUSH_TOKEN");
8+
9+
if (!$token) {
10+
$token = 'adxg1kq5a4g04k0wk0s4wkssow8osw84';
11+
}
12+
13+
$c = new Client($token);
814

915
$ok = $c->push('sales', 203);
1016
if ($ok) {

src/Databox/Client.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ public function __construct($pushToken = null)
1010
{
1111
parent::__construct([
1212
'base_uri' => 'https://push2new.databox.com',
13-
'headers' => [
14-
'User-Agent' => 'databox-php/1.2',
13+
'headers' => [
14+
'User-Agent' => 'databox-php/1.3',
1515
'Content-Type' => 'application/json',
16-
'Accept' => 'application/json'
16+
'Accept' => 'application/json'
1717
],
1818
'auth' => [$pushToken, '', 'Basic']
1919
]);
@@ -24,6 +24,11 @@ public function rawPush($path = '/', $data = [])
2424
return json_decode($this->post($path, $data)->getBody(), true);
2525
}
2626

27+
public function rawGet($path)
28+
{
29+
return json_decode($this->get($path)->getBody(), true);
30+
}
31+
2732
private function processKPI($key, $value, $date = null, $attributes = null)
2833
{
2934
$data = [sprintf('$%s', trim($key, '$')) => $value];
@@ -62,6 +67,6 @@ public function insertAll($kpis)
6267

6368
public function lastPush($n = 1)
6469
{
65-
return $this->rawPush(sprintf('/lastpushes/%d', $n));
70+
return $this->rawGet(sprintf('/lastpushes/%d', $n));
6671
}
6772
}

tests/Databox/Tests/ClientTest.php

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,22 @@ class ClientTest extends \PHPUnit_Framework_TestCase
1010
public function __construct()
1111
{
1212
$this->client = $this->getMockBuilder('Databox\Client')
13-
->setMethods(['rawPush'])
13+
->setMethods(['rawPush', 'rawGet'])
1414
->getMock();
1515
}
1616

1717
public function testClientCorrectOptions()
1818
{
19-
$mimeType = 'application/json';
19+
$mimeType = 'application/json';
2020
$userAgent = 'databox-php';
21-
$token = 'test-token';
22-
$baseUrl = 'https://push2new.databox.com';
21+
$token = 'test-token';
22+
$baseUrl = 'https://push2new.databox.com';
2323

2424
$client = new Client($token);
2525
$this->assertEquals($mimeType, $client->getConfig('headers')['Content-Type']);
2626
$this->assertEquals($userAgent, substr($client->getConfig('headers')['User-Agent'], 0, 11));
2727
$this->assertEquals($mimeType, $client->getConfig('headers')['Accept']);
28-
$this->assertEquals($baseUrl, (string) $client->getConfig('base_uri'));
28+
$this->assertEquals($baseUrl, (string)$client->getConfig('base_uri'));
2929
$this->assertEquals($token, $client->getConfig('auth')[0]);
3030
}
3131

@@ -35,16 +35,28 @@ public function testRawPush()
3535
->setMethods(['post'])
3636
->getMock();
3737

38-
$json = '{"status":"ok"}';
38+
$json = '{"status":"ok"}';
3939
$response = new Response(200, [], $json);
4040
$client->method('post')->willReturn($response);
4141

4242
$this->assertEquals($json, json_encode($client->rawPush()));
4343
}
4444

45+
public function testRawGet()
46+
{
47+
$client = $this->getMockBuilder('Databox\Client')
48+
->setMethods(['get'])
49+
->getMock();
50+
51+
$json = '[]';
52+
$response = new Response(200, [], $json);
53+
$client->method('get')->willReturn($response);
54+
$this->assertEquals($json, json_encode([]));
55+
}
56+
4557
public function testLastPush()
4658
{
47-
$this->client->method('rawPush')->willReturn([
59+
$this->client->method('rawGet')->willReturn([
4860
[], []
4961
]);
5062

0 commit comments

Comments
 (0)