Class: AdsCommon::CredentialHandler
- Inherits:
-
Object
- Object
- AdsCommon::CredentialHandler
- Defined in:
- lib/ads_common/credential_handler.rb
Instance Method Summary collapse
-
#credentials(credentials_override = nil) ⇒ Object
Returns credentials set for the next call.
-
#credentials=(new_credentials) ⇒ Object
Set the credentials hash to a new one.
-
#generate_user_agent(extra_ids = [], agent_app = nil) ⇒ Object
Generates string for UserAgent to put into headers.
-
#identifier ⇒ Object
Get the unique identifier for the current account/network context.
-
#include_in_user_agent(name, suffix = nil) ⇒ Object
Adds a custom string to the user agent, one time, the next time a user agent is generated.
-
#initialize(config) ⇒ CredentialHandler
constructor
Initializes CredentialHandler.
-
#set_auth_handler(auth_handler) ⇒ Object
Sets authorization handler to notify about credential changes.
-
#set_credential(credential, value) ⇒ Object
Set a single credential to a new value.
Constructor Details
#initialize(config) ⇒ CredentialHandler
Initializes CredentialHandler.
27 28 29 30 31 32 33 |
# 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 |
Instance Method Details
#credentials(credentials_override = nil) ⇒ Object
Returns credentials set for the next call.
36 37 38 39 40 |
# 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) ⇒ Object
Set the credentials hash to a new one. Calculate difference, and call the AuthHandler callback appropriately.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# 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) ⇒ Object
Generates string for UserAgent to put into headers.
83 84 85 86 87 88 89 90 91 92 93 94 |
# 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 ⇒ Object
Get the unique identifier for the current account/network context. This is meant to be overriden by each client library.
103 104 105 |
# File 'lib/ads_common/credential_handler.rb', line 103 def identifier() return nil end |
#include_in_user_agent(name, suffix = nil) ⇒ Object
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.
73 74 75 76 77 78 79 80 |
# 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) ⇒ Object
Sets authorization handler to notify about credential changes.
97 98 99 |
# File 'lib/ads_common/credential_handler.rb', line 97 def set_auth_handler(auth_handler) @auth_handler = auth_handler end |
#set_credential(credential, value) ⇒ Object
Set a single credential to a new value. Call the AuthHandler callback appropriately.
65 66 67 68 |
# 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 |