Class: DFAClient

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

Overview

A Savon Client for DFA

Instance Method Summary collapse

Constructor Details

#initialize(version = 'v1.19') ⇒ DFAClient

Returns a new instance of DFAClient.



6
7
8
9
10
11
# File 'lib/dfa_client.rb', line 6

def initialize(version = 'v1.19')
  @namespace = "http://www.doubleclick.net/dfa-api/#{version}"
  @baseurl = "https://advertisersapi.doubleclick.com/#{version}/api/dfa-api/"
  @token = nil
  @last_response = nil
end

Instance Method Details

#authenticate(username = nil, password = nil) ⇒ Object

Authenticate with the DFA soap service.



14
15
16
17
18
19
# File 'lib/dfa_client.rb', line 14

def authenticate(username = nil, password = nil)
  @username = username
  @password = password
  response = self.request('login', :authenticate, {username: @username, password: @password})
  @token = response[:token]
end

#connected?Boolean

Do we have a token?

Returns:

  • (Boolean)


22
23
24
# File 'lib/dfa_client.rb', line 22

def connected?
  @token
end

#error(response) ⇒ Object

Handle soap and http errors.



38
39
40
# File 'lib/dfa_client.rb', line 38

def error(response)
  throw response.http_error? ? response.http_error : response.soap_fault
end

#request(endpoint = '', method = '', params = {}, raw = false) ⇒ Object

Make a request to the DFA API.



27
28
29
30
31
32
33
34
35
# File 'lib/dfa_client.rb', line 27

def request(endpoint = '', method='', params = {}, raw = false)
  client = Savon.client(wsdl: "#{@baseurl}#{endpoint}?wsdl") do |locals|
    locals.wsse_auth(@username, @token) if @token
  end
  @last_response = client.call(method, message: params)

  # Return data or handle exceptions.
  @last_response.success? ? (raw ? @last_response : @last_response.to_hash[:multi_ref]) : error(@last_response)
end