Class: GAShikomi::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/ga_shikomi/api.rb

Defined Under Namespace

Classes: InexpectantResponse

Constant Summary collapse

GOOGLE_API_KEYS =
%w(GOOGLE_API_CREDENTIAL GOOGLE_API_SECRETS)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(store) ⇒ Api

param

String store



17
18
19
20
21
22
23
# File 'lib/ga_shikomi/api.rb', line 17

def initialize(store)
  @client       = nil
  @analytics    = nil
  @last_request = nil

  init_and_auth_analytics(store)
end

Instance Attribute Details

#analyticsObject (readonly)

Returns the value of attribute analytics.



24
25
26
# File 'lib/ga_shikomi/api.rb', line 24

def analytics
  @analytics
end

#clientObject (readonly)

Returns the value of attribute client.



24
25
26
# File 'lib/ga_shikomi/api.rb', line 24

def client
  @client
end

#last_requestObject (readonly)

Returns the value of attribute last_request.



24
25
26
# File 'lib/ga_shikomi/api.rb', line 24

def last_request
  @last_request
end

Instance Method Details

#execute(method, params = {}) ⇒ Object

param

Object method

return

Hash



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/ga_shikomi/api.rb', line 85

def execute(method, params = {})
  @last_request = client.execute(
                    :api_method => method,
                    :parameters => params)

  result = JSON.parse(last_request.response.body)
  if result['error']
    raise InexpectantResponse, result['error']
  else
    result
  end
end

#flow(secrets) ⇒ Object

param

Google::APIClient::ClientSecrets

return

Google::APIClient::InstalledAppFlow



73
74
75
76
77
78
79
# File 'lib/ga_shikomi/api.rb', line 73

def flow(secrets)
  ::Google::APIClient::InstalledAppFlow.new(
      :client_id     => secrets.client_id,
      :client_secret => secrets.client_secret,
      :scope         => ['https://www.googleapis.com/auth/analytics',
                         'https://www.googleapis.com/auth/analytics.edit'])
end

#init_and_auth_analytics(store) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/ga_shikomi/api.rb', line 26

def init_and_auth_analytics(store)
  init_client

  if GOOGLE_API_KEYS.all? {|e| ENV.has_key? e}
    init_and_auth_with_env
  else
    init_and_auth_with_file_storage(store)
  end

  @analytics = client.discovered_api('analytics', 'v3')
end

#init_and_auth_with_envObject



38
39
40
41
42
43
44
# File 'lib/ga_shikomi/api.rb', line 38

def init_and_auth_with_env
  credential = ::Google::APIClient::Storage.new(EnvStore.new(ENV))
  credential.authorize
  secrets    = ::Google::APIClient::ClientSecrets.new(JSON.parse(ENV['GOOGLE_API_SECRETS']))

  client.authorization = credential.authorization
end

#init_and_auth_with_file_storage(store) ⇒ Object

param

String store



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/ga_shikomi/api.rb', line 49

def init_and_auth_with_file_storage(store)
  credential = ::Google::APIClient::FileStorage.new(store)
  secrets    = ::Google::APIClient::ClientSecrets.load(File.dirname(store))

  if credential.authorization.nil?
    flow = flow(secrets)

    client.authorization = flow.authorize
    credential.write_credentials(client.authorization)
  else
    client.authorization = credential.authorization
  end
end

#init_clientObject



63
64
65
66
67
# File 'lib/ga_shikomi/api.rb', line 63

def init_client
  @client = ::Google::APIClient.new(
                                :application_name    => :gacli,
                                :application_version => VERSION)
end