Class: AdsCommon::CredentialHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/ads_common/credential_handler.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ CredentialHandler

Initializes CredentialHandler.



28
29
30
31
32
# File 'lib/ads_common/credential_handler.rb', line 28

def initialize(config)
  @config = config
  @auth_handler = nil
  load_from_config(config)
end

Instance Method Details

#credentials(credentials_override = nil) ⇒ Object

Returns credentials set for the next call.



35
36
37
38
39
# File 'lib/ads_common/credential_handler.rb', line 35

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.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ads_common/credential_handler.rb', line 43

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, value)|
    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.



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/ads_common/credential_handler.rb', line 70

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 << 'Savon/%s' % Savon::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
  return '%s (%s)' % [agent_app, agent_data.join(', ')]
end

#set_auth_handler(auth_handler) ⇒ Object

Sets authorization handler to notify about credential changes.



83
84
85
# File 'lib/ads_common/credential_handler.rb', line 83

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.



64
65
66
67
# File 'lib/ads_common/credential_handler.rb', line 64

def set_credential(credential, value)
  @credentials[credential] = value
  @auth_handler.property_changed(credential, value) if @auth_handler
end