Class: Gattica::DataPoint
- Inherits:
-
Object
- Object
- Gattica::DataPoint
- 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
-
#dimensions ⇒ Object
readonly
Returns the value of attribute dimensions.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#metrics ⇒ Object
readonly
Returns the value of attribute metrics.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
-
#updated ⇒ Object
readonly
Returns the value of attribute updated.
-
#xml ⇒ Object
readonly
Returns the value of attribute xml.
Instance Method Summary collapse
-
#initialize(xml) ⇒ DataPoint
constructor
Parses the XML <entry> element.
-
#to_csv(format = :long) ⇒ Object
Outputs in Comma Seperated Values format.
- #to_yaml ⇒ Object
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 26 27 28 29 30 |
# File 'lib/gattica/data_point.rb', line 14 def initialize(xml) @xml = xml.to_s @id = xml.at_xpath('xmlns:id').text @updated = DateTime.parse(xml.at_xpath('xmlns:updated').text) @title = xml.at_xpath('xmlns:title').text @dimensions = xml.xpath('dxp:dimension').collect do |dimension| { dimension['name'].split(':').last.to_sym => dimension['value'].split(':', 1).last } end @metrics = xml.xpath('dxp:metric').collect do |metric| # We're adding the 'to_f.to_i' conversion to the google metric value # because sometimes the google metric will come back in scientific # notation(eg 3.14E7). When applying 'to_i' to this value, the result # will be '3' because 'to_i' doesn't know how to parse scientific. # However, to_f does, and will return the correct 31400000. { metric['name'].split(':').last.to_sym => metric['value'].split(':', 1).last.to_f.to_i } end end |
Instance Attribute Details
#dimensions ⇒ Object (readonly)
Returns the value of attribute dimensions.
11 12 13 |
# File 'lib/gattica/data_point.rb', line 11 def dimensions @dimensions end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
11 12 13 |
# File 'lib/gattica/data_point.rb', line 11 def id @id end |
#metrics ⇒ Object (readonly)
Returns the value of attribute metrics.
11 12 13 |
# File 'lib/gattica/data_point.rb', line 11 def metrics @metrics end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
11 12 13 |
# File 'lib/gattica/data_point.rb', line 11 def title @title end |
#updated ⇒ Object (readonly)
Returns the value of attribute updated.
11 12 13 |
# File 'lib/gattica/data_point.rb', line 11 def updated @updated end |
#xml ⇒ Object (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
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/gattica/data_point.rb', line 34 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.values.first}) # output all metrics columns.concat(@metrics.map {|m| m.values.first}) output = CSV.generate_line(columns) return output end |
#to_yaml ⇒ Object
55 56 57 58 59 60 61 |
# File 'lib/gattica/data_point.rb', line 55 def to_yaml { 'id' => @id, 'updated' => @updated, 'title' => @title, 'dimensions' => @dimensions, 'metrics' => @metrics }.to_yaml end |