Class: GoogleAnalyticsFeeds::DataFeed

Inherits:
Object
  • Object
show all
Includes:
Naming
Defined in:
lib/google_analytics_feeds.rb

Constant Summary collapse

BASE_URI =
"https://www.googleapis.com/analytics/v2.4/data"

Instance Method Summary collapse

Methods included from Naming

#name_to_symbol, #symbol_to_name

Constructor Details

#initializeDataFeed

Returns a new instance of DataFeed.



202
203
204
# File 'lib/google_analytics_feeds.rb', line 202

def initialize
  @params = {}
end

Instance Method Details

#cloneObject



275
276
277
278
279
# File 'lib/google_analytics_feeds.rb', line 275

def clone
  obj = super
  obj.instance_variable_set(:@params, @params.clone)
  obj
end

#dates(start_date, end_date) ⇒ Object



224
225
226
227
228
229
# File 'lib/google_analytics_feeds.rb', line 224

def dates(start_date, end_date)
  clone_and_set {|params|
    params['start-date'] = start_date.strftime("%Y-%m-%d")
    params['end-date'] = end_date.strftime("%Y-%m-%d")
  }
end

#dimensions(*vals) ⇒ Object



218
219
220
221
222
# File 'lib/google_analytics_feeds.rb', line 218

def dimensions(*vals)
  clone_and_set {|params|
    params['dimensions'] = vals.map {|v| symbol_to_name(v) }.join(',')
  }
end

#filters(&block) ⇒ Object



243
244
245
246
247
248
# File 'lib/google_analytics_feeds.rb', line 243

def filters(&block)
  builder = 
  clone_and_set {|params|
    params['filters'] = FilterBuilder.new.build(&block)
  }
end

#max_results(i) ⇒ Object



237
238
239
240
241
# File 'lib/google_analytics_feeds.rb', line 237

def max_results(i)
  clone_and_set {|params|
    params['max-results'] = i.to_s
  }
end

#metrics(*vals) ⇒ Object



212
213
214
215
216
# File 'lib/google_analytics_feeds.rb', line 212

def metrics(*vals)
  clone_and_set {|params|
    params['metrics'] = vals.map {|v| symbol_to_name(v) }.join(',')
  }
end

#profile(id) ⇒ Object



206
207
208
209
210
# File 'lib/google_analytics_feeds.rb', line 206

def profile(id)
  clone_and_set {|params|
    params['ids'] = symbol_to_name(id)
  }
end

#retrieve(session_token, connection) ⇒ Object



268
269
270
271
272
273
# File 'lib/google_analytics_feeds.rb', line 268

def retrieve(session_token, connection)
  connection.get(uri) do |request|
    request.headers['Authorization'] = 
      "GoogleLogin auth=#{session_token}"
  end
end

#sort(column, direction) ⇒ Object

Sorts the result set by a column.

Direction can be :asc or :desc.



253
254
255
256
257
258
# File 'lib/google_analytics_feeds.rb', line 253

def sort(column, direction)
  clone_and_set {|params|
    c = symbol_to_name(column)
    params['sort'] = (direction == :desc ? "-#{c}" : c)
  }
end

#start_index(i) ⇒ Object



231
232
233
234
235
# File 'lib/google_analytics_feeds.rb', line 231

def start_index(i)
  clone_and_set {|params|
    params['start-index'] = i.to_s
  }
end

#uriObject Also known as: to_s



260
261
262
263
264
# File 'lib/google_analytics_feeds.rb', line 260

def uri
  uri = Addressable::URI.parse(BASE_URI)
  uri.query_values = @params
  uri.to_s
end