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'
    }
    


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
220
221
222
223
224
225
226
# File 'lib/adwords4r/credentials.rb', line 161

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)



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

def credentials
  @credentials
end

#environmentObject (readonly)

The environment being used (production, sandbox)



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

def environment
  @environment
end

#partial_failureObject

Whether we’re making requests with support for partial failures



141
142
143
# File 'lib/adwords4r/credentials.rb', line 141

def partial_failure
  @partial_failure
end

#return_money_in_microsObject

Whether we’re making requests with the returnMoneyInMicros option enabled



143
144
145
# File 'lib/adwords4r/credentials.rb', line 143

def return_money_in_micros
  @return_money_in_micros
end

#use_mccObject

Whether we’re making MCC-level requests



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

def use_mcc
  @use_mcc
end

#validate_onlyObject

Whether we’re making validate-only requests



139
140
141
# File 'lib/adwords4r/credentials.rb', line 139

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



280
281
282
283
# File 'lib/adwords4r/credentials.rb', line 280

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



248
249
250
251
252
253
254
# File 'lib/adwords4r/credentials.rb', line 248

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



234
235
236
237
238
239
240
# File 'lib/adwords4r/credentials.rb', line 234

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



335
336
337
338
339
340
341
# File 'lib/adwords4r/credentials.rb', line 335

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



293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/adwords4r/credentials.rb', line 293

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)



265
266
267
268
269
270
271
272
# File 'lib/adwords4r/credentials.rb', line 265

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.



318
319
320
321
322
323
324
325
326
327
# File 'lib/adwords4r/credentials.rb', line 318

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