Skip to content

Commit f7e1dec

Browse files
committed
Merge pull request #3 from databox/feature/attributes
add attributes support
2 parents e268c02 + 96973bf commit f7e1dec

3 files changed

Lines changed: 17 additions & 5 deletions

File tree

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,13 @@ $c->insertAll([
5353
['sales', 103, '2015-01-01 17:00:00'],
5454
]);
5555

56+
// Or push some attributes
57+
$ok = $c->push('sales', 203, null, [
58+
'city' => 'Boston'
59+
]);
60+
5661
print_r(
57-
$c->lastPush(2)
62+
$c->lastPush(3)
5863
);
5964

6065
```

src/Databox/Client.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,24 @@ public function rawPush($path = '/', $data = [])
2424
return $this->post($path, $data)->json();
2525
}
2626

27-
private function processKPI($key, $value, $date = null)
27+
private function processKPI($key, $value, $date = null, $attributes = null)
2828
{
29-
$data = [sprintf('$%s', $key) => $value];
29+
$data = [sprintf('$%s', trim($key, '$')) => $value];
3030
if (!is_null($date)) {
3131
$data['date'] = $date;
3232
}
3333

34+
if (is_array($attributes)) {
35+
$data = $data + $attributes;
36+
}
37+
3438
return $data;
3539
}
3640

37-
public function push($key, $value, $date = null)
41+
public function push($key, $value, $date = null, $attributes = null)
3842
{
3943
$response = $this->rawPush('/', [
40-
'json' => ['data' => [$this->processKPI($key, $value, $date)]]
44+
'json' => ['data' => [$this->processKPI($key, $value, $date, $attributes)]]
4145
]);
4246

4347
return $response['status'] === 'ok';

tests/Databox/Tests/ClientTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ public function testPush()
2929

3030
$this->assertTrue($this->client->push('sales', 53.2));
3131
$this->assertTrue($this->client->push('sales', 40, '2015-01-01 17:00:00'));
32+
$this->assertTrue($this->client->push('sales', 40, '2015-01-01 17:00:00', [
33+
'name' => 'attribute name'
34+
]));
3235
}
3336

3437
public function testFailedPush()

0 commit comments

Comments
 (0)