Class: Databox::Client
- Inherits:
-
Object
- Object
- Databox::Client
- Includes:
- HTTParty
- Defined in:
- lib/databox/client.rb
Instance Attribute Summary collapse
-
#last_push_content ⇒ Object
Returns the value of attribute last_push_content.
Instance Method Summary collapse
- #handle(response) ⇒ Object
-
#initialize ⇒ Client
constructor
A new instance of Client.
- #insert_all(rows = []) ⇒ Object
- #last_push(n = 1) ⇒ Object
- #process_kpi(options = {}) ⇒ Object
- #push(kpi = {}) ⇒ Object
- #push_host ⇒ Object
- #push_token ⇒ Object
-
#raw_push(path = '/', data = nil) ⇒ Object
Sends data to actual end-point.
Constructor Details
#initialize ⇒ Client
Returns a new instance of Client.
14 15 16 17 18 19 20 21 |
# File 'lib/databox/client.rb', line 14 def initialize Databox.configure unless Databox.configured? self.class.base_uri push_host self.class.basic_auth push_token, '' self.class.headers 'Content-Type' => 'application/json' self end |
Instance Attribute Details
#last_push_content ⇒ Object
Returns the value of attribute last_push_content.
12 13 14 |
# File 'lib/databox/client.rb', line 12 def last_push_content @last_push_content end |
Instance Method Details
#handle(response) ⇒ Object
36 37 38 |
# File 'lib/databox/client.rb', line 36 def handle(response) response.parsed_response end |
#insert_all(rows = []) ⇒ Object
63 64 65 66 |
# File 'lib/databox/client.rb', line 63 def insert_all(rows=[]) self.last_push_content = raw_push('/', rows.map {|r| process_kpi(r) }) self.last_push_content.key?('id') end |
#last_push(n = 1) ⇒ Object
68 69 70 |
# File 'lib/databox/client.rb', line 68 def last_push(n=1) handle self.class.get("/lastpushes?limit=#{n}") end |
#process_kpi(options = {}) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/databox/client.rb', line 40 def process_kpi(={}) %i{key value}.each do |k| raise("Missing '#{k}'") if ([k] || [k.to_s]).nil? end ["$#{(['key'] || [:key])}"] = ['value'] || [:value] .delete_if { |k, _| [:key, 'key', :value, 'value'].include?(k) } attributes = [:attributes] || ['attributes'] unless attributes.nil? [:attributes, 'attributes'].each {|k| .delete(k) } attributes.each { |k,v| [k] = v } end end |
#push(kpi = {}) ⇒ Object
58 59 60 61 |
# File 'lib/databox/client.rb', line 58 def push(kpi={}) self.last_push_content = raw_push('/', [process_kpi(kpi)]) self.last_push_content.key?('id') end |
#push_host ⇒ Object
23 24 25 |
# File 'lib/databox/client.rb', line 23 def push_host Databox.configuration.push_host end |
#push_token ⇒ Object
27 28 29 |
# File 'lib/databox/client.rb', line 27 def push_token Databox.configuration.push_token end |
#raw_push(path = '/', data = nil) ⇒ Object
Sends data to actual end-point.
32 33 34 |
# File 'lib/databox/client.rb', line 32 def raw_push(path='/', data=nil) handle self.class.post(path, data.nil? ? {} : {body: JSON.dump({data: data})}) end |