Skip to content

Commit 12307d7

Browse files
committed
Merge pull request #2 from databox/attributes
Additional attributes support
2 parents 5c4910d + 999d5cf commit 12307d7

File tree

4 files changed

+56
-18
lines changed

4 files changed

+56
-18
lines changed

README.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,13 @@ Pushing data directly to Databox with help of `push` method:
3535
```ruby
3636
client = Databox::Client.new
3737

38-
client.push('sales.total', 3000)
39-
client.push('temp.boston', 52.0)
40-
client.push('temp.boston', 52.0, '2015-01-01 17:00:00')
38+
client.push(key: 'sales.total', value: 3000)
39+
client.push(key: 'temp.boston', value: 52.0)
40+
client.push(key: 'temp.boston', value: 52.0, date: '2015-01-01 17:00:00')
41+
client.push(key: 'temp.boston', value: 52.0, attributes: {
42+
location: 'boston-south'
43+
})
44+
4145
```
4246

4347
Inserting multiple matrices with one `insert_all`:
@@ -72,6 +76,10 @@ client.last_push
7276
- [Databox Web App](https://app.databox.com/)
7377
- [Databox Developers Portal](https://developers.databox.com/)
7478

79+
## Author & License
80+
81+
- [Oto Brglez](https://github.com/otobrglez)
82+
- Comes with `MIT` license and terms
7583

7684
[travis-badge]: https://secure.travis-ci.org/databox/databox-ruby.png?branch=v2
7785
[travis]: http://travis-ci.org/databox/databox-ruby

lib/databox/client.rb

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ class Databox::Client
88
debug_output if [1, "1"].include?(ENV["HTTPARTY_DEBUG"])
99
default_timeout 1 if ENV["DATABOX_MODE"] == "test"
1010

11+
attr_accessor :last_push_content
12+
1113
def initialize
1214
Databox.configure unless Databox.configured?
1315

@@ -43,15 +45,24 @@ def process_kpi(options={})
4345

4446
options["$#{(options['key'] || options[:key])}"] = options['value'] || options[:value]
4547
options.delete_if { |k, _| [:key, 'key', :value, 'value'].include?(k) }
48+
49+
attributes = options[:attributes] || options['attributes']
50+
unless attributes.nil?
51+
[:attributes, 'attributes'].each {|k| options.delete(k) }
52+
attributes.each { |k,v| options[k] = v }
53+
end
54+
4655
options
4756
end
4857

49-
def push(key, value, date=nil)
50-
raw_push('/', [process_kpi({key: key, value: value, date: date})])['status'] == 'ok'
58+
def push(kpi={})
59+
self.last_push_content = raw_push('/', [process_kpi(kpi)])
60+
self.last_push_content['status'] == 'ok'
5161
end
5262

5363
def insert_all(rows=[])
54-
raw_push('/', rows.map {|r| process_kpi(r) })['status'] == 'ok'
64+
self.last_push_content = raw_push('/', rows.map {|r| process_kpi(r) })
65+
self.last_push_content['status'] == 'ok'
5566
end
5667

5768
def last_push(n=1)

lib/databox/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module Databox
2-
VERSION = '0.2.0'
2+
VERSION = '0.2.1'
33
end

spec/databox/client_spec.rb

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,46 @@
88
end
99

1010
allow_any_instance_of(Databox::Client).to receive(:raw_push)\
11-
.and_return({'status'=> 'ok'})
11+
.and_return({'status' => 'ok'})
1212
end
1313

1414
let!(:client) { Databox::Client.new }
1515

1616
context 'push' do
17-
it { expect { client.push(nil, nil) }.to raise_exception }
18-
it { expect { client.push('sales.total', nil) }.to raise_exception }
19-
it { expect { client.push(nil, 3000) }.to raise_exception }
20-
it { expect(client.push('sales.total', 2000)).to eq true }
17+
it { expect { client.push(nil) }.to raise_exception }
18+
it { expect { client.push(key: 'sales.total', value: nil) }.to raise_exception }
19+
it { expect { client.push(key: nil, value: 3000) }.to raise_exception }
20+
it { expect(client.push(key: 'sales.total', value: 2000)).to eq true }
21+
end
22+
23+
context 'push w/ attributes' do
24+
it {
25+
payload = {
26+
key: 'test',
27+
value: 200,
28+
attributes: {
29+
'me': 'Oto'
30+
}
31+
}
32+
33+
expect(client).to receive(:raw_push)
34+
.with('/', [
35+
{"$test" => 200, :me => "Oto"}
36+
])
37+
.once.and_call_original
38+
expect(client.push(payload)).to eq true
39+
}
2140
end
2241

2342
context 'insert_all' do
2443
it { expect { client.insert_all([
25-
{key: 'temp.lj'},
26-
{key: 'temp.ljx', value: 60.3},
27-
])}.to raise_exception }
44+
{key: 'temp.lj'},
45+
{key: 'temp.ljx', value: 60.3},
46+
]) }.to raise_exception }
2847

2948
it { expect(client.insert_all([
30-
{key: 'temp.ljx', value: 4.3},
31-
{key: 'temp.ljx', value: 1.3, date: '2015-01-01 09:00:00'},
32-
])).to eq true }
49+
{key: 'temp.ljx', value: 4.3},
50+
{key: 'temp.ljx', value: 1.3, date: '2015-01-01 09:00:00'},
51+
])).to eq true }
3352
end
3453
end

0 commit comments

Comments
 (0)