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

#hash_to_query, #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



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/gattica/data_set.rb', line 25

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

  # only show the nitty gritty details of id, updated_at and title if requested
  case format
  when :long
    ["id", "updated", "title"].each { |c| columns << c }
  end
  
  unless @points.empty?   # if there was at least one result
    @points.first.dimensions.map { |d|
      (
        d.keys.first if d.length == 1
      )
    }.each { |c| columns << c }
    @points.first.metrics.map { |m|
      (
        m.keys.first if m.length == 1
      )
    }.each { |c| columns << c }
  end
  
  output = CSV.generate_line(columns) + "\n"

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

#to_yamlObject



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

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