Class: Carbonator::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/carbonator/parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Parser

Returns a new instance of Parser.



5
6
7
# File 'lib/carbonator/parser.rb', line 5

def initialize(opts={})
  self.prefix = opts[:prefix] || 'carbonator'
end

Instance Attribute Details

#prefixObject

Returns the value of attribute prefix.



3
4
5
# File 'lib/carbonator/parser.rb', line 3

def prefix
  @prefix
end

Instance Method Details

#measurement?(data) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/carbonator/parser.rb', line 25

def measurement?(data)
  data.has_key?('measure') && data.has_key?('value')
end

#metric(data) ⇒ Object



9
10
11
# File 'lib/carbonator/parser.rb', line 9

def metric(data)
  prefix ? "#{prefix}.#{data['measure']}" : data['measure']
end

#parse(data) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/carbonator/parser.rb', line 13

def parse(data)
  return nil unless measurement?(data)
  metric = metric(data)
  value       = value(data)
  timestamp   = timestamp(data)
  "#{metric} #{value} #{timestamp}"
end

#timestamp(data) ⇒ Object



21
22
23
# File 'lib/carbonator/parser.rb', line 21

def timestamp(data)
  data['timestamp'] || Time.now.to_i  
end

#value(data) ⇒ Object



29
30
31
# File 'lib/carbonator/parser.rb', line 29

def value(data)
  data['value']
end