Class: Gattica::DataSet

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

Overview

Encapsulates the data returned by the GA API

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Convertible

#to_h, #to_query, #to_s, #to_xml

Constructor Details

#initialize(xml) ⇒ DataSet

Returns a new instance of DataSet.



11
12
13
14
15
16
17
18
19
# File 'lib/gattica/data_set.rb', line 11

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

Instance Attribute Details

#end_dateObject (readonly)

Returns the value of attribute end_date.



9
10
11
# File 'lib/gattica/data_set.rb', line 9

def end_date
  @end_date
end

#items_per_pageObject (readonly)

Returns the value of attribute items_per_page.



9
10
11
# File 'lib/gattica/data_set.rb', line 9

def items_per_page
  @items_per_page
end

#pointsObject (readonly)

Returns the value of attribute points.



9
10
11
# File 'lib/gattica/data_set.rb', line 9

def points
  @points
end

#start_dateObject (readonly)

Returns the value of attribute start_date.



9
10
11
# File 'lib/gattica/data_set.rb', line 9

def start_date
  @start_date
end

#start_indexObject (readonly)

Returns the value of attribute start_index.



9
10
11
# File 'lib/gattica/data_set.rb', line 9

def start_index
  @start_index
end

#total_resultsObject (readonly)

Returns the value of attribute total_results.



9
10
11
# File 'lib/gattica/data_set.rb', line 9

def total_results
  @total_results
end

#xmlObject (readonly)

Returns the value of attribute xml.



9
10
11
# File 'lib/gattica/data_set.rb', line 9

def xml
  @xml
end

Instance Method Details

#to_csv(format = :long) ⇒ Object

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



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/gattica/data_set.rb', line 50

def to_csv(format = :long)
  output = ''
  
  # build the headers
  output = to_csv_header(format)

  # get the data from each point
  @points.each do |point|
    output += point.to_csv(format) + "\n"
  end
  
  return output
end

#to_csv_header(format = :long) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/gattica/data_set.rb', line 21

def to_csv_header(format = :long)
  # build the headers
  output = ''
  columns = []

  # only show the nitty gritty details of id, updated_at and title if requested
  case format #it would be nice if case statements in ruby worked differently
  when :long
    columns.concat(["id", "updated", "title"])
    unless @points.empty?   # if there was at least one result
      columns.concat(@points.first.dimensions.map {|d| d.key})
      columns.concat(@points.first.metrics.map {|m| m.key})
    end    
  when :short
    unless @points.empty?   # if there was at least one result
      columns.concat(@points.first.dimensions.map {|d| d.key})
      columns.concat(@points.first.metrics.map {|m| m.key})
    end    
  when :noheader
  end
  
  output = CSV.generate_line(columns) + "\n" if (columns.size > 0) 

  return output    
end

#to_yamlObject



65
66
67
68
69
70
71
72
# File 'lib/gattica/data_set.rb', line 65

def to_yaml
  { 'total_results' => @total_results,
    'start_index' => @start_index,
    'items_per_page' => @items_per_page,
    'start_date' => @start_date,
    'end_date' => @end_date,
    'points' => @points}.to_yaml
end