Class: Bing::Ads::API::V11::Services::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/bing/ads/api/v11/services/base.rb

Overview

Bing::Ads::API::V11::Base

Direct Known Subclasses

Bulk, CampaignManagement, CustomerManagement

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Base

  • environment - :production or :sandbox

  • developer_token - client application’s developer access token

  • customer_id - identifier for the customer that owns the account

  • account_id - identifier of the account that own the entities in the request

  • client_settings - Hash with any Client additional options (such as header, logger or enconding)

  • retry_attempts - Number of times the service must retry on failure

  • log_level - :debug :warn :error :fatal

(EITHER)

  • authentication_token - OAuth2 token

(OR)

  • username - Bing Ads username

  • password - Bing Ads password

Parameters:

  • options (defaults to: {})
    • Hash with autentication and environment settings



23
24
25
26
27
28
29
30
31
32
# File 'lib/bing/ads/api/v11/services/base.rb', line 23

def initialize(options = {})
  @environment = options.delete(:environment)
  @retry_attempts = options.delete(:retry_attempts) || 0
  @account_id = options[:account_id]
  @customer_id = options[:customer_id]
  raise 'You must set the service environment' unless @environment
  options[:wsdl_url] = service_wsdl_url
  options[:namespace_identifier] = Bing::Ads::API::V11::NAMESPACE_IDENTIFIER
  @soap_client = Bing::Ads::API::SOAPClient.new(options)
end

Instance Attribute Details

#environmentObject

Returns the value of attribute environment.



8
9
10
# File 'lib/bing/ads/api/v11/services/base.rb', line 8

def environment
  @environment
end

#retry_attemptsObject

Returns the value of attribute retry_attempts.



8
9
10
# File 'lib/bing/ads/api/v11/services/base.rb', line 8

def retry_attempts
  @retry_attempts
end

#soap_clientObject

Returns the value of attribute soap_client.



8
9
10
# File 'lib/bing/ads/api/v11/services/base.rb', line 8

def soap_client
  @soap_client
end

Instance Method Details

#call(operation, payload) ⇒ Object

This is a utility wrapper for calling services into the SOAPClient. This methods handle the Savon::Client Exceptions and returns a Hash with the call response

Examples:

service.call(:some_operation, { key: value })
# => <Hash>

Parameters:

  • operation
    • name of the operation to be called

  • payload
    • hash with the parameters to the operation

Returns:

  • Hash with the result of the service call

Raises:

  • ServiceError if the SOAP call fails or the response is invalid



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
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/bing/ads/api/v11/services/base.rb', line 47

def call(operation, payload)
  retries_made = 0
  raise 'You must provide an operation' if operation.nil?
  begin
    response = soap_client.call(operation: operation.to_sym, payload: payload)
    return response.hash
  rescue Savon::SOAPFault => error
    fault_detail = error.to_hash[:fault][:detail]
    if fault_detail.key?(:api_fault_detail)
      handle_soap_fault(operation, fault_detail, :api_fault_detail)
    elsif fault_detail.key?(:ad_api_fault_detail)
      handle_soap_fault(operation, fault_detail, :ad_api_fault_detail)
    else
      if retries_made < retry_attempts
        sleep(2**retries_made)
        retries_made += 1
        retry
      else
        raise Bing::Ads::API::Errors::UnhandledSOAPFault,
              "SOAP error (#{fault_detail.keys.join(', ')}) while calling #{operation}. #{error.message}"
      end
    end
  rescue Savon::HTTPError => error
    # TODO better handling
    raise
  rescue Savon::InvalidResponseError => error
    # TODO better handling
    raise
  rescue
    if retries_made < retry_attempts
      sleep(2**retries_made)
      retries_made += 1
      retry
    else
      raise
    end
  end
end

#response_body(response, method) ⇒ Object

Extracts the actual response from the entire response hash.

Examples:

service.response_body(Hash, 'add_campaigns')
# => Hash

Parameters:

  • response
    • The complete response hash received from a Operation call

  • method
    • Name of the method of with the ‘reponse’ tag is require

Returns:

  • Hash with the content of the called method response hash



96
97
98
# File 'lib/bing/ads/api/v11/services/base.rb', line 96

def response_body(response, method)
  response[:envelope][:body]["#{method}_response".to_sym]
end