Class: GoogleApi::Ga::Session
- Inherits:
-
Object
- Object
- GoogleApi::Ga::Session
- Defined in:
- lib/google_api/ga/session.rb
Constant Summary collapse
- SCOPE =
"https://www.googleapis.com/auth/analytics.readonly"- NAME_API =
"analytics"- VERSION_API =
"v3"- @@api =
nil- @@client =
nil
Class Method Summary collapse
-
._config ⇒ Object
Config ——————————————————————————–.
- .api ⇒ Object
- .check_session! ⇒ Object
- .client ⇒ Object
- .login(code = nil) ⇒ Object
-
.login_by_cert ⇒ Object
—————————————————————————————.
- .login_by_cert! ⇒ Object
- .login_by_line(server = 'http://localhost/oauth2callback', port = 0) ⇒ Object
Class Method Details
._config ⇒ Object
Config ——————————————————————————–
17 18 19 |
# File 'lib/google_api/ga/session.rb', line 17 def self._config GoogleApi.config end |
.api ⇒ Object
136 137 138 139 140 |
# File 'lib/google_api/ga/session.rb', line 136 def self.api check_session! @@api end |
.check_session! ⇒ Object
130 131 132 133 134 |
# File 'lib/google_api/ga/session.rb', line 130 def self.check_session! if @@api.nil? || @@client.nil? raise GoogleApi::SessionError, "You are not log in." end end |
.client ⇒ Object
142 143 144 145 146 147 148 149 150 |
# File 'lib/google_api/ga/session.rb', line 142 def self.client check_session! if @@client..refresh_token && @@client..expired? @@client..fetch_access_token! end @@client end |
.login(code = nil) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/google_api/ga/session.rb', line 63 def self.login(code = nil) @@client = Google::APIClient.new @@client..client_id = client_id @@client..client_secret = client_secret @@client..scope = SCOPE @@client..redirect_uri = redirect_uri @@api = @@client.discovered_api(NAME_API, VERSION_API) if code @@client..code = code begin @@client..fetch_access_token! rescue return false end return true else @@client...to_s end end |
.login_by_cert ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/google_api/ga/session.rb', line 37 def self.login_by_cert @@client = Google::APIClient.new key = Google::APIClient::PKCS12.load_key(client_cert_file, key_secret) asserter = Google::APIClient::JWTAsserter.new(client_developer_email, SCOPE, key) begin @@client. = asserter.() @@api = @@client.discovered_api(NAME_API, VERSION_API) rescue return false end return true end |
.login_by_cert! ⇒ Object
53 54 55 56 57 58 59 60 61 |
# File 'lib/google_api/ga/session.rb', line 53 def self.login_by_cert! i = 0 while !login_by_cert print "\r[#{Time.now.strftime("%H:%M:%S")}] ##{i += 1} ... \r".bold sleep(1) end return true end |
.login_by_line(server = 'http://localhost/oauth2callback', port = 0) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/google_api/ga/session.rb', line 87 def self.login_by_line(server = 'http://localhost/oauth2callback', port = 0) require "launchy" # open browser require "socket" # make tcp server require "uri" # parse uri uri = URI(server) # Start webserver. webserver = TCPServer.new(uri.host, port) # By default port is 0. It means that TCPServer will get first free port. # Port is required for redirect_uri. uri.port = webserver.addr[1] # Add redirect_uri for google oauth 2 callback. GoogleApi.config.ga.redirect_uri = uri.to_s # Open browser. Launchy.open(login) # Wait for new session. session = webserver.accept # Parse header for query. request = session.gets.gsub(/GET\ \//, '').gsub(/\ HTTP.*/, '') request = Hash[URI.decode_www_form(URI(request).query)] code = request['code'] # Close session and webserver. session.close if login(code) session.write("You have been successfully logged. Now you can close the browser.") return true end session.write("You have not been logged. Please try again.") return false end |