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.
@@ -2,13 +2,6 @@ module GoogleApi
2
2
  module Ga
3
3
 
4
4
  CONFIGURATION = {
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
5
  cache: GoogleApi::Cache.new
13
6
  }
14
7
 
@@ -23,17 +16,12 @@ module GoogleApi
23
16
  autoload :Goal, 'google_api/ga/management/goal'
24
17
  autoload :Segment, 'google_api/ga/management/segment'
25
18
 
26
- # Helper
27
- autoload :Helper, 'google_api/ga/helper'
28
-
29
19
  # Data
30
20
  autoload :Data, 'google_api/ga/data'
31
21
  autoload :DataDsl, 'google_api/ga/data/data_dsl'
32
22
  autoload :FiltersDsl, 'google_api/ga/data/filters_dsl'
33
23
  autoload :SegmentDsl, 'google_api/ga/data/segment_dsl'
34
24
 
35
- extend GoogleApi::Ga::Helper
36
-
37
25
  @@id = 0
38
26
 
39
27
  def self.id(id=nil)
@@ -1,155 +1,15 @@
1
- require "google_api/core_ext/string"
2
-
3
1
  module GoogleApi
4
2
  module Ga
5
-
6
- class Session
3
+ class Session < GoogleApi::SessionMethods
7
4
 
8
5
  SCOPE = "https://www.googleapis.com/auth/analytics.readonly"
9
6
  NAME_API = "analytics"
10
7
  VERSION_API = "v3"
11
8
 
12
- @@api = nil
13
- @@client = nil
14
-
15
- # Config --------------------------------------------------------------------------------
16
-
17
- def self._config
18
- GoogleApi.config
19
- end
20
-
21
- [:client_id, :client_secret, :redirect_uri, :client_cert_file, :client_developer_email, :key_secret].each do |key|
22
- eval <<-METHOD
23
- def self.#{key}
24
- to_return = _config.ga.#{key} ? _config.ga.#{key} : _config.#{key}
25
-
26
- if to_return.nil?
27
- raise GoogleApi::CanBeNilError, "#{key} can be nil."
28
- end
29
-
30
- to_return
31
- end
32
- METHOD
33
- end
34
-
35
- # ---------------------------------------------------------------------------------------
36
-
37
- def self.login_by_cert
38
- @@client = Google::APIClient.new
39
-
40
- key = Google::APIClient::PKCS12.load_key(client_cert_file, key_secret)
41
- asserter = Google::APIClient::JWTAsserter.new(client_developer_email, SCOPE, key)
42
-
43
- begin
44
- @@client.authorization = asserter.authorize()
45
- @@api = @@client.discovered_api(NAME_API, VERSION_API)
46
- rescue
47
- return false
48
- end
49
-
50
- return true
51
- end
52
-
53
- def self.login_by_cert!
54
- i = 0
55
- while !login_by_cert
56
- print "\r[#{Time.now.strftime("%H:%M:%S")}] ##{i += 1} ... \r".bold
57
- sleep(1)
58
- end
59
-
60
- return true
61
- end
62
-
63
- def self.login(code = nil)
64
- @@client = Google::APIClient.new
65
- @@client.authorization.client_id = client_id
66
- @@client.authorization.client_secret = client_secret
67
- @@client.authorization.scope = SCOPE
68
- @@client.authorization.redirect_uri = redirect_uri
69
-
70
- @@api = @@client.discovered_api(NAME_API, VERSION_API)
71
-
72
- if code
73
- @@client.authorization.code = code
74
-
75
- begin
76
- @@client.authorization.fetch_access_token!
77
- rescue
78
- return false
79
- end
80
-
81
- return true
82
- else
83
- @@client.authorization.authorization_uri.to_s
84
- end
85
- end
86
-
87
- def self.login_by_line(server = 'http://localhost/oauth2callback', port = 0)
88
- require "launchy" # open browser
89
- require "socket" # make tcp server
90
- require "uri" # parse uri
9
+ CONFIG_NAME = "ga"
91
10
 
92
- uri = URI(server)
93
-
94
- # Start webserver.
95
- webserver = TCPServer.new(uri.host, port)
96
-
97
- # By default port is 0. It means that TCPServer will get first free port.
98
- # Port is required for redirect_uri.
99
- uri.port = webserver.addr[1]
100
-
101
- # Add redirect_uri for google oauth 2 callback.
102
- GoogleApi.config.ga.redirect_uri = uri.to_s
103
-
104
- # Open browser.
105
- Launchy.open(login)
106
-
107
- # Wait for new session.
108
- session = webserver.accept
109
-
110
- # Parse header for query.
111
- request = session.gets.gsub(/GET\ \//, '').gsub(/\ HTTP.*/, '')
112
- request = Hash[URI.decode_www_form(URI(request).query)]
113
-
114
- code = request['code']
115
-
116
- # Close session and webserver.
117
- session.close
118
-
119
- if login(code)
120
- session.write("You have been successfully logged. Now you can close the browser.")
121
-
122
- return true
123
- end
124
-
125
- session.write("You have not been logged. Please try again.")
126
-
127
- return false
128
- end
129
-
130
- def self.check_session!
131
- if @@api.nil? || @@client.nil?
132
- raise GoogleApi::SessionError, "You are not log in."
133
- end
134
- end
135
-
136
- def self.api
137
- check_session!
138
-
139
- @@api
140
- end
141
-
142
- def self.client
143
- check_session!
144
-
145
- if @@client.authorization.refresh_token && @@client.authorization.expired?
146
- @@client.authorization.fetch_access_token!
147
- end
148
-
149
- @@client
150
- end
11
+ @@session = GoogleApi::Session.new(CONFIG_NAME, SCOPE, NAME_API, VERSION_API)
151
12
 
152
13
  end
153
-
154
14
  end
155
15
  end
@@ -0,0 +1,202 @@
1
+ require "google_api/core_ext/string"
2
+
3
+ module GoogleApi
4
+ class Session
5
+
6
+ attr_reader :scope
7
+
8
+ def initialize(config_name, scope, name_api, version_api)
9
+ @config_name = config_name
10
+ @scope = scope
11
+ @name_api = name_api
12
+ @version_api = version_api
13
+
14
+ @client = nil
15
+ @api = nil
16
+ end
17
+
18
+ # Login using cert file (Service account)
19
+ #
20
+ # Required: client_cert_file, client_developer_email, key_secret
21
+ #
22
+ # Success: return true
23
+ # Failure: return false
24
+ #
25
+ def login_by_cert
26
+ @client = Google::APIClient.new
27
+
28
+ key = Google::APIClient::PKCS12.load_key(c('client_cert_file'), c('key_secret'))
29
+ asserter = Google::APIClient::JWTAsserter.new(c('client_developer_email'), @scope, key)
30
+
31
+ begin
32
+ @client.authorization = asserter.authorize()
33
+ @api = @client.discovered_api(@name_api, @version_api)
34
+ rescue
35
+ return false
36
+ end
37
+
38
+ return true
39
+ end
40
+
41
+ # In loop call login_by_cert
42
+ #
43
+ # return true after successful login
44
+ # loop can be stopped with ctr+c - return false
45
+ #
46
+ def login_by_cert!
47
+ trap("INT"){ return false }
48
+
49
+ i = 0
50
+ until login_by_cert
51
+ progress_log(i+=1)
52
+ sleep(1)
53
+ end
54
+
55
+ return true
56
+ end
57
+
58
+ # Classic oauth 2 login
59
+ #
60
+ # login() -> return autorization url
61
+ # login(code) -> try login, return true false
62
+ #
63
+ def login(code = nil)
64
+ @client = Google::APIClient.new
65
+ @client.authorization.client_id = c('client_id')
66
+ @client.authorization.client_secret = c('client_secret')
67
+ @client.authorization.scope = @scope
68
+ @client.authorization.redirect_uri = c('redirect_uri')
69
+
70
+ @api = @client.discovered_api(@name_api, @version_api)
71
+
72
+ unless code
73
+ return @client.authorization.authorization_uri.to_s
74
+ end
75
+
76
+ begin
77
+ @client.authorization.code = code
78
+ @client.authorization.fetch_access_token!
79
+ rescue
80
+ return false
81
+ end
82
+
83
+ return true
84
+ end
85
+
86
+ # Automaticaly open autorization url a waiting for callback.
87
+ # Launchy gem is required
88
+ #
89
+ # Parameters:
90
+ # server:: server will be on this addres, its alson address for oatuh 2 callback
91
+ # port:: listening port for server
92
+ # port=0:: server will be on first free port
93
+ #
94
+ # Steps:
95
+ # 1) create server
96
+ # 2) launch browser and redirect to google api
97
+ # 3) confirm and google api redirect to localhost
98
+ # 4) server get code and start session
99
+ # 5) close server - you are login
100
+ #
101
+ def login_by_line(server = 'http://localhost/oauth2callback', port = 0)
102
+ begin
103
+ require "launchy" # open browser
104
+ rescue
105
+ raise GoogleApi::RequireError, "You don't have launchy gem. Firt install it: gem install launchy."
106
+ end
107
+
108
+ require "socket" # make tcp server
109
+ require "uri" # parse uri
110
+
111
+ uri = URI(server)
112
+
113
+ # Start webserver.
114
+ webserver = TCPServer.new(uri.host, port)
115
+
116
+ # By default port is 0. It means that TCPServer will get first free port.
117
+ # Port is required for redirect_uri.
118
+ uri.port = webserver.addr[1]
119
+
120
+ # Add redirect_uri for google oauth 2 callback.
121
+ _config.send(@config_name).redirect_uri = uri.to_s
122
+
123
+ # Open browser.
124
+ Launchy.open(login)
125
+
126
+ # Wait for new session.
127
+ session = webserver.accept
128
+
129
+ # Parse header for query.
130
+ request = session.gets.gsub(/GET\ \//, '').gsub(/\ HTTP.*/, '')
131
+ request = Hash[URI.decode_www_form(URI(request).query)]
132
+
133
+ # Failure login
134
+ to_return = false
135
+ message = "You have not been logged. Please try again."
136
+
137
+ if login(request['code'])
138
+ message = "You have been successfully logged. Now you can close the browser."
139
+
140
+ to_return = true
141
+ end
142
+
143
+ session.write(message)
144
+
145
+ # Close session and webserver.
146
+ session.close
147
+
148
+ return to_return
149
+ end
150
+
151
+ # Check session
152
+ def check_session
153
+ if @api.nil? || @client.nil?
154
+ return false
155
+ end
156
+
157
+ return true
158
+ end
159
+
160
+ # Check session with error
161
+ def check_session!
162
+ if @api.nil? || @client.nil?
163
+ raise GoogleApi::SessionError, "You are not log in."
164
+ end
165
+ end
166
+
167
+ def client
168
+ check_session!
169
+
170
+ if @client.authorization.refresh_token && @client.authorization.expired?
171
+ @client.authorization.fetch_access_token!
172
+ end
173
+
174
+ @client
175
+ end
176
+
177
+ def api
178
+ check_session!
179
+
180
+ @api
181
+ end
182
+
183
+ private
184
+
185
+ def progress_log(i)
186
+ print("\r[#{Time.now.strftime("%H:%M:%S")}] ##{i} ... \r".bold)
187
+ end
188
+
189
+ def _config
190
+ GoogleApi.config
191
+ end
192
+
193
+ def c(key)
194
+ if _config.send(@config_name).send(key)
195
+ _config.send(@config_name).send(key)
196
+ else
197
+ _config.send(key)
198
+ end
199
+ end
200
+
201
+ end
202
+ end
@@ -0,0 +1,41 @@
1
+ module GoogleApi
2
+ class SessionMethods
3
+
4
+ def self._session
5
+ self.class_variable_get(:@@session)
6
+ end
7
+
8
+ def self.login_by_cert
9
+ _session.login_by_cert
10
+ end
11
+
12
+ def self.login_by_cert!
13
+ _session.login_by_cert!
14
+ end
15
+
16
+ def self.login(code = nil)
17
+ _session.login(code)
18
+ end
19
+
20
+ def self.login_by_line(server = 'http://localhost/oauth2callback', port = 0)
21
+ _session.login_by_line(server, port)
22
+ end
23
+
24
+ def self.check_session
25
+ _session.check_session
26
+ end
27
+
28
+ def self.check_session!
29
+ _session.check_session!
30
+ end
31
+
32
+ def self.api
33
+ _session.api
34
+ end
35
+
36
+ def self.client
37
+ _session.client
38
+ end
39
+
40
+ end
41
+ end
@@ -0,0 +1,16 @@
1
+ module GoogleApi
2
+ module Shorten
3
+ class Session < GoogleApi::SessionMethods
4
+
5
+ SCOPE = "https://www.googleapis.com/auth/urlshortener"
6
+ NAME_API = "urlshortener"
7
+ VERSION_API = "v1"
8
+
9
+ CONFIG_NAME = "shorten"
10
+
11
+ @@session = GoogleApi::Session.new(CONFIG_NAME, SCOPE, NAME_API, VERSION_API)
12
+
13
+ end
14
+ end
15
+ end
16
+