class BingAdsRubySdk::SoapClient

Constants

XSI_NAMESPACE
XSI_NAMESPACE_KEY

Attributes

client[R]
concrete_abstract_mapping[R]
header[R]
lolsoap_parser[R]

Public Class Methods

new(service_name:, version:, environment:, header:) click to toggle source
# File lib/bing_ads_ruby_sdk/soap_client.rb, line 10
def initialize(service_name:, version:, environment:, header:)
  @header = header
  @lolsoap_parser, @concrete_abstract_mapping = cache(service_name) do
    ::BingAdsRubySdk::AugmentedParser.new(
      path_to_wsdl(version, environment, service_name)
    ).call
  end
end

Private Class Methods

cached_parsers() click to toggle source
# File lib/bing_ads_ruby_sdk/soap_client.rb, line 136
def self.cached_parsers
  @cached_parsers
end

Public Instance Methods

call(operation_name, message = {}) click to toggle source
# File lib/bing_ads_ruby_sdk/soap_client.rb, line 19
def call(operation_name, message = {})
  request = lolsoap_client.request(operation_name)

  request.header do |h|
    header.content.each do |k, v|
      h.__send__(k, v)
    end
  end
  request.body do |node|
    insert_args(message, node)
  end

  BingAdsRubySdk.log(:debug) { format_xml(request.content) }

  response_body = BingAdsRubySdk::HttpClient.post(request)

  parse_response(request, response_body)
end
wsdl_wrapper(operation_name) click to toggle source
# File lib/bing_ads_ruby_sdk/soap_client.rb, line 38
def wsdl_wrapper(operation_name)
  WsdlOperationWrapper.new(lolsoap_parser, operation_name)
end

Private Instance Methods

cache(name) { || ... } click to toggle source
# File lib/bing_ads_ruby_sdk/soap_client.rb, line 131
def cache(name)
  self.class.cached_parsers[name] ||= yield
end
format_xml(string) click to toggle source
# File lib/bing_ads_ruby_sdk/soap_client.rb, line 107
def format_xml(string)
  BingAdsRubySdk::LogMessage.new(string).to_s
end
insert_args(args, node) click to toggle source
# File lib/bing_ads_ruby_sdk/soap_client.rb, line 46
def insert_args(args, node)
  # if ever the current node is a subtype
  if base_type_name = concrete_abstract_mapping[node.__type__.name]
    # and add an attribute to specify the real type we want
    node.__attribute__(
      type_attribute_name,
      "#{node.__type__.prefix}:#{node.__node__.name}"
    )
    # we have to change the node name to the base type
    node.__node__.name = base_type_name
  end

  args.each do |arg_name, arg_value|
    case arg_value
    when Hash
      node.__send__(arg_name) do |subnode|
        insert_args(arg_value, subnode)
      end
    when Array
      node.__send__(arg_name) do |subnode|
        # arrays can only contain hashes
        arg_value.each do |elt|
          insert_args(elt, subnode)
        end
      end
    else
      if arg_name == BingAdsRubySdk.type_key
        # this is for now only useful for Account. Indeed, for some unknown reason
        # Account is abstract, AdvertiserAccount is the only expect subtype
        # yet the wsdl doesnt declare it as an actual subtype
        node.__attribute__(
          type_attribute_name,
          prefixed_type_name(arg_value)
        )
      else
        node.__send__(arg_name, arg_value)
      end
    end
  end
end
lolsoap_client() click to toggle source
# File lib/bing_ads_ruby_sdk/soap_client.rb, line 97
def lolsoap_client
  @lolsoap ||= LolSoap::Client.new(lolsoap_wsdl).tap do |c|
    c.wsdl.namespaces[XSI_NAMESPACE_KEY] = XSI_NAMESPACE
  end
end
lolsoap_wsdl() click to toggle source
# File lib/bing_ads_ruby_sdk/soap_client.rb, line 103
def lolsoap_wsdl
  @lolsoap_wsdl ||= LolSoap::WSDL.new(lolsoap_parser)
end
parse_response(req, response_body) click to toggle source
# File lib/bing_ads_ruby_sdk/soap_client.rb, line 87
def parse_response(req, response_body)
  lolsoap_client.response(req, response_body).body_hash.tap do |b_h|
    BingAdsRubySdk.log(:debug) { b_h }
    BingAdsRubySdk::Errors::ErrorHandler.new(b_h).call
  end
rescue BingAdsRubySdk::Errors::GeneralError => e
  BingAdsRubySdk.log(:warn) { format_xml(response_body) }
  raise e
end
path_to_wsdl(version, environment, service_name) click to toggle source
# File lib/bing_ads_ruby_sdk/soap_client.rb, line 111
def path_to_wsdl(version, environment, service_name)
  File.join(
    BingAdsRubySdk.root_path,
    'lib',
    'bing_ads_ruby_sdk',
    'wsdl',
    version.to_s,
    environment.to_s,
    "#{service_name}.xml"
  )
end
prefixed_type_name(typename) click to toggle source
# File lib/bing_ads_ruby_sdk/soap_client.rb, line 123
def prefixed_type_name(typename)
  WsdlOperationWrapper.prefix_and_name(lolsoap_wsdl, typename)
end
type_attribute_name() click to toggle source
# File lib/bing_ads_ruby_sdk/soap_client.rb, line 127
def type_attribute_name
  "#{XSI_NAMESPACE_KEY}:type"
end