Class: GoogleApi::Ga::Session

Inherits:
Object
  • Object
show all
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

Class Method Details

._configObject

Config ——————————————————————————–



17
18
19
# File 'lib/google_api/ga/session.rb', line 17

def self._config
  GoogleApi.config
end

.apiObject



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

.clientObject



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.authorization.refresh_token && @@client.authorization.expired?
    @@client.authorization.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.(code = nil)
  @@client = Google::APIClient.new
  @@client.authorization.client_id     = client_id
  @@client.authorization.client_secret = client_secret
  @@client.authorization.scope         = SCOPE
  @@client.authorization.redirect_uri  = redirect_uri

  @@api = @@client.discovered_api(NAME_API, VERSION_API)

  if code
    @@client.authorization.code = code

    begin
      @@client.authorization.fetch_access_token!
    rescue
      return false
    end

    return true
  else
    @@client.authorization.authorization_uri.to_s
  end
end

.login_by_certObject




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.
  @@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.authorization = asserter.authorize()
    @@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.
  i = 0
  while !
    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.(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()

  # 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 (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