class GTM::Client

Attributes

auth_file[RW]
client[RW]
containers[RW]
gtm[RW]
scope[RW]

Public Class Methods

new() click to toggle source
# File lib/gtm/client.rb, line 10
def initialize
  @client = Google::APIClient.new(
      :application_name => 'gtmclient',
      :application_version => '1.0.0'
  )
  @gtm = @client.discovered_api('tagmanager')
  @scope ||= {path: ''}
end

Public Instance Methods

run() { |accounts| ... } click to toggle source
# File lib/gtm/client.rb, line 28
def run
  set_auth_file
  tries = 0
  begin
    result = @client.execute(
        api_method: @gtm.accounts.list,
        parameters: {
        }
    )
    tries += 1
    raise GTM::AuthException if result.data['error'] && result.data.error['code']
    @accounts = result.data.accounts
    yield @accounts
    puts 'DONE'
  rescue AuthException
    client_secrets = Google::APIClient::ClientSecrets.load
    flow = Google::APIClient::InstalledAppFlow.new(
        client_id: client_secrets.client_id,
        client_secret: client_secrets.client_secret,
        scope: @@scopes
    )

    auth = flow.authorize
    data = Marshal.dump(auth)
    @client.authorization = auth
    File.write @auth_file, data
    retry if tries < 2
  end
end
set_auth_file() click to toggle source
# File lib/gtm/client.rb, line 19
def set_auth_file
  auth_path = "#{@scope[:path]}.dump"
  FileUtils::mkdir_p auth_path
  @auth_file = "#{auth_path}/auth"
  data = File.read(@auth_file) rescue nil
  auth = Marshal.load(data) rescue nil
  @client.authorization = auth
end