Sha256: 21ced145bcb6fee6f508a82cbb9416739f69a5aeff6b7d4b05999157b8abe587

Contents?: true

Size: 1.88 KB

Versions: 3

Compression:

Stored size: 1.88 KB

Contents

require "csv"

module Gattica
  
  # Represents a single "row" of data containing any number of dimensions, metrics
  
  class DataPoint
    
    include Convertible
    
    attr_reader :id, :updated, :title, :dimensions, :metrics, :xml
    
    # Parses the XML <entry> element
    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
    
    
    # Outputs in Comma Seperated Values format
    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
    
    
    def to_yaml
      { 'id' => @id,
        'updated' => @updated,
        'title' => @title,
        'dimensions' => @dimensions,
        'metrics' => @metrics }.to_yaml
    end
    
  end
  
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
gicodewarrior-gattica-0.5.2 lib/gattica/data_point.rb
gicodewarrior-gattica-0.5.1 lib/gattica/data_point.rb
gicodewarrior-gattica-0.5.0 lib/gattica/data_point.rb