Class: GoogleApi::Ga::Data

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

Constant Summary collapse

TYPE_INTEGER =

Parameters for google analytics ————————————————————-

{ ids: 'id',
cache: nil,
start_index: 'offset',
max_results: 'limit' }
TYPE_DATE =
{ start_date: 'from',
end_date: 'to' }
TYPE_ARRAY =
{ metrics: 'select',
dimensions: 'with',
sort: nil }
TYPE_BLOCK =
{ filters: 'where',
segment: nil }
TYPE_BOOLEAN =
{ error: nil,
use_cache: nil }

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeData

Initialize ———————————————————————————-



7
8
9
10
11
12
13
14
15
# File 'lib/google_api/ga/data.rb', line 7

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
  @error = false
  @use_cache = true
end

Class Method Details

.create_array_method(name, operator = '', suffix = '') ⇒ Object

Create an array method, suffix is for _add or _sub metrics, dimensions, sort

Example:

def metrics(*args)
  if args.empty?
    return @metrics
  end

  self.metrics = args
  self
end

def metrics=(value)
  clear
  @metrics = build_param(value)
end


92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/google_api/ga/data.rb', line 92

def self.create_array_method(name, operator = '', suffix = '')
  eval <<-METHOD
    def #{name}#{suffix}(*args)
      if args.empty?
        return @#{name}
      end

      self.#{name}#{suffix} = args
      self
    end

    def #{name}#{suffix}=(value)
      clear
      @#{name} #{operator}= build_param(value)
    end
  METHOD
end

.create_base_alias(name, method_alias, suffix = '') ⇒ Object

Create a base alias ids, cache, start_index, max_results, start_date, end_date, metrics, dimensions, sort

Example:

alias :id  :ids
alias :id= :ids=


118
119
120
121
122
123
124
125
# File 'lib/google_api/ga/data.rb', line 118

def self.create_base_alias(name, method_alias, suffix = '')
  unless method_alias.nil?
    eval <<-METHOD
      alias :#{method_alias}#{suffix}  :#{name}#{suffix}
      alias :#{method_alias}#{suffix}= :#{name}#{suffix}=
    METHOD
  end
end

.create_base_method(name) ⇒ Object

Create a base method ids, cache, start_index, max_results, start_date, end_date, error, use_cache

Example:

def ids(value = nil)
  if value.nil?
    return @ids
  end

  self.ids = value
  self
end


60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/google_api/ga/data.rb', line 60

def self.create_base_method(name)
  eval <<-METHOD
    def #{name}(value = nil)
      if value.nil?
        return @#{name}
      end

      self.#{name} = value
      self
    end
  METHOD
end

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

Auto initialize data



18
19
20
21
22
23
24
# File 'lib/google_api/ga/data.rb', line 18

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



246
247
248
# File 'lib/google_api/ga/data.rb', line 246

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

#clearObject

Clear values



212
213
214
215
216
217
218
219
# File 'lib/google_api/ga/data.rb', line 212

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

  self
end

#clear_cacheObject

Clear cache



222
223
224
225
# File 'lib/google_api/ga/data.rb', line 222

def clear_cache
  _cache.delete(parameters)
  self
end

#countObject



242
243
244
# File 'lib/google_api/ga/data.rb', line 242

def count
  data.total_results
end

#each(&block) ⇒ Object



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/google_api/ga/data.rb', line 250

def each(&block)
  case block.arity
    # each
    when 1
      rows.each do |row|
        yield(row)
      end

    # each with index
    when 2
      i = -1
      rows.each do |row|
        i += 1
        yield(i, row)
      end
  end
end

#each!(&block) ⇒ Object



268
269
270
271
# File 'lib/google_api/ga/data.rb', line 268

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

#headerObject



238
239
240
# File 'lib/google_api/ga/data.rb', line 238

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

#rowsObject



234
235
236
# File 'lib/google_api/ga/data.rb', line 234

def rows
  data.rows
end