Class: Gattica::DataSet
- Inherits:
-
Object
- Object
- Gattica::DataSet
- Includes:
- Convertible
- Defined in:
- lib/gattica/data_set.rb
Overview
Encapsulates the data returned by the GA API
Instance Attribute Summary collapse
-
#end_date ⇒ Object
readonly
Returns the value of attribute end_date.
-
#items_per_page ⇒ Object
readonly
Returns the value of attribute items_per_page.
-
#points ⇒ Object
readonly
Returns the value of attribute points.
-
#start_date ⇒ Object
readonly
Returns the value of attribute start_date.
-
#start_index ⇒ Object
readonly
Returns the value of attribute start_index.
-
#total_results ⇒ Object
readonly
Returns the value of attribute total_results.
-
#xml ⇒ Object
readonly
Returns the value of attribute xml.
Instance Method Summary collapse
-
#initialize(xml) ⇒ DataSet
constructor
A new instance of DataSet.
-
#to_csv(format = :short) ⇒ Object
Returns a string formatted as a CSV containing just the data points.
- #to_hash ⇒ Object
- #to_yaml ⇒ Object
Methods included from Convertible
#to_h, #to_query, #to_s, #to_xml
Constructor Details
#initialize(xml) ⇒ DataSet
Returns a new instance of DataSet.
10 11 12 13 14 15 16 17 18 |
# File 'lib/gattica/data_set.rb', line 10 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_date ⇒ Object (readonly)
Returns the value of attribute end_date.
7 8 9 |
# File 'lib/gattica/data_set.rb', line 7 def end_date @end_date end |
#items_per_page ⇒ Object (readonly)
Returns the value of attribute items_per_page.
7 8 9 |
# File 'lib/gattica/data_set.rb', line 7 def items_per_page @items_per_page end |
#points ⇒ Object (readonly)
Returns the value of attribute points.
7 8 9 |
# File 'lib/gattica/data_set.rb', line 7 def points @points end |
#start_date ⇒ Object (readonly)
Returns the value of attribute start_date.
7 8 9 |
# File 'lib/gattica/data_set.rb', line 7 def start_date @start_date end |
#start_index ⇒ Object (readonly)
Returns the value of attribute start_index.
7 8 9 |
# File 'lib/gattica/data_set.rb', line 7 def start_index @start_index end |
#total_results ⇒ Object (readonly)
Returns the value of attribute total_results.
7 8 9 |
# File 'lib/gattica/data_set.rb', line 7 def total_results @total_results end |
#xml ⇒ Object (readonly)
Returns the value of attribute xml.
7 8 9 |
# File 'lib/gattica/data_set.rb', line 7 def xml @xml end |
Instance Method Details
#to_csv(format = :short) ⇒ Object
Returns a string formatted as a CSV containing just the data points.
Parameters:
- format=:long
-
Adds id, updated, title to output columns
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/gattica/data_set.rb', line 24 def to_csv(format=:short) output = '' columns = [] 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}.each { |c| columns << c } @points.first.metrics.map {|m| m.keys.first}.each { |c| columns << c } end output = CSV.generate_line(columns) @points.each do |point| output += point.to_csv(format) end output end |
#to_hash ⇒ Object
51 52 53 |
# File 'lib/gattica/data_set.rb', line 51 def to_hash @points.map(&:to_hash) end |
#to_yaml ⇒ Object
42 43 44 45 46 47 48 49 |
# File 'lib/gattica/data_set.rb', line 42 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 |