google_api 1.0.2 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,9 @@
1
+ ## v1.0.3
2
+
3
+ * GoogleApi::Ga::Data has now silent error, GoogleApi::Ga::Data.error(true) - raise error, GoogleApi::Ga::Data.error(false) - not error (default)
4
+ * add GoogleApi::Ga::Data.use_cache
5
+ * add GoogleApi::Ga::Data.clear_cache
6
+
1
7
  ## v1.0.2
2
8
 
3
9
  * ids is convert to integer
data/README.md CHANGED
@@ -239,22 +239,73 @@ First you can play on the playground: http://ga-dev-tools.appspot.com/explorer/.
239
239
  ```ruby
240
240
  GoogleApi::Ga::Data
241
241
  ```
242
+
242
243
  <br>
244
+
245
+ <table>
246
+ <thead>
247
+ <tr>
248
+ <th>method name</th>
249
+ <th>alias</th>
250
+ </tr>
251
+ </thead>
252
+
253
+ <tbody>
254
+ <tr>
255
+ <td>ids</td>
256
+ <td>id</td>
257
+ </tr>
258
+ <tr>
259
+ <td>start_date</td>
260
+ <td>from</td>
261
+ </tr>
262
+ <tr>
263
+ <td>end_date</td>
264
+ <td>to</td>
265
+ </tr>
266
+ <tr>
267
+ <td>metrics</td>
268
+ <td>select</td>
269
+ </tr>
270
+ <tr>
271
+ <td>dimensions</td>
272
+ <td>with</td>
273
+ </tr>
274
+ <tr>
275
+ <td>sort</td>
276
+ <td></td>
277
+ </tr>
278
+ <tr>
279
+ <td>filters</td>
280
+ <td>where</td>
281
+ </tr>
282
+ <tr>
283
+ <td>segment</td>
284
+ <td></td>
285
+ </tr>
286
+ <tr>
287
+ <td>start_index</td>
288
+ <td>offset</td>
289
+ </tr>
290
+ <tr>
291
+ <td>max_results</td>
292
+ <td>offset</td>
293
+ </tr>
294
+ </tbody>
295
+ </table>
296
+
297
+
243
298
  > ## ids
244
299
  >
245
- > <a href="https://developers.google.com/analytics/devguides/reporting/core/v3/reference#ids" target="_blank" style="float:right">doc</a>
246
- >
247
- > **alias:** id
300
+ > **doc:** <a href="https://developers.google.com/analytics/devguides/reporting/core/v3/reference#ids" target="_blank">doc</a>
248
301
  >
249
302
  > Id of profile, by default is use id from GoogleApi::Ga.id.
250
303
 
251
304
  <br>
252
305
 
253
- > ## start_date
254
- >
255
- > <a href="https://developers.google.com/analytics/devguides/reporting/core/v3/reference#startDate" target="_blank" style="float:right">doc</a>
306
+ > ## start_date, &nbsp;&nbsp; end_date
256
307
  >
257
- > **alias:** from
308
+ > **doc:** <a href="https://developers.google.com/analytics/devguides/reporting/core/v3/reference#startDate" target="_blank">start-date</a>, <a href="https://developers.google.com/analytics/devguides/reporting/core/v3/reference#startDate" target="_blank">end-date</a>
258
309
  >
259
310
  > **default:** _Date.today_<br>
260
311
  > **parameters:**<br>
@@ -263,59 +314,19 @@ GoogleApi::Ga::Data
263
314
 
264
315
  <br>
265
316
 
266
- > ## end_date
267
- >
268
- > <a href="https://developers.google.com/analytics/devguides/reporting/core/v3/reference#startDate" target="_blank" style="float:right">doc</a>
317
+ > ## metrics, &nbsp;&nbsp; dimensions, &nbsp;&nbsp; sort
269
318
  >
270
- > **alias:** from
271
- >
272
- > **default:** _Date.today_<br>
273
- > **parameters:**<br>
274
- > &nbsp;&nbsp; _String_ in YYYY-MM-DD or _Date_ or _DateTime_ or _Time_<br>
275
- > &nbsp;&nbsp; _Integer_ for add or sub days from _Date.today_
276
-
277
- <br>
278
-
279
- > ## metrics
319
+ > **doc:** <a href="https://developers.google.com/analytics/devguides/reporting/core/v3/reference#metrics" target="_blank">metrics</a>, <a href="https://developers.google.com/analytics/devguides/reporting/core/v3/reference#dimensions" target="_blank">dimensions</a>, <a href="https://developers.google.com/analytics/devguides/reporting/core/v3/reference#sort" target="_blank">sort</a>
280
320
  >
281
- > <a href="https://developers.google.com/analytics/devguides/reporting/core/v3/reference#metrics" target="_blank" style="float:right">doc</a>
282
- >
283
- > required parameters
284
- >
285
- > **alias:** select
286
- >
287
- > you can also add or sub parameters with `.metrics_add` or `.metrics_sub`
288
- >
289
- > **parameters:** Array with String or Symbol, String (not compiled, <i>"ga:visitors"</i>) or Symbol (compiled, <i>:visitors</i>)
290
-
291
- <br>
292
-
293
- > ## dimensions
294
- >
295
- > <a href="https://developers.google.com/analytics/devguides/reporting/core/v3/reference#dimensions" target="_blank" style="float:right">doc</a>
296
- >
297
- > **alias:** with
298
- >
299
- > you can also add or sub parameters with `.dimensions_add` or `.dimensions_sub`
321
+ > you can also add or sub parameters with `.method_add` or `.method_sub`
300
322
  >
301
323
  > **parameters:** Array with String or Symbol, String (not compiled, <i>"ga:visitors"</i>) or Symbol (compiled, <i>:visitors</i>)
302
324
 
303
325
  <br>
304
326
 
305
- > ## sort
327
+ > ## filters, &nbsp;&nbsp; segment
306
328
  >
307
- > <a href="https://developers.google.com/analytics/devguides/reporting/core/v3/reference#sort" target="_blank" style="float:right">doc</a>
308
- > you can also add or sub parameters with `.sort_add` or `.sort_sub`
309
- >
310
- > **parameters:** Array with String or Symbol, String (not compiled, <i>"ga:visitors"</i>) or Symbol (compiled, <i>:visitors</i>)
311
-
312
- <br>
313
-
314
- > ## filters
315
- >
316
- > <a href="https://developers.google.com/analytics/devguides/reporting/core/v3/reference#filters" target="_blank" style="float:right">doc</a>
317
- >
318
- > **alias:** where
329
+ > **doc:** <a href="https://developers.google.com/analytics/devguides/reporting/core/v3/reference#filters" target="_blank">filters</a>, <a href="https://developers.google.com/analytics/devguides/reporting/core/v3/reference#segment" target="_blank">segment</a>
319
330
  >
320
331
  > **parameters:**<br>
321
332
  > {(attribute operator value) & (attribute operator value) | (attribute operator value)} or String (not compiled)
@@ -326,25 +337,9 @@ GoogleApi::Ga::Data
326
337
 
327
338
  <br>
328
339
 
329
- > ## segment
330
- >
331
- > <a href="https://developers.google.com/analytics/devguides/reporting/core/v3/reference#segment" target="_blank" style="float:right">doc</a>
332
- >
333
- > **parameters:**<br>
334
- > {(attribute operator value) & (attribute operator value) | (attribute operator value)} or String (not compiled) <br>
335
- > with {} automaticly add "dynamic::"
336
- >
337
- > **operators:** ==, !=, >, <, >=, <=, =~, !~<br>
338
- > % &nbsp;&nbsp;&nbsp; _is &nbsp;=@_<br>
339
- > ** &nbsp;&nbsp;&nbsp; _is &nbsp;&nbsp;!@_
340
-
341
- <br>
342
-
343
340
  > ## start_index
344
341
  >
345
- > <a href="https://developers.google.com/analytics/devguides/reporting/core/v3/reference#startIndex" target="_blank" style="float:right">doc</a>
346
- >
347
- > **alias:** offset
342
+ > **doc:** <a href="https://developers.google.com/analytics/devguides/reporting/core/v3/reference#startIndex" target="_blank">start-index</a>
348
343
  >
349
344
  > **parameters:** Integer from 1.
350
345
 
@@ -352,33 +347,49 @@ GoogleApi::Ga::Data
352
347
 
353
348
  > ## max_results
354
349
  >
355
- > <a href="https://developers.google.com/analytics/devguides/reporting/core/v3/reference#maxResults" target="_blank" style="float:right">doc</a>
356
- >
357
- > **alias:** limit
350
+ > **doc:** <a href="https://developers.google.com/analytics/devguides/reporting/core/v3/reference#maxResults" target="_blank">max-results</a>
358
351
  >
359
352
  > **parameters:** Integer from 1 to 10 000.
360
353
 
361
- <br><br><br>
362
354
 
363
- > ## cache
364
- >
365
- > For how long in minutes will be data cached. Use 0 for never expire.
355
+ ### Cache
366
356
 
367
- <br>
357
+ For how long in minutes will be data cached. Use 0 for never expire.
358
+ ```ruby
359
+ GoogleApi::Ga::Data.cache(minutes)
368
360
 
369
- > ## clear
370
- >
371
- > Clear stored data.
361
+ # you can also clear cache
362
+ GoogleApi::Ga::Data.clear_cache
363
+
364
+ # if you want clear cache and cache new
365
+ GoogleApi::Ga::Data.select(:visits).clear_cache.cache(60)
366
+
367
+ # orif you don't want use cache at all, default: true
368
+ GoogleApi::Ga::Data.use_cache(false)
369
+ ```
370
+
371
+ ### Error
372
+
373
+ What happen if parameters are wrong.
374
+ ```ruby
375
+ # raise error with message
376
+ GoogleApi::Ga::Data.error(true)
377
+
378
+ # default, not raise error, just empty result
379
+ GoogleApi::Ga::Data.error(false)
380
+ ```
372
381
 
373
382
  ### Fetch data
374
383
 
375
384
  You can use one of these. Data is stored in the class.
376
385
 
377
- `.all` - `[header, rows]`<br>
378
- `.rows` - rows returned by google analytics<br>
379
- `.header` - header of data, (`["ga:day", "ga:month", "ga:visitis"]`)<br>
380
- `.count` - number of rows<br>
381
- `.each` - each as you expected, (`|data|` or `|index, data|`)
386
+ ```
387
+ .all # [header, rows]
388
+ .rows # rows returned by google analytics
389
+ .header # header of data, (["ga:day", "ga:month", "ga:visitis"])
390
+ .count # number of rows
391
+ .each # each as you expected, (`|data|` or `|index, data|`)
392
+ ```
382
393
 
383
394
  ### Clear stored data and fetch again
384
395
 
@@ -8,6 +8,8 @@ module GoogleApi
8
8
  @metrics, @dimensions, @sort = [], [], []
9
9
  @filters, @segment = nil, nil
10
10
  @start_index, @max_results = nil, nil
11
+ @error = false
12
+ @use_cache = true
11
13
  end
12
14
 
13
15
  # Auto initialize data
@@ -235,6 +237,39 @@ module GoogleApi
235
237
  end
236
238
  end
237
239
 
240
+ # -----------------------------------------------------------------------------------
241
+ # Type 5
242
+ #
243
+ # Error, use cache
244
+ #
245
+ TYPE_5 = { error: nil,
246
+ use_cache: nil }
247
+ TYPE_5.each do |key, value|
248
+ eval <<-METHOD
249
+ def #{key}(value = nil)
250
+ if value.nil?
251
+ return @#{key}
252
+ end
253
+
254
+ if !value.is_a?(TrueClass) && !value.is_a?(FalseClass)
255
+ raise GoogleApi::TypeError, "Value must be true of false"
256
+ end
257
+
258
+ self.#{key} = value
259
+ self
260
+ end
261
+
262
+ def #{key}=(value)
263
+ @#{key} = value
264
+ end
265
+ METHOD
266
+ end
267
+
268
+ def clear_cache
269
+ _cache.delete(parameters)
270
+ self
271
+ end
272
+
238
273
  # Add row!, header!, all!, count!. First clear and run method .
239
274
  [:rows, :header, :all, :count].each do |value|
240
275
  eval <<-METHOD
@@ -283,7 +318,7 @@ module GoogleApi
283
318
  private
284
319
 
285
320
  def data
286
- @data ||= get.data
321
+ @data ||= get
287
322
  end
288
323
 
289
324
  def parameters
@@ -322,18 +357,22 @@ module GoogleApi
322
357
  end
323
358
 
324
359
  def get
325
- if @cache && _cache.exists?(parameters)
360
+ if @use_cache && _cache.exists?(parameters)
326
361
  return _cache.read(parameters)
327
362
  end
328
363
 
329
364
  result = _session.client.execute( api_method: _session.api.data.ga.get,
330
365
  parameters: parameters )
331
366
 
332
- if result.error?
367
+ if @error && result.error?
333
368
  raise GoogleApi::GaError, result.error_message
334
369
  end
335
370
 
336
- _cache.write(parameters, result, @cache) if @cache.is_a?(Integer)
371
+ result = result.data
372
+
373
+ if @use_cache && !@cache.nil?
374
+ _cache.write(parameters, result, @cache)
375
+ end
337
376
 
338
377
  result
339
378
  end
@@ -1,3 +1,3 @@
1
1
  module GoogleApi
2
- VERSION = "1.0.2"
2
+ VERSION = "1.0.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: google_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: