Class: GoogleApi::Ga::Data

Inherits:
Object
  • Object
show all
Defined in:
lib/google_api/ga/data.rb

Constant Summary collapse

DATE_FORMAT =

Convert string, DateTime and Time to date

/\A[0-9]{4}-[0-9]{2}-[0-9]{2}\Z/
TYPE_1 =

Type 1, all are integer

Ids, Cache, Start index, Max results

name,        alias
{ ids:         :id,
cache:       nil,
start_index: :offset,
max_results: :limit }
TYPE_2 =

Type 2, date

Start date, End date

name,       alias
{ start_date: :from,
end_date:   :to }
TYPE_3 =

Type 3

Metrics, Dimensions, Sort

name,       alias
{ metrics:    :select,
dimensions: :with,
sort:       nil }
TYPE_4 =

Type 4

Filters, Segment

name,       alias
{ filters: :where,
segment: nil }

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeData

Returns a new instance of Data.



5
6
7
8
9
10
11
# File 'lib/google_api/ga/data.rb', line 5

def initialize
  @ids, @cache = nil, nil
  @start_date, @end_date = Date.today, Date.today
  @metrics, @dimensions, @sort = [], [], []
  @filters, @segment = nil, nil
  @start_index, @max_results = nil, nil
end

Class Method Details

.method_missing(method, *args, &block) ⇒ Object

Auto initialize data



14
15
16
17
18
19
20
# File 'lib/google_api/ga/data.rb', line 14

def self.method_missing(method, *args, &block)
  if block_given?
    new.send(method, &block)
  else
    new.send(method, *args)
  end
end

Instance Method Details

#allObject



260
261
262
# File 'lib/google_api/ga/data.rb', line 260

def all
  @all ||= [header, rows]
end

#build_param(value) ⇒ Object

Add prefix ga: to symbol in Array



38
39
40
41
42
# File 'lib/google_api/ga/data.rb', line 38

def build_param(value)
  type?(value, Array)

  value.flatten.collect { |v| v.is_a?(Symbol) ? "ga:#{v}" : v }
end

#clearObject

Clear value



52
53
54
55
56
57
# File 'lib/google_api/ga/data.rb', line 52

def clear
  @header     = nil
  @parameters = nil
  @data       = nil
  @all        = nil
end

#countObject



256
257
258
# File 'lib/google_api/ga/data.rb', line 256

def count
  data.total_results
end

#each(&block) ⇒ Object



264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/google_api/ga/data.rb', line 264

def each(&block)
  if block.arity == 1 # each
    rows.each do |row|
      yield(row)
    end
  else                # each with index
    i = -1
    rows.each do |row|
      i += 1
      yield(i, row)
    end
  end
end

#each!(&block) ⇒ Object



278
279
280
281
# File 'lib/google_api/ga/data.rb', line 278

def each!(&block)
  clear
  each(block)
end

#headerObject



252
253
254
# File 'lib/google_api/ga/data.rb', line 252

def header
  @header ||= data.column_headers.map { |c| c.name }
end

#rowsObject



248
249
250
# File 'lib/google_api/ga/data.rb', line 248

def rows
  data.rows
end

#to_date(date) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/google_api/ga/data.rb', line 25

def to_date(date)
  if date.is_a?(String)
    unless date =~ DATE_FORMAT
      raise GoogleApi::DateError, "Date: #{date} must match with #{DATE_FORMAT}."
    end

    date = Date.parse(date)
  end

  date.to_date
end

#type?(value, type) ⇒ Boolean

Check type

Returns:

  • (Boolean)


45
46
47
48
49
# File 'lib/google_api/ga/data.rb', line 45

def type?(value, type)
  unless value.is_a?(type)
    raise GoogleApi::TypeError, "Value: #{value} must be #{type}."
  end
end