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(':').last }
  end
  @metrics = xml.search('dxp:metric').collect do |metric|
    { metric.attributes['name'].split(':').last.to_sym => metric.attributes['value'].split(':').last.to_f }
  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 = :short) ⇒ 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
48
# File 'lib/gattica/data_point.rb', line 29

def to_csv(format = :short)
  output = ''
  columns = []
  
  # only output
  case format
  when :long

    [@id, @updated, @title].each { |c| columns << c }
  end
  
  # output all dimensions

  @dimensions.map {|d| d.values.first}.each { |c| columns << c }
  # output all metrics
  @metrics.map {|m| m.values.first}.each { |c| columns << c }

  output = CSV.generate_line(columns)      
 
end

#to_hashObject



59
60
61
62
63
64
65
66
67
68
# File 'lib/gattica/data_point.rb', line 59

def to_hash

  res_hash = {}

  @dimensions.each{|d| res_hash.merge!(d) }
  # output all metrics
  @metrics.each{|m| res_hash.merge!(m) }
  res_hash

end

#to_yamlObject



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

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