Class: BingAdsApi::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/bing-ads-api/service.rb

Overview

Public : Base class for service object

Author

[email protected]

Direct Known Subclasses

CampaignManagement, CustomerManagement, Reporting

Constant Summary collapse

LOGGER =

Default logger for services

Logger.new(STDOUT)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Service

Public : Constructor

Author

[email protected]

Parameters

  • options - Hash with autentication and environment settings

Options

  • environment - :production or :sandbox

  • username - Bing Ads username

  • passwrod - Bing Ads user’s sign-in password

  • 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

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

Examples

service = BingAdsApi::Service.new(
  :environment => :sandbox,
  :username    => 'username',
  :password    => 'pass',
  :developer_token => 'SOME_TOKEN',
  :account_id  => 123456,
  :customer_id => 654321,
  :proxy => {:logger => Rails.logger}
) 
# => <Service>


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/bing-ads-api/service.rb', line 44

def initialize(options={})
	
	# Service Environment
	self.environment = options[:environment]

	# ClientProxy settings
	clientProxySettings = {
		:username => options[:username],
		:password => options[:password],
		:developer_token => options[:developer_token],
		:account_id => options[:account_id],
		:customer_id => options[:customer_id],
		:wsdl_url => options[:wdsl] || solve_wsdl_url
	}
	
	# Additionsl ClientProxy settings
	clientProxySettings[:proxy] = options[:proxy] if options[:proxy]
	
	# ClientProxy creation
	self.client_proxy = BingAdsApi::ClientProxy.new(clientProxySettings)

end

Instance Attribute Details

#client_proxyObject

Returns the value of attribute client_proxy.



11
12
13
# File 'lib/bing-ads-api/service.rb', line 11

def client_proxy
  @client_proxy
end

#environmentObject

Returns the value of attribute environment.



11
12
13
# File 'lib/bing-ads-api/service.rb', line 11

def environment
  @environment
end

Instance Method Details

#call(operation, message, &block) ⇒ Object

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

Author

[email protected]

Parameters

operation - name of the operation to be called message - hash with the parameters to the operation

Examples

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

Hash with the result of the service call

Raises

ServiceError if the SOAP call, the ClientProxy fails or the response is invalid



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/bing-ads-api/service.rb', line 84

def call(operation, message, &block)
	raise "You must provide an operation" if operation.nil?
	begin
		LOGGER.debug "BingAdsApi Service"
		LOGGER.debug "   Calling #{operation.to_s}"
		LOGGER.debug "   Message: #{message}"
		response = self.client_proxy.call(operation.to_sym, 
			message: message)
	
		LOGGER.debug "response header:"
		LOGGER.debug "\t#{response.header}"

		LOGGER.info "Operation #{operation.to_s} call success"
		return response.hash
	rescue Savon::SOAPFault => error
		LOGGER.error "SOAP Error calling #{operation.to_s}: #{error.http.code}"
		fault_detail = error.to_hash[:fault][:detail]
		if fault_detail.key?(:api_fault_detail)
			api_fault_detail = BingAdsApi::ApiFaultDetail.new(fault_detail[:api_fault_detail])
			raise BingAdsApi::ApiException.new(
				api_fault_detail, "SOAP Error calling #{operation.to_s}")
		elsif fault_detail.key?(:ad_api_fault_detail) 
			ad_api_fault_detail = BingAdsApi::AdApiFaultDetail.new(fault_detail[:ad_api_fault_detail])
			raise BingAdsApi::ApiException.new(
				ad_api_fault_detail, "SOAP Error calling #{operation.to_s}")
		else
			raise
		end
	rescue Savon::HTTPError => error
		LOGGER.error "Http Error calling #{operation.to_s}: #{error.http.code}"
		raise
	rescue Savon::InvalidResponseError => error
		LOGGER.error "Invalid server reponse calling #{operation.to_s}"
		raise
	end
end

#get_response_hash(response, method) ⇒ Object

Public : Extracts the actual response from the entire response hash. For example, if you specify ‘AddCampaigns’, this method will return the content of ‘AddCampaignsResponse’ tag as a Hash

Author

[email protected]

Parameters

response - The complete response hash received from a Operation call method - Name of the method of with the ‘reponse’ tag is require

Examples

service.get_response_hash(Hash, 'add_campaigns') 
# => Hash
Returns

Hash with the inner structure of the method response hash

Raises

exception



138
139
140
# File 'lib/bing-ads-api/service.rb', line 138

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