Class: AdCenterService

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

Overview

Parent class for individual adCenter service classes

Defined Under Namespace

Classes: HeaderHandler

Constant Summary collapse

DEFAULT_REQUIRED_CREDENTIALS =

list of required header fields for most services

%w[ ApplicationToken CustomerAccountId CustomerId UserName Password DeveloperToken ]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(endpoint, credentials) ⇒ AdCenterService

Returns a new instance of AdCenterService.

[View source]

18
19
20
21
22
23
24
25
26
27
# File 'lib/adcenter_service.rb', line 18

def initialize(endpoint, credentials)
  @service_namespace = 'https://adcenter.microsoft.com/v8'
  @required_credentials = DEFAULT_REQUIRED_CREDENTIALS
  @endpoint = endpoint
  initialize_service(@endpoint)
  initialize_authentication_headers(credentials)
  # set options on service after it is built
  @service.wiredump_dev = STDERR if $DEBUG
  @service.options["protocol.http.ssl_config.verify_mode"] = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object

passes the calls to our service to the SOAP::RPC::driver version of the service as defined in @service

[View source]

43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/adcenter_service.rb', line 43

def method_missing(method, *args)
  begin
    res = eval "@service.#{method}(*args)"
    raise AdCenterClientException.new(method, res.errors) if res.respond_to?(:errors) && ! res.errors.nil?
  rescue SOAP::FaultError => fault
    detail = fault.detail
    if detail.respond_to?(:adApiFaultDetail)
      detail.adApiFaultDetail.errors.each do |r|
        msg = sprintf("*** SOAP Fault found in '%s()': [%d] %s -- %s", method, r.code, r.errorCode, r.message)
        warn msg
      end
    # TODO: test apiFaultDetail
    elsif detail.respond_to?('apiFaultDetail')
      operationErrors = detail.operationErrors.operationError
      if !operationErrors.respond_to?('each')
        operationErrors = [operationErrors]
      end
      operationErrors.each do |opError|
        warn "Operation error '#{opError.message}' (#{opError.code}) encountered.\n"
      end
    # TODO: test generic SOAP error handling
    else
      if String == detail.class
        warn fault.to_s
      else
        warn "Generic SOAP fault '#{detail.exceptionDetail.message}' encountered.\n"
      end
    end
  end
  res
end

Instance Attribute Details

#endpointObject

the endpoint to use when connecting to this service


7
8
9
# File 'lib/adcenter_service.rb', line 7

def endpoint
  @endpoint
end

#required_credentialsObject

array of header fields required by the service


13
14
15
# File 'lib/adcenter_service.rb', line 13

def required_credentials
  @required_credentials
end

#serviceObject

the actual SOAP::RPC::Driver for the service


9
10
11
# File 'lib/adcenter_service.rb', line 9

def service
  @service
end

#service_namespaceObject

sets the namespace to be used in service headers


11
12
13
# File 'lib/adcenter_service.rb', line 11

def service_namespace
  @service_namespace
end

Instance Method Details

#initialize_authentication_headers(creds) ⇒ Object

sets up authentication header handlers

[View source]

30
31
32
33
34
# File 'lib/adcenter_service.rb', line 30

def initialize_authentication_headers(creds)
  @required_credentials.each do |key|
    self.headerhandler << HeaderHandler.new(@service_namespace, key, creds[key])
  end
end

#initialize_serviceObject

initializes the SOAP::RPC::Driver and does per-service configuration

Raises:

  • (NotImplementedError)
[View source]

37
38
39
# File 'lib/adcenter_service.rb', line 37

def initialize_service
  raise NotImplementedError.new("children of AdCenterService must implement #{__method__}")
end