Class: AdWords::AdWordsCredentials

Inherits:
Object
  • Object
show all
Defined in:
lib/adwords4r/credentials.rb

Overview

Generic credentials class, used for any API version.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(credentials = nil) ⇒ AdWordsCredentials

Constructor for AdWordsCredentials.

Args:

  • credentials: Hash of credentials (credential key to value). E.g.:

    {
     'developerToken' => '[email protected]++USD',
     'useragent' => 'Sample User Agent',
     'password' => 'password',
     'email' => '[email protected]',
     'clientEmail' => '[email protected]',
     'applicationToken' => 'IGNORED',
     'environment' => 'SANDBOX'
    }
    


154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/adwords4r/credentials.rb', line 154

def initialize(credentials=nil)
  @credentials = {}
  @environment = nil
  @auth_token = nil
  @handlers = []
  @use_mcc = false
  @validate_only = false
  @partial_failures = false
  credentials = get_defaults() if credentials.nil?
  credentials.each do |key, value|
    # 'environment' shouldn't go in the credentials array, and we'll ignore
    # 'alternateUrl' to avoid errors on upgraders' apps.
    if (key =~ /^alternateUrl/) && (credentials["environment"].nil?)
      raise AdWords::Error::Error,
          "'alternateUrl' is no longer supported. Please consult the " +
          "Readme on how to use 'environment' instead."
    elsif !(key =~ /^environment/)
      @credentials[key] = value
    end
  end

  # The user agent header differs in v13 (useragent) and v2009 (userAgent).
  # Properly populate both values, and use the name of the program ($0) if
  # nothing is given.
  user_agent = 'adwords4r: %s' % (@credentials['useragent'] ||
    @credentials['userAgent'] || $0)
  @credentials['useragent'] = @credentials['userAgent'] = user_agent

  # The properties file may include the clientEmail in a clientId property.
  # clientId might be a clientCustomerId, though, so check to make sure it
  # is an email address before assigning it to clientEmail.
  # clientCustomerIds don't seem to be supported elsewhere in this client
  # library, so ignore them.
  if @credentials['clientEmail'].nil? and @credentials['clientId'] and
      @credentials['clientId'].include?('@')
    @credentials['clientEmail'] = @credentials['clientId']
  end

  # Normalize 'token' to 'developerToken'
  if @credentials['developerToken'].nil? and @credentials['token']
    @credentials['developerToken'] = @credentials['token']
    @credentials.delete('token')
  end

  # Set environment
  if credentials['environment'].nil?
    # Get default environment
    @environment = AdWords::Service.get_default_environment
  elsif !(Service.get_environments.include?(credentials['environment']))
    raise AdWords::Error::Error,
        "Unknown environment: #{credentials['environment']}"
  else
    @environment = credentials['environment']
  end

  # Fix potential problems with changing clientEmail, by forcing it to be
  # created
  @credentials['clientEmail'] = '' if @credentials['clientEmail'].nil?

  # Check for environment mismatches.
  validate_headers_for_server

  @credentials.each do |key, value|
    @handlers << Pre2009HeaderHandler.new(key, self)
  end
end

Instance Attribute Details

#credentialsObject (readonly)

Hash of credentials (credential key to value)



128
129
130
# File 'lib/adwords4r/credentials.rb', line 128

def credentials
  @credentials
end

#environmentObject (readonly)

The environment being used (production, sandbox)



130
131
132
# File 'lib/adwords4r/credentials.rb', line 130

def environment
  @environment
end

#partial_failureObject

Whether we’re making requests with support for partial failures



136
137
138
# File 'lib/adwords4r/credentials.rb', line 136

def partial_failure
  @partial_failure
end

#use_mccObject

Whether we’re making MCC-level requests



132
133
134
# File 'lib/adwords4r/credentials.rb', line 132

def use_mcc
  @use_mcc
end

#validate_onlyObject

Whether we’re making validate-only requests



134
135
136
# File 'lib/adwords4r/credentials.rb', line 134

def validate_only
  @validate_only
end

Instance Method Details

#auth_tokenObject

Returns the authentication token used with >= v2009 requests. Generates it if there’s no valid token already generated.

Returns: The auth token (as a string).



273
274
275
276
# File 'lib/adwords4r/credentials.rb', line 273

def auth_token
  generate_auth_token if @auth_token.nil?
  return @auth_token
end

#client_customer_idObject

Returns the client customer ID currently being used (dependent on whether MCC-level requests are enabled or disabled)

Returns: Client customer ID if use_mcc is false, empty string otherwise



241
242
243
244
245
246
247
# File 'lib/adwords4r/credentials.rb', line 241

def client_customer_id
  if @use_mcc
    return ''
  else
    return @credentials['clientCustomerId']
  end
end

#client_emailObject

Returns the client email currently being used (dependent on whether MCC-level requests are enabled or disabled)

Returns: Client email if use_mcc is false, empty string otherwise



227
228
229
230
231
232
233
# File 'lib/adwords4r/credentials.rb', line 227

def client_email
  if @use_mcc
    return ''
  else
    return @credentials['clientEmail']
  end
end

#dupObject

Overloads the ‘dup’ method for AdWordsCredentials to correctly duplicate objects of this class.

Returns: Duplicated AdWordsCredentials object



328
329
330
331
332
333
334
# File 'lib/adwords4r/credentials.rb', line 328

def dup
  creds = @credentials.dup
  # Remove the prepended 'adwords4r: ' string before creating the duplicate
  creds['userAgent']['adwords4r: '] = ''
  creds['environment'] = @environment unless @environment.nil?
  return AdWordsCredentials.new(creds)
end

#generate_auth_tokenObject

Generates a new authentication token used with >= v2009 requests. The generated token is stored and can later be accessed with auth_token. It should only be necessary for user code to invoke this if the first token expires.

Returns: The auth token (as a string).



286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/adwords4r/credentials.rb', line 286

def generate_auth_token
  email = @credentials['email']
  password = @credentials['password']

  if email.nil?
    raise AdWords::Error::AuthError,
        'Email address not included in credentials.'
  end

  if password.nil?
    raise AdWords::Error::AuthError, 'Password not included in credentials.'
  end

  hostname, port, use_ssl = AdWords::Service.get_auth_server(@environment)
  @auth_token = AdWords::AuthToken::get_token(email, password, hostname,
      port, use_ssl)
  return @auth_token
end

#get_handlers(version, driver) ⇒ Object

Return a list of handlers to be inserted into the driver’s handler list.

Args:

  • version: API version being used. Must be an integer.

  • driver: the driver for the service being handled

Returns: The list of handlers (subclasses of SOAP::Header::SimpleHandler)



258
259
260
261
262
263
264
265
# File 'lib/adwords4r/credentials.rb', line 258

def get_handlers(version, driver)
  if version.is_a? Integer and version <= 13 then
    return @handlers
  else
    namespace = AdWords::Service.get_namespace_v2009(driver)
    return [V2009HeaderHandler.new(self, namespace, version)]
  end
end

#set_header(header, value) ⇒ Object

Change one of the authentication headers.

Args:

  • header: the name for the header being changed.

  • value: the new value for the header.



311
312
313
314
315
316
317
318
319
320
# File 'lib/adwords4r/credentials.rb', line 311

def set_header(header, value)
  # Invalidate previous auth token if necessary
  @auth_token = nil if header == 'email' or header == 'password'

  @credentials.each_key do |key|
    if key == header then
      @credentials[key] = value
    end
  end
end