class AdsCommon::CredentialHandler

Public Class Methods

new(config) click to toggle source

Initializes CredentialHandler.

# File lib/ads_common/credential_handler.rb, line 27
def initialize(config)
  @config = config
  @auth_handler = nil
  @extra_user_agents = {}
  @extra_user_agents_lock = Mutex.new
  load_from_config(config)
end

Public Instance Methods

credentials(credentials_override = nil) click to toggle source

Returns credentials set for the next call.

# File lib/ads_common/credential_handler.rb, line 36
def credentials(credentials_override = nil)
  credentials = @credentials.dup()
  credentials.merge!(credentials_override) unless credentials_override.nil?
  return credentials
end
credentials=(new_credentials) click to toggle source

Set the credentials hash to a new one. Calculate difference, and call the AuthHandler callback appropriately.

# File lib/ads_common/credential_handler.rb, line 44
def credentials=(new_credentials)
  # Find new and changed properties.
  diff = new_credentials.inject([]) do |result, (key, value)|
    result << [key, value] if value != @credentials[key]
    result
  end

  # Find removed properties.
  diff = @credentials.inject(diff) do |result, (key, _)|
    result << [key, nil] unless new_credentials.include?(key)
    result
  end

  # Set each property.
  diff.each { |entry| set_credential(entry[0], entry[1]) }

  return nil
end
generate_user_agent(extra_ids = [], agent_app = nil) click to toggle source

Generates string for UserAgent to put into headers.

# File lib/ads_common/credential_handler.rb, line 83
def generate_user_agent(extra_ids = [], agent_app = nil)
  agent_app ||= File.basename($0)
  agent_data = extra_ids
  agent_data << 'Common-Ruby/%s' % AdsCommon::ApiConfig::CLIENT_LIB_VERSION
  agent_data << 'GoogleAdsSavon/%s' % GoogleAdsSavon::VERSION
  ruby_engine = defined?(RUBY_ENGINE) ? RUBY_ENGINE : 'ruby'
  agent_data << [ruby_engine, RUBY_VERSION].join('/')
  agent_data << 'HTTPI/%s' % HTTPI::VERSION
  agent_data << HTTPI::Adapter.use.to_s
  agent_data += get_extra_user_agents()
  return '%s (%s)' % [agent_app, agent_data.join(', ')]
end
identifier() click to toggle source

Get the unique identifier for the current account/network context. This is meant to be overriden by each client library.

# File lib/ads_common/credential_handler.rb, line 103
def identifier()
  return nil
end
include_in_user_agent(name, suffix = nil) click to toggle source

Adds a custom string to the user agent, one time, the next time a user agent is generated. This will be rendered in the format “name/suffix”, or just “name” if the suffix is nil or omitted.

# File lib/ads_common/credential_handler.rb, line 73
def include_in_user_agent(name, suffix = nil)
  return if name.nil?
  unless @config.read('library.include_utilities_in_user_agent') == false
    @extra_user_agents_lock.synchronize do
      @extra_user_agents[name] = suffix
    end
  end
end
set_auth_handler(auth_handler) click to toggle source

Sets authorization handler to notify about credential changes.

# File lib/ads_common/credential_handler.rb, line 97
def set_auth_handler(auth_handler)
  @auth_handler = auth_handler
end
set_credential(credential, value) click to toggle source

Set a single credential to a new value. Call the AuthHandler callback appropriately.

# File lib/ads_common/credential_handler.rb, line 65
def set_credential(credential, value)
  @credentials[credential] = value
  @auth_handler.property_changed(credential, value) if @auth_handler
end

Private Instance Methods

get_extra_user_agents() click to toggle source

Generates an array of extra user agents to include in the user agent string.

# File lib/ads_common/credential_handler.rb, line 116
def get_extra_user_agents()
  @extra_user_agents_lock.synchronize do
    user_agents = @extra_user_agents.collect do |k, v|
      v.nil? ? k : '%s/%s' % [k, v]
    end
    @extra_user_agents.clear
    return user_agents
  end
end
load_from_config(config) click to toggle source

Loads the credentials from the config data.

# File lib/ads_common/credential_handler.rb, line 110
def load_from_config(config)
  @credentials = config.read('authentication')
end