Class: Gattica::DataPoint

Inherits:
Object
  • Object
show all
Includes:
Convertible
Defined in:
lib/gattica/data_point.rb

Overview

Represents a single “row” of data containing any number of dimensions, metrics

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Convertible

#to_h, #to_query, #to_s, #to_xml

Constructor Details

#initialize(xml) ⇒ DataPoint

Parses the XML <entry> element



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/gattica/data_point.rb', line 14

def initialize(xml)
  @xml = xml.to_s
  @id = xml.at('id').inner_html
  @updated = DateTime.parse(xml.at('updated').inner_html)
  @title = xml.at('title').inner_html
  @dimensions = xml.search('dxp:dimension').collect do |dimension|
    { dimension.attributes['name'].split(':').last.to_sym => dimension.attributes['value'].split(':', 1).last }
  end
  @metrics = xml.search('dxp:metric').collect do |metric|
    { metric.attributes['name'].split(':').last.to_sym => metric.attributes['value'].split(':', 1).last.to_i }
  end
end

Instance Attribute Details

#dimensionsObject (readonly)

Returns the value of attribute dimensions.



11
12
13
# File 'lib/gattica/data_point.rb', line 11

def dimensions
  @dimensions
end

#idObject (readonly)

Returns the value of attribute id.



11
12
13
# File 'lib/gattica/data_point.rb', line 11

def id
  @id
end

#metricsObject (readonly)

Returns the value of attribute metrics.



11
12
13
# File 'lib/gattica/data_point.rb', line 11

def metrics
  @metrics
end

#titleObject (readonly)

Returns the value of attribute title.



11
12
13
# File 'lib/gattica/data_point.rb', line 11

def title
  @title
end

#updatedObject (readonly)

Returns the value of attribute updated.



11
12
13
# File 'lib/gattica/data_point.rb', line 11

def updated
  @updated
end

#xmlObject (readonly)

Returns the value of attribute xml.



11
12
13
# File 'lib/gattica/data_point.rb', line 11

def xml
  @xml
end

Instance Method Details

#to_csv(format = :long) ⇒ Object

Outputs in Comma Seperated Values format



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/gattica/data_point.rb', line 29

def to_csv(format = :long)
  output = ''
  
  columns = []
  # only output
  case format
  when :long
    columns.concat([@id, @updated, @title])
  end
  
  # output all dimensions
  columns.concat(@dimensions.map {|d| d.value})
  
  # output all metrics
  columns.concat(@metrics.map {|m| m.value})

  output = CSV.generate_line(columns)      
  return output
end

#to_yamlObject



50
51
52
53
54
55
56
# File 'lib/gattica/data_point.rb', line 50

def to_yaml
  { 'id' => @id,
    'updated' => @updated,
    'title' => @title,
    'dimensions' => @dimensions,
    'metrics' => @metrics }.to_yaml
end