Skip to content

Commit

Permalink
Merge pull request #2 from databox/attributes
Browse files Browse the repository at this point in the history
Additional attributes support
  • Loading branch information
otobrglez committed Jul 22, 2015
2 parents 5c4910d + 999d5cf commit 12307d7
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 18 deletions.
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,13 @@ Pushing data directly to Databox with help of `push` method:
```ruby
client = Databox::Client.new

client.push('sales.total', 3000)
client.push('temp.boston', 52.0)
client.push('temp.boston', 52.0, '2015-01-01 17:00:00')
client.push(key: 'sales.total', value: 3000)
client.push(key: 'temp.boston', value: 52.0)
client.push(key: 'temp.boston', value: 52.0, date: '2015-01-01 17:00:00')
client.push(key: 'temp.boston', value: 52.0, attributes: {
location: 'boston-south'
})

```

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

## Author & License

- [Oto Brglez](https://github.com/otobrglez)
- Comes with `MIT` license and terms

[travis-badge]: https://secure.travis-ci.org/databox/databox-ruby.png?branch=v2
[travis]: http://travis-ci.org/databox/databox-ruby
17 changes: 14 additions & 3 deletions lib/databox/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class Databox::Client
debug_output if [1, "1"].include?(ENV["HTTPARTY_DEBUG"])
default_timeout 1 if ENV["DATABOX_MODE"] == "test"

attr_accessor :last_push_content

def initialize
Databox.configure unless Databox.configured?

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

options["$#{(options['key'] || options[:key])}"] = options['value'] || options[:value]
options.delete_if { |k, _| [:key, 'key', :value, 'value'].include?(k) }

attributes = options[:attributes] || options['attributes']
unless attributes.nil?
[:attributes, 'attributes'].each {|k| options.delete(k) }
attributes.each { |k,v| options[k] = v }
end

options
end

def push(key, value, date=nil)
raw_push('/', [process_kpi({key: key, value: value, date: date})])['status'] == 'ok'
def push(kpi={})
self.last_push_content = raw_push('/', [process_kpi(kpi)])
self.last_push_content['status'] == 'ok'
end

def insert_all(rows=[])
raw_push('/', rows.map {|r| process_kpi(r) })['status'] == 'ok'
self.last_push_content = raw_push('/', rows.map {|r| process_kpi(r) })
self.last_push_content['status'] == 'ok'
end

def last_push(n=1)
Expand Down
2 changes: 1 addition & 1 deletion lib/databox/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Databox
VERSION = '0.2.0'
VERSION = '0.2.1'
end
41 changes: 30 additions & 11 deletions spec/databox/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,46 @@
end

allow_any_instance_of(Databox::Client).to receive(:raw_push)\
.and_return({'status'=> 'ok'})
.and_return({'status' => 'ok'})
end

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

context 'push' do
it { expect { client.push(nil, nil) }.to raise_exception }
it { expect { client.push('sales.total', nil) }.to raise_exception }
it { expect { client.push(nil, 3000) }.to raise_exception }
it { expect(client.push('sales.total', 2000)).to eq true }
it { expect { client.push(nil) }.to raise_exception }
it { expect { client.push(key: 'sales.total', value: nil) }.to raise_exception }
it { expect { client.push(key: nil, value: 3000) }.to raise_exception }
it { expect(client.push(key: 'sales.total', value: 2000)).to eq true }
end

context 'push w/ attributes' do
it {
payload = {
key: 'test',
value: 200,
attributes: {
'me': 'Oto'
}
}

expect(client).to receive(:raw_push)
.with('/', [
{"$test" => 200, :me => "Oto"}
])
.once.and_call_original
expect(client.push(payload)).to eq true
}
end

context 'insert_all' do
it { expect { client.insert_all([
{key: 'temp.lj'},
{key: 'temp.ljx', value: 60.3},
])}.to raise_exception }
{key: 'temp.lj'},
{key: 'temp.ljx', value: 60.3},
]) }.to raise_exception }

it { expect(client.insert_all([
{key: 'temp.ljx', value: 4.3},
{key: 'temp.ljx', value: 1.3, date: '2015-01-01 09:00:00'},
])).to eq true }
{key: 'temp.ljx', value: 4.3},
{key: 'temp.ljx', value: 1.3, date: '2015-01-01 09:00:00'},
])).to eq true }
end
end

0 comments on commit 12307d7

Please sign in to comment.