Class: GAAPI::Row

Inherits:
Object
  • Object
show all
Defined in:
lib/gaapi/row.rb

Overview

A single row from a query to Google Analytics

Instance Method Summary collapse

Constructor Details

#initialize(report, row) ⇒ Row

Returns a new instance of Row.



12
13
14
15
# File 'lib/gaapi/row.rb', line 12

def initialize(report, row)
  @report = report
  @row = row
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object

Define and call methods for the columns in the report.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/gaapi/row.rb', line 18

def method_missing(method, *args)
  if (i = dimension_method_names.find_index(method))
    define_singleton_method(method) do
      dimensions[i]
    end
    send(method)
  elsif (i = metric_method_names.find_index(method))
    define_singleton_method(method) do
      convert_metric(i)
    end
    send(method)
  else
    super
  end
end

Instance Method Details

#dimensionsObject

An array of the dimension values, in the order that they appear in the dimension headers.



8
9
10
# File 'lib/gaapi/row.rb', line 8

def dimensions
  row["dimensions"] || []
end

#metricsObject

An array of the metric values, in the order that they appear in the metric headers.



36
37
38
39
40
41
# File 'lib/gaapi/row.rb', line 36

def metrics
  # NOTE: There is one entry in the `row["metrics"]` array for each data range.
  # Since currently we only support one date range, this following index is
  # always 0.
  row["metrics"][0]["values"]
end

#respond_to_missing?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/gaapi/row.rb', line 43

def respond_to_missing?
  true
end

#to_aObject

Return the data from the row as an Array, ordered by:

Headers first, in the order that they appear in the Report#headers array.
Metrics next, in the order that they appear in the Rport#metrics array.


50
51
52
# File 'lib/gaapi/row.rb', line 50

def to_a
  dimensions + metrics
end