google_api 1.1.0.beta → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,10 @@
1
+ ## v1.1.0
2
+
3
+ * add some test
4
+ * GoogleApi::Ga::Data.clear now return self
5
+ * GoogleApi::Ga::Data
6
+ * GoogleApi::Shorten.insert and GoogleApi::Shorten.get is available without login
7
+
1
8
  ## v1.1.0.beta
2
9
 
3
10
  * add google urlshorter
data/README.md CHANGED
@@ -2,18 +2,10 @@ Google Api [![Build Status](https://secure.travis-ci.org/ondra-m/google_api.png)
2
2
  ==========
3
3
 
4
4
  Analytics<br>
5
- Url shortener - documentation is not complete yet<br>
5
+ Url shortener<br>
6
6
  Calendar - will be soon
7
7
 
8
- Google Analytics
9
- ================
10
-
11
- <a href="#examples">examples</a>
12
-
13
- News
14
- ----
15
-
16
- For now is released beta version. Pleas use issues for problems.
8
+ More informations in [wiki](https://github.com/ondra-m/google_api/wiki).
17
9
 
18
10
  Installation
19
11
  ------------
@@ -29,481 +21,3 @@ And then execute:
29
21
  Or install it yourself as:
30
22
 
31
23
  `$ gem install google_api`
32
-
33
- Configuration
34
- -------------
35
-
36
- First you must create project at <a href="https://code.google.com/apis/console" target="_blank">google console</a>.
37
-
38
- ```ruby
39
- GoogleApi.configure do
40
- client_id "1"
41
-
42
- ga do
43
- client_id "2"
44
- end
45
- end
46
-
47
- # ----- OR -----
48
-
49
- GoogleApi.config.client_id = "4"
50
- GoogleApi.config.ga.client_id = "5"
51
- ```
52
-
53
- <table>
54
- <thead>
55
- <tr>
56
- <th>name</th>
57
- <th>example</th>
58
- <th>note</th>
59
- </tr>
60
- </thead>
61
-
62
- <tbody>
63
- <tr>
64
- <td>client_id</td>
65
- <td><i>123456.apps.googleusercontent.com</i></td>
66
- <td rowspan="3">required for oauth 2</td>
67
- </tr>
68
- <tr>
69
- <td>client_secret</td>
70
- <td><i>123456123456123456</i></td>
71
- </tr>
72
- <tr>
73
- <td>redirect_uri</td>
74
- <td><i>http://localhost/oauth2callback</i></td>
75
- </tr>
76
- <tr>
77
- <td>client_developer_email</td>
78
- <td><i>123456@developer.gserviceaccount.com</i></td>
79
- <td rowspan="3">required for login by cert</td>
80
- </tr>
81
- <tr>
82
- <td>client_cert_file</td>
83
- <td><i>/home/user/app/123456-privatekey.p12</i></td>
84
- </tr>
85
- <tr>
86
- <td>key_secret</td>
87
- <td><i>notasecret</i></td>
88
- </tr>
89
- <tr>
90
- <td colspan="3">only for ga</td>
91
- </tr>
92
- <tr>
93
- <td>cache</td>
94
- <td><i>default: </i><b>GoogleApi::Cache.new</b></td>
95
- <td>more information <a href="#Cache">Cache</a></td>
96
- </tr>
97
- </tbody>
98
- </table>
99
-
100
- <a name="Cache"></a>
101
- Cache
102
- -----
103
-
104
- Cache must have these methods:
105
-
106
- `write(key, value, expire)` - expire=0 for never expire<br>
107
- `read(key)`<br>
108
- `exists?(key)`<br>
109
- `delete(key)`
110
-
111
- Session
112
- -------
113
-
114
- There is a 3 way for starting sesssion. Client login with username and password is deprecated.
115
-
116
- ### By cert file
117
-
118
- `First you must add your developer email to google analytics profile.`
119
-
120
- ```ruby
121
- # try login once
122
- GoogleApi::Ga::Session.login_by_cert
123
-
124
- # ----- OR -----
125
-
126
- # trying login in loop
127
- GoogleApi::Ga::Session.login_by_cert!
128
- ```
129
-
130
- ### By oauth 2
131
-
132
- ```ruby
133
- # return uri for oauth 2 login
134
- GoogleApi::Ga::Session.login
135
-
136
- # after callback
137
- # code = code key in query (params[:code])
138
- GoogleApi::Ga::Session.login(code)
139
- ```
140
-
141
- **In rails:**
142
-
143
- ```ruby
144
- redirect_to(GoogleApi::Ga::Session.login)
145
-
146
- # in page specified in redirect_uri
147
- GoogleApi::Ga::Session.login(params[:code])
148
- ```
149
-
150
- ### By oauth 2 via line (browser needed)
151
-
152
- This will create TCPServer. After login will be closed.
153
-
154
- **server:** optional is localhost, must be full path!<br>
155
- **port:** on which port the server will listen
156
-
157
- ```ruby
158
- # default:
159
- # server = http://localhost/oauth2callback
160
- # port = 0 - get first free port
161
- GoogleApi::Ga::Session.login_by_line(server, port)
162
- ```
163
-
164
- Management
165
- ----------
166
-
167
- <table>
168
- <thead>
169
- <tr>
170
- <th colspan="2">Account ~~~ Webproperty ~~~ Profile ~~~ Goal ~~~ Segment</th>
171
- </tr>
172
- </thead>
173
- <tbody>
174
- <tr>
175
- <td><b>all</b></td>
176
- <td>find all</td>
177
- </tr>
178
- <tr>
179
- <td><b>find(id)</b></td>
180
- <td>find one by id</td>
181
- </tr>
182
- <tr>
183
- <td><b>refresh</b></td>
184
- <td>refresh data</td>
185
- </tr>
186
- </tbody>
187
- </table>
188
-
189
- #### Account
190
-
191
- ```ruby
192
- # Variables: id, name, created, updated
193
- # Methods: webproperties
194
- GoogleApi::Ga::Account
195
- ```
196
-
197
- #### Webproperty
198
-
199
- ```ruby
200
- # Variables: id, name, created, updated, accountId, websiteUrl
201
- # Methods: account, profiles
202
- GoogleApi::Ga::Webproperty
203
- ```
204
-
205
- #### Profile
206
-
207
- ```ruby
208
- # Variables: id, name, created, updated, accountId, websiteUrl, currency, timezone
209
- # Methods: webproperty, goals
210
- GoogleApi::Ga::Profile
211
- ```
212
-
213
- #### Goal
214
-
215
- ```ruby
216
- # Variables: accountId, webPropertyId, profileId, value, active, type, goal
217
- # Methods: profile
218
- GoogleApi::Ga::Goal
219
- ```
220
-
221
- #### Segment
222
-
223
- ```ruby
224
- # Variables: id, name, created, updated, segmentId, definition
225
- GoogleApi::Ga::Segment
226
- ```
227
-
228
- Set default id
229
- --------------
230
-
231
- ```ruby
232
- GoogleApi::Ga.id(123456) # profile id
233
- ```
234
-
235
- Data
236
- ----
237
-
238
- First you can play on the playground: http://ga-dev-tools.appspot.com/explorer/.
239
-
240
- ```ruby
241
- GoogleApi::Ga::Data
242
- ```
243
-
244
- <br>
245
-
246
- <table>
247
- <thead>
248
- <tr>
249
- <th>method name</th>
250
- <th>alias</th>
251
- </tr>
252
- </thead>
253
-
254
- <tbody>
255
- <tr>
256
- <td>ids</td>
257
- <td>id</td>
258
- </tr>
259
- <tr>
260
- <td>start_date</td>
261
- <td>from</td>
262
- </tr>
263
- <tr>
264
- <td>end_date</td>
265
- <td>to</td>
266
- </tr>
267
- <tr>
268
- <td>metrics</td>
269
- <td>select</td>
270
- </tr>
271
- <tr>
272
- <td>dimensions</td>
273
- <td>with</td>
274
- </tr>
275
- <tr>
276
- <td>sort</td>
277
- <td></td>
278
- </tr>
279
- <tr>
280
- <td>filters</td>
281
- <td>where</td>
282
- </tr>
283
- <tr>
284
- <td>segment</td>
285
- <td></td>
286
- </tr>
287
- <tr>
288
- <td>start_index</td>
289
- <td>offset</td>
290
- </tr>
291
- <tr>
292
- <td>max_results</td>
293
- <td>limit</td>
294
- </tr>
295
- </tbody>
296
- </table>
297
-
298
-
299
- > ## ids
300
- >
301
- > **doc:** <a href="https://developers.google.com/analytics/devguides/reporting/core/v3/reference#ids" target="_blank">doc</a>
302
- >
303
- > Id of profile, by default is use id from GoogleApi::Ga.id.
304
-
305
- <br>
306
-
307
- > ## start_date, &nbsp;&nbsp; end_date
308
- >
309
- > **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>
310
- >
311
- > **default:** _Date.today_<br>
312
- > **parameters:**<br>
313
- > &nbsp;&nbsp; _String_ in YYYY-MM-DD or _Date_ or _DateTime_ or _Time_<br>
314
- > &nbsp;&nbsp; _Integer_ for add or sub days from _Date.today_
315
-
316
- <br>
317
-
318
- > ## metrics, &nbsp;&nbsp; dimensions, &nbsp;&nbsp; sort
319
- >
320
- > **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>
321
- >
322
- > you can also add or sub parameters with `.method_add` or `.method_sub`
323
- >
324
- > **parameters:** Array with String or Symbol, String (not compiled, <i>"ga:visitors"</i>) or Symbol (compiled, <i>:visitors</i>)
325
-
326
- <br>
327
-
328
- > ## filters, &nbsp;&nbsp; segment
329
- >
330
- > **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>
331
- >
332
- > **parameters:**<br>
333
- > {(attribute operator value) & (attribute operator value) | (attribute operator value)} or String (not compiled)
334
- >
335
- > **operators:** ==, !=, >, <, >=, <=, =~, !~<br>
336
- > % &nbsp;&nbsp;&nbsp; _is &nbsp;=@_<br>
337
- > ** &nbsp;&nbsp;&nbsp; _is &nbsp;&nbsp;!@_
338
-
339
- <br>
340
-
341
- > ## start_index
342
- >
343
- > **doc:** <a href="https://developers.google.com/analytics/devguides/reporting/core/v3/reference#startIndex" target="_blank">start-index</a>
344
- >
345
- > **parameters:** Integer from 1.
346
-
347
- <br>
348
-
349
- > ## max_results
350
- >
351
- > **doc:** <a href="https://developers.google.com/analytics/devguides/reporting/core/v3/reference#maxResults" target="_blank">max-results</a>
352
- >
353
- > **parameters:** Integer from 1 to 10 000.
354
-
355
-
356
- ### Cache
357
-
358
- For how long in minutes will be data cached. Use 0 for never expire.
359
- ```ruby
360
- GoogleApi::Ga::Data.cache(minutes)
361
-
362
- # you can also clear cache
363
- # it will clear cache only for entered parameters
364
- GoogleApi::Ga::Data.clear_cache
365
-
366
- # if you want clear cache and cache new
367
- GoogleApi::Ga::Data.select(:visits).clear_cache.cache(60)
368
-
369
- # or if you don't want use cache at all, default: true
370
- GoogleApi::Ga::Data.use_cache(false)
371
- ```
372
-
373
- ### Error
374
-
375
- What happen if parameters are wrong.
376
- ```ruby
377
- # raise error with message
378
- GoogleApi::Ga::Data.error(true)
379
-
380
- # default, not raise error, just empty result
381
- GoogleApi::Ga::Data.error(false)
382
- ```
383
-
384
- ### Fetch data
385
-
386
- You can use one of these. Data is stored in the class.
387
-
388
- ```
389
- .all # [header, rows]
390
- .rows # rows returned by google analytics
391
- .header # header of data, (["ga:day", "ga:month", "ga:visitis"])
392
- .count # number of rows
393
- .each # each as you expected, (|data| or |index, data|)
394
- ```
395
-
396
- ### Clear stored data and fetch again
397
-
398
- If you add some parameters clear is called automatically.
399
-
400
- clear: `.clear`<br>
401
-
402
- clear and fetch new:<br>
403
- `.all!`, `.rows!`, `.header!`, `.count!`, `.each!`<br>
404
-
405
- Examples
406
- --------
407
-
408
- ```ruby
409
- # Start session
410
- # =============
411
-
412
- # set configuration
413
- GoogleApi.config.ga.client_cert_file = "privatekey.p12"
414
- GoogleApi.config.ga.client_developer_email = "123456@developer.gserviceaccount.com"
415
-
416
- # start session
417
- GoogleApi::Ga::Session.login_by_cert!
418
-
419
-
420
-
421
- # Get profile id
422
- # ==============
423
-
424
- # get profile id
425
- id = GoogleApi::Ga::Profile.all.first.id
426
-
427
- # set default id
428
- GoogleApi::Ga.id(id)
429
-
430
-
431
-
432
- # Starting session by line
433
- # ========================
434
-
435
- # First install launchy:
436
- # gem install launchy
437
-
438
- # callback_uri and port are optional - auto start server at localhost
439
- GoogleApi::Ga::Session.login_by_line(callback_uri, port)
440
-
441
- # This will do
442
- # 1) create server
443
- # 2) launch browser and redirect to google api
444
- # 3) confirm and google api redirect to localhost
445
- # 4) server get code and start session
446
- # 5) close server - you are login
447
-
448
-
449
-
450
- # Check session, error if not login
451
- # =================================
452
-
453
- GoogleApi::Ga::Session.check_session
454
-
455
-
456
-
457
- # Management of accounts
458
- # ======================
459
-
460
- # all accounts
461
- accounts = GoogleApi::Ga::Account.all
462
-
463
- # webproperties for account
464
- accounts.first.webproperties
465
-
466
- # all webproperties
467
- GoogleApi::Ga::Webproperty.all
468
-
469
- # all profiles
470
- GoogleApi::Ga::Profile.all
471
-
472
- # all goal
473
- GoogleApi::Ga::Goal.all
474
-
475
- # all segment
476
- GoogleApi::Ga::Segment.all
477
-
478
-
479
-
480
- # Count of visitors between previous month and today
481
- # ==================================================
482
-
483
- GoogleApi::Ga::Data.from(-30).select(:visits).rows
484
-
485
-
486
-
487
- # Count of visitors between previous month and today - 2, and cache it for 30 minutes
488
- # ===================================================================================
489
-
490
- GoogleApi::Ga::Data.from(-30).to(-2).select(:visits).cache(30).rows
491
-
492
-
493
-
494
- # Visitors by day, month, year from Czech Republic. Browser is Firefox and Opera or Chrome
495
- # ========================================================================================
496
-
497
- GoogleApi::Ga::Data.from(-30).select(:visits).with(:day, :month, :year)
498
- .sort(:year, :month, :day)
499
- .where{(country == "Czech Republic") &
500
- (browser == "Firefox") &
501
- (browser == "Opera") |
502
- (browser == "Chrome")}
503
- .rows
504
- # ----- OR -----
505
- GoogleApi::Ga::Data.from(-30).select(:visits).with(:day, :month, :year)
506
- .sort(:year, :month, :day)
507
- .where("ga:country==Czech Republic;ga:browser==Firefox;ga:browser==Opera,ga:browser==Chrome")
508
- .rows
509
- ```
@@ -1,6 +1,5 @@
1
1
  require "google/api_client"
2
2
 
3
- require "google_api/core_ext/hash"
4
3
  require "google_api/configuration"
5
4
  require "google_api/version"
6
5
 
@@ -214,6 +214,8 @@ module GoogleApi
214
214
  @parameters = nil
215
215
  @data = nil
216
216
  @all = nil
217
+
218
+ self
217
219
  end
218
220
 
219
221
  # Clear cache
@@ -223,9 +225,9 @@ module GoogleApi
223
225
  end
224
226
 
225
227
  # Add row!, header!, all!, count!. First clear and run normal method.
226
- [:rows, :header, :all, :count].each do |value|
228
+ [:rows, :header, :all, :count].each do |name|
227
229
  eval <<-METHOD
228
- def #{value}!; clear; #{value}; end
230
+ def #{name}!; clear; #{name}; end
229
231
  METHOD
230
232
  end
231
233
 
@@ -246,16 +248,20 @@ module GoogleApi
246
248
  end
247
249
 
248
250
  def each(&block)
249
- if block.arity == 1 # each
250
- rows.each do |row|
251
- yield(row)
252
- end
253
- else # each with index
254
- i = -1
255
- rows.each do |row|
256
- i += 1
257
- yield(i, row)
258
- end
251
+ case block.arity
252
+ # each
253
+ when 1
254
+ rows.each do |row|
255
+ yield(row)
256
+ end
257
+
258
+ # each with index
259
+ when 2
260
+ i = -1
261
+ rows.each do |row|
262
+ i += 1
263
+ yield(i, row)
264
+ end
259
265
  end
260
266
  end
261
267
 
@@ -2,9 +2,9 @@ module GoogleApi
2
2
  module Ga
3
3
  class Goal < Management
4
4
 
5
- attr_reader :accountId
6
- attr_reader :webPropertyId
7
- attr_reader :profileId
5
+ attr_reader :account_id
6
+ attr_reader :webproperty_id
7
+ attr_reader :profile_id
8
8
  attr_reader :value
9
9
  attr_reader :active
10
10
  attr_reader :type
@@ -25,13 +25,13 @@ module GoogleApi
25
25
  def refresh
26
26
  @profile = nil
27
27
 
28
- set( Goal.get({ accountId: @accountId, webPropertyId: @id, profileId: @profileId, goalId: @id }).first )
28
+ set( Goal.get({ accountId: @account_id, webPropertyId: @webproperty_id, profileId: @profile_id, goalId: @id }).first )
29
29
 
30
30
  return true
31
31
  end
32
32
 
33
33
  def profile
34
- @profile ||= Profile.find(@profileId)
34
+ @profile ||= Profile.find(@profile_id)
35
35
  end
36
36
 
37
37
  private
@@ -41,14 +41,14 @@ module GoogleApi
41
41
  end
42
42
 
43
43
  def set(goal)
44
- @accountId = goal['accountId']
45
- @webPropertyId = goal['webPropertyId']
46
- @profileId = goal['profileId']
47
- @value = goal['value']
48
- @active = goal['active']
49
- @type = goal['type']
50
-
51
- @goal = goal[camelize(@type) + 'Details']
44
+ self.account_id = goal['accountId']
45
+ self.webproperty_id = goal['webPropertyId']
46
+ self.profile_id = goal['profileId']
47
+ self.value = goal['value']
48
+ self.active = goal['active']
49
+ self.type = goal['type']
50
+
51
+ self.goal = goal[camelize(@type) + 'Details']
52
52
 
53
53
  super(goal)
54
54
  end
@@ -25,10 +25,10 @@ module GoogleApi
25
25
  end
26
26
 
27
27
  def set(values)
28
- @id = values['id']
29
- @name = values['name']
30
- @created = values['created']
31
- @updated = values['updated']
28
+ self.id = values['id']
29
+ self.name = values['name']
30
+ self.created = values['created']
31
+ self.updated = values['updated']
32
32
  end
33
33
 
34
34
  end
@@ -2,8 +2,8 @@ module GoogleApi
2
2
  module Ga
3
3
  class Profile < Management
4
4
 
5
- attr_reader :accountId
6
- attr_reader :webPropertyId
5
+ attr_reader :account_id
6
+ attr_reader :webproperty_id
7
7
  attr_reader :currency
8
8
  attr_reader :timezone
9
9
 
@@ -23,27 +23,19 @@ module GoogleApi
23
23
  @webproperty = nil
24
24
  @goals = nil
25
25
 
26
- set( Profile.get({ accountId: @accountId, webPropertyId: @webPropertyId, profileId: @id }).first )
26
+ set( Profile.get({ accountId: @account_id, webPropertyId: @webproperty_id, profileId: @id }).first )
27
27
 
28
28
  return true
29
29
  end
30
30
 
31
31
  def webproperty
32
- @webproperty ||= Webproperty.find(@webpropertyId)
32
+ @webproperty ||= Webproperty.find(@webproperty_id)
33
33
  end
34
34
 
35
35
  def goals
36
- @goals ||= Goal.all(@accountId, @webPropertyId, @id)
36
+ @goals ||= Goal.all(@account_id, @webproperty_id, @id)
37
37
  end
38
38
 
39
- # def get(parameters, start_date = prev_month, end_date = now, expire = nil)
40
- # Data.get(@id, parameters, start_date, end_date, expire)
41
- # end
42
-
43
- # def get!(parameters, start_date = prev_month, end_date = now)
44
- # Data.get!(@id, parameters, start_date, end_date)
45
- # end
46
-
47
39
  private
48
40
 
49
41
  def self.get(parameters = nil)
@@ -51,10 +43,10 @@ module GoogleApi
51
43
  end
52
44
 
53
45
  def set(profile)
54
- @accountId = profile['accountId']
55
- @webPropertyId = profile['webPropertyId']
56
- @currency = profile['currency']
57
- @timezone = profile['timezone']
46
+ self.account_id = profile['accountId']
47
+ self.webproperty_id = profile['webPropertyId']
48
+ self.currency = profile['currency']
49
+ self.timezone = profile['timezone']
58
50
 
59
51
  super(profile)
60
52
  end
@@ -2,7 +2,7 @@ module GoogleApi
2
2
  module Ga
3
3
  class Segment < Management
4
4
 
5
- attr_accessor :segmentId
5
+ attr_accessor :segment_id
6
6
  attr_accessor :definition
7
7
 
8
8
  def initialize(segment)
@@ -30,8 +30,8 @@ module GoogleApi
30
30
  end
31
31
 
32
32
  def set(segment)
33
- @segmentId = segment['segmentId']
34
- @definition = segment['definition']
33
+ self.segment_id = segment['segmentId']
34
+ self.definition = segment['definition']
35
35
 
36
36
  super(segment)
37
37
  end
@@ -2,8 +2,8 @@ module GoogleApi
2
2
  module Ga
3
3
  class Webproperty < Management
4
4
 
5
- attr_reader :accountId
6
- attr_reader :websiteUrl
5
+ attr_reader :account_id
6
+ attr_reader :website_url
7
7
 
8
8
  def initialize(webproperty)
9
9
  set(webproperty)
@@ -21,17 +21,17 @@ module GoogleApi
21
21
  @account = nil
22
22
  @profiles = nil
23
23
 
24
- set( Webproperty.get({ accountId: @accountId, webPropertyId: @id }).first )
24
+ set( Webproperty.get({ accountId: @account_id, webPropertyId: @id }).first )
25
25
 
26
26
  return true
27
27
  end
28
28
 
29
29
  def account
30
- @account ||= Account.find(@accountId)
30
+ @account ||= Account.find(@account_id)
31
31
  end
32
32
 
33
33
  def profiles
34
- @profiles ||= Profile.all(@accountId, @id)
34
+ @profiles ||= Profile.all(@account_id, @id)
35
35
  end
36
36
 
37
37
  private
@@ -41,8 +41,8 @@ module GoogleApi
41
41
  end
42
42
 
43
43
  def set(webproperty)
44
- @accountId = webproperty['accountId']
45
- @websiteUrl = webproperty['websiteUrl']
44
+ self.account_id = webproperty['accountId']
45
+ self.website_url = webproperty['websiteUrl']
46
46
 
47
47
  super(webproperty)
48
48
  end
@@ -3,14 +3,16 @@ module GoogleApi
3
3
 
4
4
  CONFIGURATION = {}
5
5
 
6
+ URLSHORTENER_URI = 'https://www.googleapis.com/urlshortener/v1/url'
7
+
6
8
  # Session
7
9
  autoload :Session, 'google_api/shorten/session'
8
10
 
9
11
  # Url
10
- autoload :Url, 'google_api/shorten/url/url'
12
+ autoload :Url, 'google_api/shorten/url/url'
11
13
  autoload :Insert, 'google_api/shorten/url/insert'
12
- autoload :List, 'google_api/shorten/url/list'
13
- autoload :Get, 'google_api/shorten/url/get'
14
+ autoload :List, 'google_api/shorten/url/list'
15
+ autoload :Get, 'google_api/shorten/url/get'
14
16
 
15
17
  def self.insert(url)
16
18
  Insert.new(url)
@@ -16,17 +16,34 @@ module GoogleApi
16
16
  raise GoogleApi::ShortenError, "Invalid projection. Must be #{PROJECTION_LIST.join(',')}."
17
17
  end
18
18
 
19
- get
19
+ @data = _session.check_session ? get : get_without_login
20
20
  end
21
21
 
22
22
  private
23
23
 
24
24
  def get
25
- @data = JSON.parse(_session.client.execute( api_method: _session.api.url.get,
26
- parameters: { shortUrl: @short_url,
27
- projection: @projection } ).response.env[:body])
25
+ JSON.parse(_session.client.execute( api_method: _session.api.url.get,
26
+ parameters: { shortUrl: @short_url,
27
+ projection: @projection } ).body)
28
28
  end
29
-
29
+
30
+ def get_without_login
31
+ require 'net/http'
32
+ require 'json'
33
+
34
+ uri = URI(Shorten::URLSHORTENER_URI)
35
+ uri.query = URI.encode_www_form({ shortUrl: @short_url,
36
+ projection: @projection })
37
+
38
+ res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
39
+ req = Net::HTTP::Get.new(uri.request_uri)
40
+
41
+ http.request(req)
42
+ end
43
+
44
+ JSON.parse(res.body)
45
+ end
46
+
30
47
  end
31
48
  end
32
49
  end
@@ -8,7 +8,7 @@ module GoogleApi
8
8
  def initialize(long_url)
9
9
  @long_url = long_url
10
10
 
11
- get
11
+ @short_url = _session.check_session ? get : get_without_login
12
12
  end
13
13
 
14
14
  def details(projection = 'FULL')
@@ -18,9 +18,26 @@ module GoogleApi
18
18
  private
19
19
 
20
20
  def get
21
- @short_url = _session.client.execute( api_method: _session.api.url.insert,
22
- body: { longUrl: @long_url }.to_json,
23
- headers: {'Content-Type' => 'application/json'} ).data.id
21
+ _session.client.execute( api_method: _session.api.url.insert,
22
+ body: { longUrl: @long_url }.to_json,
23
+ headers: {'Content-Type' => 'application/json'} ).data.id
24
+ end
25
+
26
+ def get_without_login
27
+ require 'net/http'
28
+ require 'json'
29
+
30
+ uri = URI(Shorten::URLSHORTENER_URI)
31
+
32
+ req = Net::HTTP::Post.new(uri.path)
33
+ req.body = {longUrl: @long_url}.to_json
34
+ req.content_type = 'application/json'
35
+
36
+ res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
37
+ http.request(req)
38
+ end
39
+
40
+ JSON.parse(res.body)['id']
24
41
  end
25
42
 
26
43
  end
@@ -3,7 +3,7 @@ module GoogleApi
3
3
  class List < Url
4
4
 
5
5
  def initialize
6
- get
6
+ @data = get
7
7
  end
8
8
 
9
9
  def count
@@ -17,8 +17,10 @@ module GoogleApi
17
17
  private
18
18
 
19
19
  def get
20
- @data = JSON.parse(_session.client.execute( api_method: _session.api.url.list,
21
- parameters: { projection: 'FULL' } ).response.env[:body])
20
+ JSON.parse(
21
+ _session.client.execute( api_method: _session.api.url.list,
22
+ parameters: { projection: 'FULL' } ).body
23
+ )
22
24
  end
23
25
 
24
26
  end
@@ -1,3 +1,3 @@
1
1
  module GoogleApi
2
- VERSION = "1.1.0.beta"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -0,0 +1,93 @@
1
+ require "spec_helper"
2
+
3
+ describe "GoogleApi::Configuration" do
4
+ it "Dsl" do
5
+ GoogleApi.configure do
6
+ client_id "1"
7
+ client_secret "2"
8
+ client_developer_email "3"
9
+
10
+ ga do
11
+ client_id "4"
12
+ client_secret "5"
13
+ client_developer_email "6"
14
+ end
15
+
16
+ shorten do
17
+ client_id "7"
18
+ client_secret "8"
19
+ client_developer_email "9"
20
+ end
21
+ end
22
+
23
+ GoogleApi.config.client_id.should eq("1")
24
+ GoogleApi.config.client_secret.should eq("2")
25
+ GoogleApi.config.client_developer_email.should eq("3")
26
+
27
+ GoogleApi.config.ga.client_id.should eq("4")
28
+ GoogleApi.config.ga.client_secret.should eq("5")
29
+ GoogleApi.config.ga.client_developer_email.should eq("6")
30
+
31
+ GoogleApi.config.shorten.client_id.should eq("7")
32
+ GoogleApi.config.shorten.client_secret.should eq("8")
33
+ GoogleApi.config.shorten.client_developer_email.should eq("9")
34
+ end
35
+
36
+ it "Classic" do
37
+ GoogleApi.configure do |config|
38
+ config.client_id "1"
39
+ config.client_secret "2"
40
+ config.client_developer_email "3"
41
+
42
+ config.ga do |ga_config|
43
+ ga_config.client_id "4"
44
+ ga_config.client_secret "5"
45
+ ga_config.client_developer_email "6"
46
+ end
47
+
48
+ config.shorten do |shorten_config|
49
+ shorten_config.client_id "7"
50
+ shorten_config.client_secret "8"
51
+ shorten_config.client_developer_email "9"
52
+ end
53
+ end
54
+
55
+ GoogleApi.config.client_id.should eq("1")
56
+ GoogleApi.config.client_secret.should eq("2")
57
+ GoogleApi.config.client_developer_email.should eq("3")
58
+
59
+ GoogleApi.config.ga.client_id.should eq("4")
60
+ GoogleApi.config.ga.client_secret.should eq("5")
61
+ GoogleApi.config.ga.client_developer_email.should eq("6")
62
+
63
+ GoogleApi.config.shorten.client_id.should eq("7")
64
+ GoogleApi.config.shorten.client_secret.should eq("8")
65
+ GoogleApi.config.shorten.client_developer_email.should eq("9")
66
+ end
67
+
68
+ it "Minimal" do
69
+ GoogleApi.config.client_id = "1"
70
+ GoogleApi.config.client_secret = "2"
71
+ GoogleApi.config.client_developer_email = "3"
72
+
73
+ GoogleApi.config.ga.client_id = "4"
74
+ GoogleApi.config.ga.client_secret = "5"
75
+ GoogleApi.config.ga.client_developer_email = "6"
76
+
77
+ GoogleApi.config.shorten.client_id = "7"
78
+ GoogleApi.config.shorten.client_secret = "8"
79
+ GoogleApi.config.shorten.client_developer_email = "9"
80
+
81
+ GoogleApi.config.client_id.should eq("1")
82
+ GoogleApi.config.client_secret.should eq("2")
83
+ GoogleApi.config.client_developer_email.should eq("3")
84
+
85
+ GoogleApi.config.ga.client_id.should eq("4")
86
+ GoogleApi.config.ga.client_secret.should eq("5")
87
+ GoogleApi.config.ga.client_developer_email.should eq("6")
88
+
89
+ GoogleApi.config.shorten.client_id.should eq("7")
90
+ GoogleApi.config.shorten.client_secret.should eq("8")
91
+ GoogleApi.config.shorten.client_developer_email.should eq("9")
92
+ end
93
+ end
@@ -87,14 +87,44 @@ describe "GoogleApi::Ga::Data" do
87
87
 
88
88
 
89
89
 
90
- # Date add and sub
90
+ # Save parameters
91
+ # -------------------------------------------------------------------------------------------------
92
+ it "save parameters" do
93
+ @data = @data.id(1)
94
+ .from(Date.today)
95
+ .to(Date.today)
96
+ .select(:visits)
97
+ .with(:day, :month, "ga:year")
98
+ .where("country==Czech Republic")
99
+ .offset(1)
100
+ .limit(1)
101
+
102
+ @data.id.should eql(1)
103
+ @data.from.should eql(Date.today)
104
+ @data.to.should eql(Date.today)
105
+ @data.select.should eql(["ga:visits"])
106
+ @data.with.should eql(["ga:day", "ga:month", "ga:year"])
107
+ @data.where.should eql("country==Czech Republic")
108
+ @data.offset.should eql(1)
109
+ @data.limit.should eql(1)
110
+ end
111
+
112
+
113
+
114
+ # Date add
91
115
  # -------------------------------------------------------------------------------------------------
92
116
  it "date add and sub" do
93
117
  @data = @data.start_date(+30).end_date(+30)
94
118
 
95
119
  @data.start_date.should eql(Date.today + 30)
96
120
  @data.end_date.should eql(Date.today + 30)
121
+ end
122
+
97
123
 
124
+
125
+ # Date sub
126
+ # -------------------------------------------------------------------------------------------------
127
+ it "date add and sub" do
98
128
  @data = @data.start_date(Date.today).end_date(Date.today)
99
129
  @data = @data.start_date(-30).end_date(-30)
100
130
 
@@ -0,0 +1,20 @@
1
+ require "spec_helper"
2
+
3
+ describe "GoogleApi::Shorten" do
4
+ it "Get." do
5
+ get = GoogleApi::Shorten.get('http://goo.gl/dOpuw')
6
+
7
+ get.data['id'].should eql('http://goo.gl/dOpuw')
8
+ get.data['longUrl'].should eql('https://github.com/ondra-m/google_api')
9
+ end
10
+
11
+ it "Insert short_url is nil if long_url isn't right url." do
12
+ insert = GoogleApi::Shorten.insert('google_api')
13
+
14
+ insert.short_url.should eql(nil)
15
+ end
16
+
17
+ it "List shour raise error." do
18
+ lambda { GoogleApi::Shorten.list }.should raise_error(GoogleApi::SessionError)
19
+ end
20
+ end
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: google_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0.beta
5
- prerelease: 6
4
+ version: 1.1.0
5
+ prerelease:
6
6
  platform: ruby
7
7
  authors:
8
8
  - Ondřej Moravčík
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-23 00:00:00.000000000 Z
12
+ date: 2012-08-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: google-api-client
@@ -78,7 +78,6 @@ files:
78
78
  - lib/google_api.rb
79
79
  - lib/google_api/cache.rb
80
80
  - lib/google_api/configuration.rb
81
- - lib/google_api/core_ext/hash.rb
82
81
  - lib/google_api/core_ext/string.rb
83
82
  - lib/google_api/ga/data.rb
84
83
  - lib/google_api/ga/data/data_dsl.rb
@@ -101,10 +100,11 @@ files:
101
100
  - lib/google_api/shorten/url/list.rb
102
101
  - lib/google_api/shorten/url/url.rb
103
102
  - lib/google_api/version.rb
104
- - spec/lib/google_api_cache_spec.rb
105
- - spec/lib/google_api_ga_data_spec.rb
106
- - spec/lib/google_api_ga_spec.rb
107
- - spec/lib/google_api_spec.rb
103
+ - spec/lib/cache_spec.rb
104
+ - spec/lib/configuration_spec.rb
105
+ - spec/lib/ga_data_spec.rb
106
+ - spec/lib/ga_spec.rb
107
+ - spec/lib/shorten_spec.rb
108
108
  - spec/spec_helper.rb
109
109
  homepage: https://github.com/ondra-m/google_api
110
110
  licenses: []
@@ -121,9 +121,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
121
121
  required_rubygems_version: !ruby/object:Gem::Requirement
122
122
  none: false
123
123
  requirements:
124
- - - ! '>'
124
+ - - ! '>='
125
125
  - !ruby/object:Gem::Version
126
- version: 1.3.1
126
+ version: '0'
127
127
  requirements: []
128
128
  rubyforge_project:
129
129
  rubygems_version: 1.8.23
@@ -131,9 +131,10 @@ signing_key:
131
131
  specification_version: 3
132
132
  summary: Simple Google Api. Include google analytics, url shortener.
133
133
  test_files:
134
- - spec/lib/google_api_cache_spec.rb
135
- - spec/lib/google_api_ga_data_spec.rb
136
- - spec/lib/google_api_ga_spec.rb
137
- - spec/lib/google_api_spec.rb
134
+ - spec/lib/cache_spec.rb
135
+ - spec/lib/configuration_spec.rb
136
+ - spec/lib/ga_data_spec.rb
137
+ - spec/lib/ga_spec.rb
138
+ - spec/lib/shorten_spec.rb
138
139
  - spec/spec_helper.rb
139
140
  has_rdoc:
@@ -1,11 +0,0 @@
1
- unless Hash.new.respond_to?(:symbolize_keys!)
2
- class Hash
3
- def symbolize_keys!
4
- keys.each do |key|
5
- self[(key.to_sym rescue key) || key] = delete(key)
6
- end
7
-
8
- self
9
- end
10
- end
11
- end
@@ -1,61 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe "GoogleApi::Configuration" do
4
- it "Setting is right." do
5
- GoogleApi.configure do
6
- client_id "1"
7
- client_secret "2"
8
- client_developer_email "3"
9
-
10
- ga do
11
- client_id "4"
12
- client_secret "5"
13
- client_developer_email "6"
14
- end
15
- end
16
-
17
- GoogleApi.config.client_id.should eq("1")
18
- GoogleApi.config.client_secret.should eq("2")
19
- GoogleApi.config.client_developer_email.should eq("3")
20
-
21
- GoogleApi.config.ga.client_id.should eq("4")
22
- GoogleApi.config.ga.client_secret.should eq("5")
23
- GoogleApi.config.ga.client_developer_email.should eq("6")
24
-
25
- GoogleApi.configure do |config|
26
- config.client_id "7"
27
- config.client_secret "8"
28
- config.client_developer_email "9"
29
-
30
- config.ga do |ga_config|
31
- ga_config.client_id "10"
32
- ga_config.client_secret "11"
33
- ga_config.client_developer_email "12"
34
- end
35
- end
36
-
37
- GoogleApi.config.client_id.should eq("7")
38
- GoogleApi.config.client_secret.should eq("8")
39
- GoogleApi.config.client_developer_email.should eq("9")
40
-
41
- GoogleApi.config.ga.client_id.should eq("10")
42
- GoogleApi.config.ga.client_secret.should eq("11")
43
- GoogleApi.config.ga.client_developer_email.should eq("12")
44
-
45
- GoogleApi.config.client_id = "13"
46
- GoogleApi.config.client_secret = "14"
47
- GoogleApi.config.client_developer_email = "15"
48
-
49
- GoogleApi.config.ga.client_id = "16"
50
- GoogleApi.config.ga.client_secret = "17"
51
- GoogleApi.config.ga.client_developer_email = "18"
52
-
53
- GoogleApi.config.client_id.should eq("13")
54
- GoogleApi.config.client_secret.should eq("14")
55
- GoogleApi.config.client_developer_email.should eq("15")
56
-
57
- GoogleApi.config.ga.client_id.should eq("16")
58
- GoogleApi.config.ga.client_secret.should eq("17")
59
- GoogleApi.config.ga.client_developer_email.should eq("18")
60
- end
61
- end