databox 0.1.2 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.pryrc +7 -0
  4. data/.ruby-version +1 -1
  5. data/.travis.yml +4 -7
  6. data/README.md +40 -149
  7. data/databox.gemspec +7 -17
  8. data/example.rb +17 -0
  9. data/lib/databox.rb +8 -22
  10. data/lib/databox/client.rb +42 -61
  11. data/lib/databox/configuration.rb +3 -8
  12. data/lib/databox/version.rb +1 -1
  13. data/spec/databox/client_spec.rb +42 -85
  14. data/spec/databox_spec.rb +9 -0
  15. data/spec/spec_helper.rb +3 -32
  16. metadata +26 -160
  17. data/lib/databox/integration.rb +0 -127
  18. data/spec/databox/bar_chart_spec.rb +0 -23
  19. data/spec/databox/big_number_spec.rb +0 -29
  20. data/spec/databox/compare_spec.rb +0 -22
  21. data/spec/databox/funnel_spec.rb +0 -30
  22. data/spec/databox/integration_spec.rb +0 -11
  23. data/spec/databox/interval_spec.rb +0 -23
  24. data/spec/databox/line_chart_spec.rb +0 -23
  25. data/spec/databox/messages_spec.rb +0 -52
  26. data/spec/databox/pie_spec.rb +0 -25
  27. data/spec/databox/pipeline_spec.rb +0 -24
  28. data/spec/databox/progress_spec.rb +0 -20
  29. data/spec/requests/bar_chart_simple.txt +0 -14
  30. data/spec/requests/big_number_simple.txt +0 -14
  31. data/spec/requests/compare_simple.txt +0 -14
  32. data/spec/requests/funnel_simple.txt +0 -14
  33. data/spec/requests/interval_simple.txt +0 -14
  34. data/spec/requests/invalid_push.txt +0 -19
  35. data/spec/requests/line_chart_simple.txt +0 -14
  36. data/spec/requests/logs.txt +0 -14
  37. data/spec/requests/multiple_message.txt +0 -14
  38. data/spec/requests/pie_simple.txt +0 -14
  39. data/spec/requests/pipeline_simple.txt +0 -14
  40. data/spec/requests/progress_simple.txt +0 -14
  41. data/spec/requests/simple_message.txt +0 -14
  42. data/spec/requests/simple_push.txt +0 -14
  43. data/test_rvms.sh +0 -16
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2a6922c503db36ec8c5a0402a96c7a098d6c2e18
4
- data.tar.gz: 5824a170a896105170b2d17318af8ca22878ffd5
3
+ metadata.gz: a6e85fe8361df5f8a6e5e222fded72b065e72a06
4
+ data.tar.gz: 831e85f19a7a8162c5c1bc77d95601f45f8f9807
5
5
  SHA512:
6
- metadata.gz: 23cd2a8298ba629d762508a141fb10ab9d9f38de59a766f9899db1a41d9f08f3710089dbed5b3cc27d5f9669955c0a3187e90732c1e71e33810c7b1b960500bd
7
- data.tar.gz: 4e5d98b5a643cb4e79364fda6f329a32d5d4fd85d0440c4fc2e5c270ac53d5b2d767978bd1a0bf951bf784c3650081da13339c3aac87d4ce6128ab60eadc25ef
6
+ metadata.gz: a175fdc88f0efdd14afed93bb1d3db17edc7714633702a7731f5ce1f390f1db1edbc5f836f654167fc75ef12d8cf4263594fa725b38385ef9d32d58e9359713b
7
+ data.tar.gz: 32e2097b929ea498c8b5205eef0ad9c7ec62b58789fd11dc49fde733caa1a05389a026c70b5a3afcd5beb5ff7c5bfa776410845771b8bd709d0e89f4ee304966
data/.gitignore CHANGED
@@ -1,3 +1,4 @@
1
+ .idea/
1
2
  *.sh
2
3
  !*test_*sh
3
4
  *.gem
data/.pryrc ADDED
@@ -0,0 +1,7 @@
1
+ require 'databox'
2
+
3
+ Databox.configure do |c|
4
+ c.push_token = ENV["DATABOX_PUSH_TOKEN"]
5
+ end
6
+
7
+ client = Databox::Client.new
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- ruby-2.0.0
1
+ ruby-2.2.2
data/.travis.yml CHANGED
@@ -1,7 +1,4 @@
1
- script: "bundle exec rspec"
2
-
3
- install: "travis_retry bundle install"
4
-
5
- rvm:
6
- - 1.9.3
7
- - 2.0.0
1
+ install:
2
+ - "travis_retry bundle install"
3
+ script:
4
+ - "bundle exec rspec --format doc --color"
data/README.md CHANGED
@@ -1,11 +1,9 @@
1
- # Databox
1
+ # Databox bindings for Ruby
2
2
 
3
- [![Gem Version][fury-badge]][fury] [![Build Status][travis-badge]][travis] [![Coverage Status](https://coveralls.io/repos/otobrglez/databox/badge.png?branch=master)](https://coveralls.io/r/otobrglez/databox?branch=master)
3
+ [![Build Status][travis-badge]][travis]
4
4
 
5
5
  Ruby Gem for [Databox](http://databox.com/) - Mobile Executive Dashboard.
6
6
 
7
- - By [Oto Brglez](https://github.com/otobrglez)
8
-
9
7
  ## Installation
10
8
 
11
9
  Add this line to your application's Gemfile:
@@ -20,175 +18,68 @@ Or install it yourself as:
20
18
 
21
19
  $ gem install databox
22
20
 
23
- ## Usage
24
-
25
- Before using Databox gem you have to initialize it with key and token. There are 3 ways how you can do initialization.
26
-
27
- - Within simple initializer
28
-
29
- ```ruby
30
- Databox.configure do |c|
31
- c.token = "YOUR_TOKEN"
32
- c.key = "YOUR_KEY"
33
- end
34
- ```
35
-
36
- - By setting system ```ENV``` variables ```DATABOX_KEY``` and ```DATABOX_TOKEN```
37
-
38
- - If your are using custom integration with different key please use ```id```.
39
-
40
- ```ruby
41
- chart = Databox::LineChart.new "my_line", id: "OTHER_KEY"
42
- ```
43
-
44
- Databox gem support following widgets
45
-
46
- | Widget | Implemented | Example | Databox Documentation
47
- | --------------- | :-----------: | ------------------------------ | ----------------------
48
- | Big Number | Yes | [Example](#big-number) | [Basic Widgets / Big Number](https://developers.databox.com/push/v1/widgets/main/#big-number)
49
- | Compare | Yes | [Example](#compare) | [Basic Widgets / Compare](https://developers.databox.com/push/v1/widgets/main/#compare)
50
- | Internal | Yes | [Example](#interval) | [Basic Widgets / Interval Values](https://developers.databox.com/push/v1/widgets/main/#interval-values)
51
- | Line Chart | Yes | [Example](#line-chart) | [Basic Widgets / Line Chart](https://developers.databox.com/push/v1/widgets/main/#line-chart-and-bar-chart)
52
- | Bar Chart | Yes | [Example](#bar-chart) | [Basic Widgets / Bar Chart](https://developers.databox.com/push/v1/widgets/main/#line-chart-and-bar-chart)
53
- | Pipeline | Yes | [Example](#pipeline) | [Pipeline](https://developers.databox.com/push/v1/widgets/pipeline/)
54
- | Funnel | Yes | [Example](#funnel) | [Funnel](https://developers.databox.com/push/v1/widgets/funnel/)
55
- | Pie | Yes | [Example](#pie) | [Pie](https://developers.databox.com/push/v1/widgets/pie/)
56
- | Progress | Yes | [Example](#progress) | [Progress](https://developers.databox.com/push/v1/widgets/progress/)
57
- | Table | No | |
58
- | Messages | Yes | [Example](#messages) | [Messages](https://developers.databox.com/push/v1/widgets/messages/)
21
+ ## Initialisation
59
22
 
23
+ Before pushing data to Databox you have to initialize it with token.
60
24
 
61
- ### Big Number
62
25
  ```ruby
63
- number = Databox::BigNumber.new "just_number"
64
- number.set 5000
65
- number.save
66
-
67
- # You can set date on initialize
68
- number_dated = Databox::BigNumber.new "just_number", date: "2014-02-03"
69
- # Or when setting number
70
- number_dated.set 10_000, "2014-02-03"
71
- number_dated.save
72
-
26
+ Databox.configure do |c|
27
+ c.push_token = "YOUR_TOKEN"
28
+ end
73
29
  ```
74
30
 
75
- ### Line Chart
76
- ```ruby
77
- chart = Databox::LineChart.new "my_line"
78
- chart.add 40, "2014-01-29"
79
- chart.add 100, "2014-02-01"
80
- chart.add 500, "2014-02-02"
81
- chart.add 20, "2014-02-03"
82
- chart.save
31
+ ## Usage
83
32
 
84
- ```
33
+ Pushing data directly to Databox with help of `push` method:
85
34
 
86
- ### Bar Chart
87
35
  ```ruby
88
- chart = Databox::BarChart.new "my_bar"
89
- chart.add 40, "2014-01-29"
90
- chart.add 100, "2014-02-01"
91
- chart.add 500, "2014-02-02"
92
- chart.add 20, "2014-02-03"
93
- chart.save
94
- ```
36
+ client = Databox::Client.new
95
37
 
96
- ### Compare
97
- ```ruby
98
- compare = Databox::BarChart.new "my_compare"
99
- compare.add 40, "2014-01-29"
100
- compare.add 100, "2014-02-01"
101
- compare.add 500, "2014-02-02"
102
- compare.add 20, "2014-02-03"
103
- compare.save
104
- ```
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
+ })
105
44
 
106
- ### Interval
107
- ```ruby
108
- interval = Databox::BarChart.new "my_interval"
109
- interval.add 40, "2014-01-29"
110
- interval.add 100, "2014-02-01"
111
- interval.add 500, "2014-02-02"
112
- interval.add 20, "2014-02-03"
113
- interval.save
114
45
  ```
115
46
 
116
- ### Pipeline
117
- ```ruby
118
- pipeline = Databox::Pipeline.new "pipe_visits"
119
- pipeline.add "Negotiation", 1_121_603
120
- pipeline.add "Proposal", 3_245_927
121
- pipeline.add "Solution", 10_726_397
122
- pipeline.save
123
- ```
124
-
125
- ### Funnel
126
- ```ruby
127
- funnel = Databox::Funnel.new "funnel_simple"
128
- funnel.add "Requests sent", 33_342
129
- funnel.add "New Accounts", 25_350
130
- funnel.add "Viewed results", 8_930
131
- funnel.add "Return Visits", 3_580
132
- funnel.save
133
- ```
47
+ Inserting multiple matrices with one `insert_all`:
134
48
 
135
- ### Pie
136
49
  ```ruby
137
- pie = Databox::Pie.new "my_pie"
138
- pie.add "A", 3_014
139
- pie.add "B", 29_496
140
- pie.add "C", 9_121
141
- pie.add "D", 20_390
142
- pie.add "E", 7_423
143
- pie.save
144
- ```
145
-
146
- ### Progress
147
- ```ruby
148
- progress = Databox::Progress.new "my_progress"
149
- progress.set "33% done", 100, 33
150
- pie.save
151
- ```
50
+ client = Databox::Client.new
152
51
 
153
- ### Messages
154
-
155
- ```ruby
156
- messages = Databox::Messages.new "just_messages"
157
- messages.add "I was here"
158
- messages.add "Second message"
159
- messages.save
52
+ client.insert_all [
53
+ {key: 'temp.boston', value: 52},
54
+ {key: 'temp.boston', value: 50, date: '2015-01-01 17:00:00'},
55
+ {key: 'temp.ny', value: 49}
56
+ ]
160
57
  ```
161
58
 
162
- ## Using client directly
163
-
164
- You can also use Databox::Client directly to insert raw KPIs. This is done by invoking Databox::Client and pushing Hash or Array of Hashes to it.
59
+ Retrieving information from last push with `last_push`:
165
60
 
166
61
  ```ruby
167
- client = Databox.client
168
-
169
- client.push([{
170
- key: "name",
171
- date: "2014-01-22T12:00:00",
172
- value: 1
173
- }])
174
-
175
- puts client.logs
62
+ client.last_push
63
+
64
+ # =>
65
+ #[{
66
+ # "push"=>"{\"data\":[{\"$sales.total\":2000}]}",
67
+ # "err"=>"[]",
68
+ # "no_err"=>0,
69
+ # "datetime"=>"2015-06-05T10:21:23.861Z",
70
+ # "keys"=>"[\"132|sales.total\"]"
71
+ #}]
176
72
  ```
177
73
 
178
- ## Versions
179
-
180
- [Databox](https://github.com/otobrglez/databox) is tested on following Ruby versions
181
-
182
- - MRI 1.9.3
183
- - MRI 2.0.0
184
-
185
74
  ## Resources
186
75
 
187
76
  - [Databox Web App](https://app.databox.com/)
188
77
  - [Databox Developers Portal](https://developers.databox.com/)
189
78
 
79
+ ## Author & License
80
+
81
+ - [Oto Brglez](https://github.com/otobrglez)
82
+ - Comes with `MIT` license and terms
190
83
 
191
- [fury-badge]: https://badge.fury.io/rb/databox.png
192
- [fury]: http://badge.fury.io/rb/databox
193
- [travis-badge]: https://secure.travis-ci.org/otobrglez/databox.png?branch=master
194
- [travis]: http://travis-ci.org/otobrglez/databox
84
+ [travis-badge]: https://secure.travis-ci.org/databox/databox-ruby.png?branch=v2
85
+ [travis]: http://travis-ci.org/databox/databox-ruby
data/databox.gemspec CHANGED
@@ -4,35 +4,25 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
  require 'databox/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
- spec.name = "databox"
7
+ spec.name = 'databox'
8
8
  spec.version = Databox::VERSION
9
- spec.authors = ["Oto Brglez"]
10
- spec.email = ["otobrglez@gmail.com"]
9
+ spec.authors = ['Oto Brglez']
10
+ spec.email = ['otobrglez@gmail.com']
11
11
  spec.description = %q{Ruby Gem for Databox - Mobile Executive Dashboard.}
12
12
  spec.summary = %q{API wrapper for Databox}
13
- spec.homepage = "https://github.com/otobrglez/databox"
14
- spec.license = "MIT"
13
+ spec.homepage = 'https://github.com/databox/databox-ruby'
14
+ spec.license = 'MIT'
15
15
 
16
16
  spec.files = `git ls-files`.split($/)
17
17
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
- spec.require_paths = ["lib"]
19
+ spec.require_paths = ['lib']
20
20
 
21
-
22
- spec.add_dependency "httparty"
23
-
24
- #TODO: Use multi_json spec.add_dependency "multi_json"
21
+ spec.add_dependency 'httparty'
25
22
 
26
23
  spec.add_development_dependency "bundler", "~> 1.3"
27
24
  spec.add_development_dependency "rake"
28
25
  spec.add_development_dependency "rspec"
29
26
  spec.add_development_dependency "shoulda-matchers"
30
- spec.add_development_dependency "webmock"
31
- spec.add_development_dependency "dotenv"
32
- spec.add_development_dependency "guard"
33
- spec.add_development_dependency "guard-rspec"
34
27
  spec.add_development_dependency "pry"
35
- spec.add_development_dependency "simplecov"
36
- spec.add_development_dependency "coveralls"
37
-
38
28
  end
data/example.rb ADDED
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env ruby
2
+ require 'pp'
3
+ require 'bundler/setup'
4
+ require 'databox'
5
+
6
+ Databox.configure do |c|
7
+ c.push_token = ENV["DATABOX_PUSH_TOKEN"]
8
+ end
9
+
10
+ client = Databox::Client.new()
11
+
12
+ 3.times do |t|
13
+ client.push("example.ruby", t*100, (DateTime.now - t).strftime('%Y-%m-%d'))
14
+ end
15
+
16
+ pp client.last_push(3)
17
+
data/lib/databox.rb CHANGED
@@ -1,34 +1,21 @@
1
- require "httparty"
2
-
3
1
  module Databox
4
- autoload :VERSION, "databox/version"
5
- autoload :Client, "databox/client"
6
- autoload :Configuration, "databox/configuration"
7
- autoload :Integration, "databox/integration"
8
-
9
- autoload :BigNumber, "databox/integration"
10
- autoload :LineChart, "databox/integration"
11
- autoload :BarChart, "databox/integration"
12
- autoload :Compare, "databox/integration"
13
- autoload :Interval, "databox/integration"
14
-
15
- autoload :Pipeline, "databox/integration"
16
- autoload :Funnel, "databox/integration"
17
- autoload :Pie, "databox/integration"
18
- autoload :Progress, "databox/integration"
19
- autoload :Messages, "databox/integration"
20
-
2
+ autoload :VERSION, 'databox/version'
3
+ autoload :Client, 'databox/client'
4
+ autoload :Configuration, 'databox/configuration'
21
5
 
22
6
  class << self
23
7
  attr_accessor :configuration
24
8
 
25
9
  def configured?
26
10
  return false if configuration.nil?
27
- [configuration.token, configuration.key, configuration.url].compact.size == 3
11
+ [
12
+ configuration.push_host,
13
+ configuration.push_token
14
+ ].compact.size == 2
28
15
  end
29
16
 
30
17
  def client
31
- @client ||= Databox::Client.new
18
+ @client ||= ::Client.new
32
19
  end
33
20
  end
34
21
 
@@ -36,5 +23,4 @@ module Databox
36
23
  self.configuration ||= Configuration.new
37
24
  yield configuration if block_given?
38
25
  end
39
-
40
26
  end
@@ -1,90 +1,71 @@
1
+ require 'httparty'
2
+ require 'json'
3
+
1
4
  class Databox::Client
2
5
  include HTTParty
3
6
  format :json
4
-
5
- headers "User-Agent" => "Databox/#{Databox::VERSION} (Ruby)"
6
-
7
- debug_output if ENV["HTTPARTY_DEBUG"] == "1"
8
-
7
+ headers 'User-Agent' => "Databox/#{Databox::VERSION} (Ruby)"
8
+ debug_output if [1, "1"].include?(ENV["HTTPARTY_DEBUG"])
9
9
  default_timeout 1 if ENV["DATABOX_MODE"] == "test"
10
10
 
11
- attr_accessor :token
12
- def token
13
- @token || Databox.configuration.token
14
- end
15
-
16
- def key; Databox.configuration.key end
17
- def url; Databox.configuration.url end
11
+ attr_accessor :last_push_content
18
12
 
19
13
  def initialize
20
14
  Databox.configure unless Databox.configured?
21
15
 
22
- self.class.base_uri url
23
- self.class.basic_auth key, "password"
16
+ self.class.base_uri push_host
17
+ self.class.basic_auth push_token, ''
18
+ self.class.headers 'Content-Type' => 'application/json'
19
+ self
24
20
  end
25
21
 
26
- def push data={}
27
- if validate data
28
- data = [data] unless data.is_a?(Array)
29
- handle self.class.post("/push/custom/#{self.token}", body: { data: data }.to_json)
30
- end
22
+ def push_host
23
+ Databox.configuration.push_host
31
24
  end
32
25
 
33
- def logs
34
- handle self.class.get("/push/custom/#{self.token}/logs")
26
+ def push_token
27
+ Databox.configuration.push_token
35
28
  end
36
29
 
37
- def handle response
38
- if response.code > 201
39
- raise Databox::ClientError.new(
40
- response.code.to_s+" - "+
41
- response.parsed_response["error"]["type"]+" - "+
42
- response.parsed_response["error"]["message"]
43
- )
44
- end
45
-
46
- output = response.parsed_response
30
+ # Sends data to actual end-point.
31
+ def raw_push(path='/', data=nil)
32
+ handle self.class.post(path, data.nil? ? {} : {body: JSON.dump({data: data})})
33
+ end
47
34
 
48
- if output.is_a?(Hash) and output.keys.include?("response")
49
- Databox::Response.new(output["response"])
50
- elsif output.is_a?(Array)
51
- output.map { |item| Databox::Response.new(item) }
52
- else
53
- output
54
- end
35
+ def handle(response)
36
+ response.parsed_response
55
37
  end
56
38
 
57
- def validate data
58
- return data.map do |dp|
59
- validate(dp)
60
- end if data.is_a?(Array)
39
+ def process_kpi(options={})
40
+ options.delete_if { |k, _| [:date, 'date'].include?(k) }
61
41
 
62
- errors = []
63
- errors.push("Data is missing") if data.nil? or data == {}
64
- errors.push("Key is required") if data[:key].nil?
65
- # errors.push("Value is required") if data[:value].nil?
42
+ %i{key value}.each do |k|
43
+ raise("Missing '#{k}'") if (options[k] || options[k.to_s]).nil?
44
+ end
66
45
 
67
- errors.push("Date format is invalid") if not(data[:date].nil?) and (Date.iso8601(data[:date]) rescue false) == false
68
- errors.push("Key format is invalid") unless data[:key] =~/^[a-zA-Z0-9_\.\@]*$/
46
+ options["$#{(options['key'] || options[:key])}"] = options['value'] || options[:value]
47
+ options.delete_if { |k, _| [:key, 'key', :value, 'value'].include?(k) }
69
48
 
70
- unless errors.empty?
71
- invalid_record = Databox::InvalidRecord.new "Payload is invalid"
72
- invalid_record.errors = errors
73
- raise invalid_record
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 }
74
53
  end
75
54
 
76
- true
55
+ options
77
56
  end
78
- end
79
57
 
80
- class Databox::InvalidRecord < StandardError
81
- attr_accessor :errors
82
- end
58
+ def push(kpi={})
59
+ self.last_push_content = raw_push('/', [process_kpi(kpi)])
60
+ self.last_push_content['status'] == 'ok'
61
+ end
83
62
 
84
- class Databox::ClientError < StandardError; end
63
+ def insert_all(rows=[])
64
+ self.last_push_content = raw_push('/', rows.map {|r| process_kpi(r) })
65
+ self.last_push_content['status'] == 'ok'
66
+ end
85
67
 
86
- class Databox::Response < OpenStruct
87
- def success?
88
- self.type == "success"
68
+ def last_push(n=1)
69
+ raw_push "/lastpushes/#{n}"
89
70
  end
90
71
  end