Class: Databox::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/databox/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClient

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_contentObject

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(options={})

  %i{key value}.each do |k|
    raise("Missing '#{k}'") if (options[k] || options[k.to_s]).nil?
  end

  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

#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_hostObject



23
24
25
# File 'lib/databox/client.rb', line 23

def push_host
  Databox.configuration.push_host
end

#push_tokenObject



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