Sha256: e15dac454df2e16bc0283d4a371b420a3cce37fc1d434ea0929bc2f0272bec8b

Contents?: true

Size: 1.56 KB

Versions: 2

Compression:

Stored size: 1.56 KB

Contents

module Gattica
  
  # Encapsulates the data returned by the GA API
  
  class DataSet
    
    include Convertible
    
    attr_reader :total_results, :start_index, :items_per_page, :start_date, :end_date, :points, :xml
      
    def initialize(xml)
      @xml = xml.to_s
      @total_results = xml.at('openSearch:totalResults').inner_html.to_i
      @start_index = xml.at('openSearch:startIndex').inner_html.to_i
      @items_per_page = xml.at('openSearch:itemsPerPage').inner_html.to_i
      @start_date = Date.parse(xml.at('dxp:startDate').inner_html)
      @end_date = Date.parse(xml.at('dxp:endDate').inner_html)
      @points = xml.search(:entry).collect { |entry| DataPoint.new(entry) }
    end
    
    
    # output important data to CSV, ignoring all the specific data about this dataset 
    # (total_results, start_date) and just output the data from the points
    
    def to_csv(format = :long)
      # build the headers
      output = ''
      
      # only show the nitty gritty details of id, updated_at and title if requested
      case format
      when :long
        output = '"id","updated","title",'
      end
      
      output += @points.first.dimensions.collect do |dimension|
        "\"#{dimension.key.to_s}\""
      end.join(',')
      output += ','
      output += @points.first.metrics.collect do |metric|
        "\"#{metric.key.to_s}\""
      end.join(',') 
      output += "\n"
      
      # get the data from each point
      @points.each do |point|
        output += point.to_csv(format) + "\n"
      end
      
      return output
    end
    
  end
  
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
cannikin-gattica-0.1.4 lib/gattica/data_set.rb
cannikin-gattica-0.2.0 lib/gattica/data_set.rb