google_api 1.0.3 → 1.1.0.beta
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.
- data/CHANGELOG.md +8 -0
- data/README.md +8 -6
- data/google_api.gemspec +2 -2
- data/lib/google_api.rb +10 -9
- data/lib/google_api/configuration.rb +19 -5
- data/lib/google_api/ga/data.rb +154 -173
- data/lib/google_api/{ga.rb → ga/ga.rb} +0 -12
- data/lib/google_api/ga/session.rb +3 -143
- data/lib/google_api/session/session.rb +202 -0
- data/lib/google_api/session/session_methods.rb +41 -0
- data/lib/google_api/shorten/session.rb +16 -0
- data/lib/google_api/shorten/shorten.rb +28 -0
- data/lib/google_api/shorten/url/get.rb +32 -0
- data/lib/google_api/shorten/url/insert.rb +28 -0
- data/lib/google_api/shorten/url/list.rb +26 -0
- data/lib/google_api/shorten/url/url.rb +11 -0
- data/lib/google_api/version.rb +1 -1
- data/spec/lib/google_api_ga_data_spec.rb +0 -8
- metadata +16 -10
- data/lib/google_api/date.rb +0 -58
- data/lib/google_api/ga/helper.rb +0 -41
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,11 @@
|
|
1
|
+
## v1.1.0.beta
|
2
|
+
|
3
|
+
* add google urlshorter
|
4
|
+
* login_by_cert! can be end by ctr+c
|
5
|
+
* session start logic moved to GoogleApi::Session
|
6
|
+
* add check_session without raise error
|
7
|
+
* fix: login_by_line are now first write messae and then write
|
8
|
+
|
1
9
|
## v1.0.3
|
2
10
|
|
3
11
|
* GoogleApi::Ga::Data has now silent error, GoogleApi::Ga::Data.error(true) - raise error, GoogleApi::Ga::Data.error(false) - not error (default)
|
data/README.md
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
Google Api [](http://travis-ci.org/ondra-m/google_api)
|
2
2
|
==========
|
3
3
|
|
4
|
-
Analytics
|
4
|
+
Analytics<br>
|
5
|
+
Url shortener - documentation is not complete yet<br>
|
5
6
|
Calendar - will be soon
|
6
7
|
|
7
8
|
Google Analytics
|
@@ -289,7 +290,7 @@ GoogleApi::Ga::Data
|
|
289
290
|
</tr>
|
290
291
|
<tr>
|
291
292
|
<td>max_results</td>
|
292
|
-
<td>
|
293
|
+
<td>limit</td>
|
293
294
|
</tr>
|
294
295
|
</tbody>
|
295
296
|
</table>
|
@@ -359,12 +360,13 @@ For how long in minutes will be data cached. Use 0 for never expire.
|
|
359
360
|
GoogleApi::Ga::Data.cache(minutes)
|
360
361
|
|
361
362
|
# you can also clear cache
|
363
|
+
# it will clear cache only for entered parameters
|
362
364
|
GoogleApi::Ga::Data.clear_cache
|
363
365
|
|
364
366
|
# if you want clear cache and cache new
|
365
367
|
GoogleApi::Ga::Data.select(:visits).clear_cache.cache(60)
|
366
368
|
|
367
|
-
#
|
369
|
+
# or if you don't want use cache at all, default: true
|
368
370
|
GoogleApi::Ga::Data.use_cache(false)
|
369
371
|
```
|
370
372
|
|
@@ -388,12 +390,12 @@ You can use one of these. Data is stored in the class.
|
|
388
390
|
.rows # rows returned by google analytics
|
389
391
|
.header # header of data, (["ga:day", "ga:month", "ga:visitis"])
|
390
392
|
.count # number of rows
|
391
|
-
.each # each as you expected, (
|
393
|
+
.each # each as you expected, (|data| or |index, data|)
|
392
394
|
```
|
393
395
|
|
394
396
|
### Clear stored data and fetch again
|
395
397
|
|
396
|
-
If you add some parameters clear is called
|
398
|
+
If you add some parameters clear is called automatically.
|
397
399
|
|
398
400
|
clear: `.clear`<br>
|
399
401
|
|
@@ -448,7 +450,7 @@ Examples
|
|
448
450
|
# Check session, error if not login
|
449
451
|
# =================================
|
450
452
|
|
451
|
-
GoogleApi::Ga::Session.check_session
|
453
|
+
GoogleApi::Ga::Session.check_session
|
452
454
|
|
453
455
|
|
454
456
|
|
data/google_api.gemspec
CHANGED
@@ -4,8 +4,8 @@ require File.expand_path('../lib/google_api/version', __FILE__)
|
|
4
4
|
Gem::Specification.new do |gem|
|
5
5
|
gem.authors = ["Ondřej Moravčík"]
|
6
6
|
gem.email = ["moravcik.ondrej@gmail.com"]
|
7
|
-
gem.description = %q{Simple Google Api. Include google analytics.}
|
8
|
-
gem.summary = %q{Simple Google Api. Include google analytics.}
|
7
|
+
gem.description = %q{Simple Google Api. Include google analytics, url shortener.}
|
8
|
+
gem.summary = %q{Simple Google Api. Include google analytics, url shortener.}
|
9
9
|
gem.homepage = "https://github.com/ondra-m/google_api"
|
10
10
|
|
11
11
|
gem.files = `git ls-files`.split($\)
|
data/lib/google_api.rb
CHANGED
@@ -6,25 +6,26 @@ require "google_api/version"
|
|
6
6
|
|
7
7
|
module GoogleApi
|
8
8
|
|
9
|
+
autoload :Session, 'google_api/session/session'
|
10
|
+
autoload :SessionMethods, 'google_api/session/session_methods'
|
11
|
+
|
9
12
|
autoload :Cache, 'google_api/cache'
|
10
13
|
|
11
|
-
autoload :Ga,
|
14
|
+
autoload :Ga, 'google_api/ga/ga' # Google analytics
|
15
|
+
autoload :Shorten, 'google_api/shorten/shorten' # Google urlshortener
|
12
16
|
|
13
17
|
class SessionError < StandardError; end
|
14
18
|
class GaError < StandardError; end
|
19
|
+
class ShortenError < StandardError; end
|
15
20
|
class DateError < StandardError; end
|
16
21
|
class TypeError < StandardError; end
|
17
22
|
class CanBeNilError < StandardError; end
|
23
|
+
class RequireError < StandardError; end
|
18
24
|
|
25
|
+
# Global configuration
|
19
26
|
CONFIGURATION = {
|
20
|
-
|
21
|
-
|
22
|
-
client_developer_email: nil,
|
23
|
-
client_cert_file: nil,
|
24
|
-
key_secret: 'notasecret',
|
25
|
-
redirect_uri: nil,
|
26
|
-
|
27
|
-
ga: Configuration.new(Ga::CONFIGURATION)
|
27
|
+
ga: Configuration.new(Ga::CONFIGURATION),
|
28
|
+
shorten: Configuration.new(Shorten::CONFIGURATION)
|
28
29
|
}
|
29
30
|
|
30
31
|
def self.config
|
@@ -1,13 +1,23 @@
|
|
1
1
|
module GoogleApi
|
2
2
|
class Configuration
|
3
3
|
|
4
|
-
|
4
|
+
DEFAULT = {
|
5
|
+
client_id: nil,
|
6
|
+
client_secret: nil,
|
7
|
+
client_developer_email: nil,
|
8
|
+
client_cert_file: nil,
|
9
|
+
key_secret: 'notasecret',
|
10
|
+
redirect_uri: nil,
|
11
|
+
}
|
12
|
+
|
13
|
+
def initialize(config, use_default = true)
|
14
|
+
|
15
|
+
if use_default
|
16
|
+
config = DEFAULT.merge(config)
|
17
|
+
end
|
18
|
+
|
5
19
|
config.each do |key, value|
|
6
20
|
eval <<-METHOD
|
7
|
-
def #{key}=(value)
|
8
|
-
@#{key} = value
|
9
|
-
end
|
10
|
-
|
11
21
|
def #{key}(value = nil, &block)
|
12
22
|
if block_given?
|
13
23
|
@#{key}.instance_eval(&block)
|
@@ -19,6 +29,10 @@ module GoogleApi
|
|
19
29
|
|
20
30
|
self.#{key} = value
|
21
31
|
end
|
32
|
+
|
33
|
+
def #{key}=(value)
|
34
|
+
@#{key} = value
|
35
|
+
end
|
22
36
|
METHOD
|
23
37
|
|
24
38
|
self.send("#{key}=", value)
|
data/lib/google_api/ga/data.rb
CHANGED
@@ -2,6 +2,8 @@ module GoogleApi
|
|
2
2
|
module Ga
|
3
3
|
class Data
|
4
4
|
|
5
|
+
# Initialize ----------------------------------------------------------------------------------
|
6
|
+
|
5
7
|
def initialize
|
6
8
|
@ids, @cache = nil, nil
|
7
9
|
@start_date, @end_date = Date.today, Date.today
|
@@ -21,262 +23,209 @@ module GoogleApi
|
|
21
23
|
end
|
22
24
|
end
|
23
25
|
|
24
|
-
#
|
25
|
-
DATE_FORMAT = /\A[0-9]{4}-[0-9]{2}-[0-9]{2}\Z/
|
26
|
-
|
27
|
-
def to_date(date)
|
28
|
-
if date.is_a?(String)
|
29
|
-
unless date =~ DATE_FORMAT
|
30
|
-
raise GoogleApi::DateError, "Date: #{date} must match with #{DATE_FORMAT}."
|
31
|
-
end
|
32
|
-
|
33
|
-
date = Date.parse(date)
|
34
|
-
end
|
26
|
+
# Parameters for google analytics -------------------------------------------------------------
|
35
27
|
|
36
|
-
|
37
|
-
|
28
|
+
TYPE_INTEGER = { ids: 'id',
|
29
|
+
cache: nil,
|
30
|
+
start_index: 'offset',
|
31
|
+
max_results: 'limit' }
|
38
32
|
|
39
|
-
|
40
|
-
|
41
|
-
type?(value, Array)
|
33
|
+
TYPE_DATE = { start_date: 'from',
|
34
|
+
end_date: 'to' }
|
42
35
|
|
43
|
-
|
44
|
-
|
36
|
+
TYPE_ARRAY = { metrics: 'select',
|
37
|
+
dimensions: 'with',
|
38
|
+
sort: nil }
|
45
39
|
|
46
|
-
|
47
|
-
|
48
|
-
unless value.is_a?(type)
|
49
|
-
raise GoogleApi::TypeError, "Value: #{value} must be #{type}."
|
50
|
-
end
|
51
|
-
end
|
40
|
+
TYPE_BLOCK = { filters: 'where',
|
41
|
+
segment: nil }
|
52
42
|
|
53
|
-
|
54
|
-
|
55
|
-
@header = nil
|
56
|
-
@parameters = nil
|
57
|
-
@data = nil
|
58
|
-
@all = nil
|
59
|
-
end
|
43
|
+
TYPE_BOOLEAN = { error: nil,
|
44
|
+
use_cache: nil }
|
60
45
|
|
61
|
-
#
|
62
|
-
#
|
46
|
+
# Create a base method
|
47
|
+
# ids, cache, start_index, max_results, start_date, end_date, error, use_cache
|
63
48
|
#
|
64
|
-
#
|
49
|
+
# Example:
|
65
50
|
#
|
66
|
-
#
|
51
|
+
# def ids(value = nil)
|
52
|
+
# if value.nil?
|
53
|
+
# return @ids
|
54
|
+
# end
|
67
55
|
#
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
TYPE_1.each do |key, value|
|
56
|
+
# self.ids = value
|
57
|
+
# self
|
58
|
+
# end
|
59
|
+
#
|
60
|
+
def self.create_base_method(name)
|
74
61
|
eval <<-METHOD
|
75
|
-
def #{
|
62
|
+
def #{name}(value = nil)
|
76
63
|
if value.nil?
|
77
|
-
return @#{
|
64
|
+
return @#{name}
|
78
65
|
end
|
79
66
|
|
80
|
-
self.#{
|
67
|
+
self.#{name} = value
|
81
68
|
self
|
82
69
|
end
|
83
|
-
|
84
|
-
def #{key}=(value)
|
85
|
-
value = value.to_i
|
86
|
-
|
87
|
-
@#{key} = value
|
88
|
-
end
|
89
70
|
METHOD
|
90
|
-
|
91
|
-
unless value.nil?
|
92
|
-
eval <<-METHOD
|
93
|
-
alias :#{value} :#{key}
|
94
|
-
alias :#{value}= :#{key}=
|
95
|
-
METHOD
|
96
|
-
end
|
97
71
|
end
|
98
72
|
|
99
|
-
#
|
100
|
-
#
|
73
|
+
# Create an array method, suffix is for _add or _sub
|
74
|
+
# metrics, dimensions, sort
|
101
75
|
#
|
102
|
-
#
|
76
|
+
# Example:
|
103
77
|
#
|
104
|
-
#
|
78
|
+
# def metrics(*args)
|
79
|
+
# if args.empty?
|
80
|
+
# return @metrics
|
81
|
+
# end
|
105
82
|
#
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
83
|
+
# self.metrics = args
|
84
|
+
# self
|
85
|
+
# end
|
86
|
+
#
|
87
|
+
# def metrics=(value)
|
88
|
+
# clear
|
89
|
+
# @metrics = build_param(value)
|
90
|
+
# end
|
91
|
+
#
|
92
|
+
def self.create_array_method(name, operator = '', suffix = '')
|
110
93
|
eval <<-METHOD
|
111
|
-
def #{
|
112
|
-
if
|
113
|
-
return @#{
|
94
|
+
def #{name}#{suffix}(*args)
|
95
|
+
if args.empty?
|
96
|
+
return @#{name}
|
114
97
|
end
|
115
98
|
|
116
|
-
self.#{
|
99
|
+
self.#{name}#{suffix} = args
|
117
100
|
self
|
118
101
|
end
|
119
102
|
|
120
|
-
def #{
|
121
|
-
|
122
|
-
|
123
|
-
else
|
124
|
-
@#{key} = to_date(date)
|
125
|
-
end
|
103
|
+
def #{name}#{suffix}=(value)
|
104
|
+
clear
|
105
|
+
@#{name} #{operator}= build_param(value)
|
126
106
|
end
|
127
107
|
METHOD
|
108
|
+
end
|
128
109
|
|
129
|
-
|
110
|
+
# Create a base alias
|
111
|
+
# ids, cache, start_index, max_results, start_date, end_date, metrics, dimensions, sort
|
112
|
+
#
|
113
|
+
# Example:
|
114
|
+
#
|
115
|
+
# alias :id :ids
|
116
|
+
# alias :id= :ids=
|
117
|
+
#
|
118
|
+
def self.create_base_alias(name, method_alias, suffix = '')
|
119
|
+
unless method_alias.nil?
|
130
120
|
eval <<-METHOD
|
131
|
-
alias :#{
|
132
|
-
alias :#{
|
121
|
+
alias :#{method_alias}#{suffix} :#{name}#{suffix}
|
122
|
+
alias :#{method_alias}#{suffix}= :#{name}#{suffix}=
|
133
123
|
METHOD
|
134
124
|
end
|
135
125
|
end
|
136
126
|
|
137
|
-
#
|
138
|
-
|
139
|
-
|
140
|
-
# Metrics, Dimensions, Sort
|
141
|
-
#
|
142
|
-
# name, alias
|
143
|
-
#
|
144
|
-
TYPE_3 = { metrics: :select,
|
145
|
-
dimensions: :with,
|
146
|
-
sort: nil }
|
127
|
+
# ids, cache, start_index, max_results
|
128
|
+
TYPE_INTEGER.each do |name, method_alias|
|
129
|
+
create_base_method(name)
|
147
130
|
|
148
|
-
TYPE_3.each do |key, value|
|
149
131
|
eval <<-METHOD
|
150
|
-
def #{
|
151
|
-
if args.size == 0
|
152
|
-
return @#{key}
|
153
|
-
end
|
154
|
-
|
155
|
-
self.#{key} = args
|
156
|
-
self
|
157
|
-
end
|
158
|
-
|
159
|
-
def #{key}=(value)
|
132
|
+
def #{name}=(value)
|
160
133
|
clear
|
161
|
-
@#{
|
134
|
+
@#{name} = value.to_i
|
162
135
|
end
|
136
|
+
METHOD
|
163
137
|
|
164
|
-
|
165
|
-
|
166
|
-
return @#{key}
|
167
|
-
end
|
138
|
+
create_base_alias(name, method_alias)
|
139
|
+
end
|
168
140
|
|
169
|
-
|
170
|
-
|
171
|
-
|
141
|
+
# start_date, end_date
|
142
|
+
TYPE_DATE.each do |name, method_alias|
|
143
|
+
create_base_method(name)
|
172
144
|
|
173
|
-
|
145
|
+
eval <<-METHOD
|
146
|
+
def #{name}=(value)
|
174
147
|
clear
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
if args.size == 0
|
180
|
-
return @#{key}
|
148
|
+
if value.is_a?(Integer)
|
149
|
+
@#{name} += value
|
150
|
+
else
|
151
|
+
@#{name} = to_date(value)
|
181
152
|
end
|
182
|
-
|
183
|
-
self.#{key}_sub = args
|
184
|
-
self
|
185
|
-
end
|
186
|
-
|
187
|
-
def #{key}_sub=(value)
|
188
|
-
clear
|
189
|
-
@#{key} -= build_param(value)
|
190
153
|
end
|
191
154
|
METHOD
|
192
155
|
|
193
|
-
|
194
|
-
|
195
|
-
alias :#{value} :#{key}
|
196
|
-
alias :#{value}= :#{key}=
|
156
|
+
create_base_alias(name, method_alias)
|
157
|
+
end
|
197
158
|
|
198
|
-
|
199
|
-
|
159
|
+
# metrics, dimensions, sort
|
160
|
+
TYPE_ARRAY.each do |name, method_alias|
|
161
|
+
create_array_method(name)
|
162
|
+
create_array_method(name, '+', '_add')
|
163
|
+
create_array_method(name, '-', '_sub')
|
200
164
|
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
end
|
165
|
+
create_base_alias(name, method_alias)
|
166
|
+
create_base_alias(name, method_alias, '_add')
|
167
|
+
create_base_alias(name, method_alias, '_sub')
|
205
168
|
end
|
206
169
|
|
207
|
-
#
|
208
|
-
|
209
|
-
#
|
210
|
-
# Filters, Segment
|
211
|
-
#
|
212
|
-
# name, alias
|
213
|
-
#
|
214
|
-
TYPE_4 = { filters: :where,
|
215
|
-
segment: nil }
|
216
|
-
|
217
|
-
TYPE_4.each do |key, value|
|
170
|
+
# filters, segment
|
171
|
+
TYPE_BLOCK.each do |name, method_alias|
|
218
172
|
eval <<-METHOD
|
219
|
-
def #{
|
173
|
+
def #{name}(value = nil, &block)
|
220
174
|
if !block_given? && value.nil?
|
221
|
-
return @#{
|
175
|
+
return @#{name}
|
222
176
|
end
|
223
177
|
|
224
178
|
if block_given?
|
225
|
-
@#{
|
179
|
+
@#{name} = #{name.to_s.capitalize}Dsl.new.instance_eval(&block).join
|
226
180
|
else
|
227
|
-
@#{
|
181
|
+
@#{name} = value
|
228
182
|
end
|
229
183
|
self
|
230
184
|
end
|
231
185
|
METHOD
|
232
186
|
|
233
|
-
unless
|
187
|
+
unless method_alias.nil?
|
234
188
|
eval <<-METHOD
|
235
|
-
alias :#{
|
189
|
+
alias :#{method_alias} :#{name}
|
236
190
|
METHOD
|
237
191
|
end
|
238
192
|
end
|
239
193
|
|
240
|
-
#
|
241
|
-
|
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
|
194
|
+
# error, user_cache
|
195
|
+
TYPE_BOOLEAN.each do |name, method_alias|
|
196
|
+
create_base_method(name)
|
253
197
|
|
198
|
+
eval <<-METHOD
|
199
|
+
def #{name}=(value)
|
254
200
|
if !value.is_a?(TrueClass) && !value.is_a?(FalseClass)
|
255
201
|
raise GoogleApi::TypeError, "Value must be true of false"
|
256
202
|
end
|
257
203
|
|
258
|
-
|
259
|
-
self
|
260
|
-
end
|
261
|
-
|
262
|
-
def #{key}=(value)
|
263
|
-
@#{key} = value
|
204
|
+
@#{name} = value
|
264
205
|
end
|
265
206
|
METHOD
|
266
207
|
end
|
267
208
|
|
209
|
+
# Methods for manipulations with data ---------------------------------------------------------
|
210
|
+
|
211
|
+
# Clear values
|
212
|
+
def clear
|
213
|
+
@header = nil
|
214
|
+
@parameters = nil
|
215
|
+
@data = nil
|
216
|
+
@all = nil
|
217
|
+
end
|
218
|
+
|
219
|
+
# Clear cache
|
268
220
|
def clear_cache
|
269
221
|
_cache.delete(parameters)
|
270
222
|
self
|
271
223
|
end
|
272
224
|
|
273
|
-
# Add row!, header!, all!, count!. First clear and run method
|
225
|
+
# Add row!, header!, all!, count!. First clear and run normal method.
|
274
226
|
[:rows, :header, :all, :count].each do |value|
|
275
227
|
eval <<-METHOD
|
276
|
-
def #{value}
|
277
|
-
clear
|
278
|
-
#{value}
|
279
|
-
end
|
228
|
+
def #{value}!; clear; #{value}; end
|
280
229
|
METHOD
|
281
230
|
end
|
282
231
|
|
@@ -317,10 +266,41 @@ module GoogleApi
|
|
317
266
|
|
318
267
|
private
|
319
268
|
|
269
|
+
# Store data, call clear for fetch again
|
320
270
|
def data
|
321
271
|
@data ||= get
|
322
272
|
end
|
323
273
|
|
274
|
+
# Convert string, DateTime and Time to date for google analytics YYYY-MM-DD
|
275
|
+
DATE_FORMAT = /\A[0-9]{4}-[0-9]{2}-[0-9]{2}\Z/
|
276
|
+
|
277
|
+
def to_date(date)
|
278
|
+
if date.is_a?(String)
|
279
|
+
unless date =~ DATE_FORMAT
|
280
|
+
raise GoogleApi::DateError, "Date: #{date} must match with #{DATE_FORMAT}."
|
281
|
+
end
|
282
|
+
|
283
|
+
date = Date.parse(date)
|
284
|
+
end
|
285
|
+
|
286
|
+
date.to_date
|
287
|
+
end
|
288
|
+
|
289
|
+
# Add prefix ga: to symbol in Array
|
290
|
+
def build_param(value)
|
291
|
+
type?(value, Array)
|
292
|
+
|
293
|
+
value.flatten.collect { |v| v.is_a?(Symbol) ? "ga:#{v}" : v }
|
294
|
+
end
|
295
|
+
|
296
|
+
# Check type
|
297
|
+
def type?(value, type)
|
298
|
+
unless value.is_a?(type)
|
299
|
+
raise GoogleApi::TypeError, "Value: #{value} must be #{type}."
|
300
|
+
end
|
301
|
+
end
|
302
|
+
|
303
|
+
# Build and store parameters
|
324
304
|
def parameters
|
325
305
|
return @parameters if @parameters
|
326
306
|
|
@@ -356,6 +336,7 @@ module GoogleApi
|
|
356
336
|
Session
|
357
337
|
end
|
358
338
|
|
339
|
+
# Get data from google analytics
|
359
340
|
def get
|
360
341
|
if @use_cache && _cache.exists?(parameters)
|
361
342
|
return _cache.read(parameters)
|