class GAShikomi::Api
Constants
- GOOGLE_API_KEYS
Attributes
analytics[R]
client[R]
last_request[R]
Public Class Methods
new(store)
click to toggle source
- param
-
String store
# File lib/ga_shikomi/api.rb, line 16 def initialize(store) @client = nil @analytics = nil @last_request = nil init_and_auth_analytics(store) end
Public Instance Methods
execute(method, params = {})
click to toggle source
- param
-
Object method
- return
-
Hash
# File lib/ga_shikomi/api.rb, line 84 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)
click to toggle source
- param
-
Google::APIClient::ClientSecrets
- return
-
Google::APIClient::InstalledAppFlow
# File lib/ga_shikomi/api.rb, line 72 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)
click to toggle source
# File lib/ga_shikomi/api.rb, line 25 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_env()
click to toggle source
# File lib/ga_shikomi/api.rb, line 37 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)
click to toggle source
- param
-
String store
# File lib/ga_shikomi/api.rb, line 48 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_client()
click to toggle source
# File lib/ga_shikomi/api.rb, line 62 def init_client @client = ::Google::APIClient.new( :application_name => :gacli, :application_version => VERSION) end