Module: LegatoGaUser

Included in:
SimpleGaReports
Defined in:
lib/simple_ga_reporting/legato_ga_user.rb

Constant Summary collapse

SCOPE =
'https://www.googleapis.com/auth/analytics.readonly'.freeze
TOKEN_CREDENTIAL_URI =
'https://accounts.google.com/o/oauth2/token'.freeze
AUDIENCE =
'https://accounts.google.com/o/oauth2/token'.freeze
AUTHORIZE_URL =
'https://accounts.google.com/o/oauth2/auth'.freeze
TOKEN_URL =
'https://accounts.google.com/o/oauth2/token'.freeze

Instance Method Summary collapse

Instance Method Details

#client_email(key_and_email) ⇒ Object



50
51
52
# File 'lib/simple_ga_reporting/legato_ga_user.rb', line 50

def client_email(key_and_email)
  YAML.load_file(key_and_email)['client_email']
end

#create_ga_user(key_and_email, private_key: nil, client_email: nil) ⇒ Object

private



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/simple_ga_reporting/legato_ga_user.rb', line 14

def create_ga_user(key_and_email, private_key: nil, client_email: nil)
  ga_user_private_key   = private_key.nil? ? private_key(key_and_email) : private_key
  ga_user_client_email  = client_email.nil? ? client_email(key_and_email) : client_email

  signing_key = OpenSSL::PKey::RSA.new(ga_user_private_key)
  auth_client = Signet::OAuth2::Client.new(
    token_credential_uri: TOKEN_CREDENTIAL_URI,
    audience: AUDIENCE,
    scope: SCOPE,
    issuer: ga_user_client_email,
    signing_key: signing_key,
    sub: ga_user_client_email,
  )
  access_token = auth_client.fetch_access_token!

  oauth_client = OAuth2::Client.new('', '', {
    authorize_url: AUTHORIZE_URL,
    token_url: TOKEN_URL,
  })

  token = OAuth2::AccessToken.new(
    oauth_client,
    access_token['access_token'],
    expires_in: access_token['expires_in'],
  )

  ga_user = Legato::User.new(token)
  ga_user.access_token.expired?

  ga_user
end

#private_key(key_and_email) ⇒ Object



46
47
48
# File 'lib/simple_ga_reporting/legato_ga_user.rb', line 46

def private_key(key_and_email)
  YAML.load_file(key_and_email)['private_key']
end