Class: Arbetsformedlingen::API::BaseSOAPClient

Inherits:
Object
  • Object
show all
Defined in:
lib/arbetsformedlingen/api/base_soap_client.rb

Overview

Base client for SOAP clients

Direct Known Subclasses

TaxonomyClient, WSOccupationClient

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, namespace) ⇒ BaseSOAPClient

Initialize client



13
14
15
16
17
# File 'lib/arbetsformedlingen/api/base_soap_client.rb', line 13

def initialize(url, namespace)
  @service_url = url
  @namespace = namespace
  @request = SOAPRequest.new(url)
end

Instance Attribute Details

#namespaceObject (readonly)

Returns the value of attribute namespace.



10
11
12
# File 'lib/arbetsformedlingen/api/base_soap_client.rb', line 10

def namespace
  @namespace
end

#requestObject (readonly)

Returns the value of attribute request.



10
11
12
# File 'lib/arbetsformedlingen/api/base_soap_client.rb', line 10

def request
  @request
end

#service_urlObject (readonly)

Returns the value of attribute service_url.



10
11
12
# File 'lib/arbetsformedlingen/api/base_soap_client.rb', line 10

def service_url
  @service_url
end

Instance Method Details

#client_request(name, args: {}) ⇒ Response

Perform client request

Parameters:

  • name (String)

    of the main node

  • args (Hash) (defaults to: {})

    optional arguments

Returns:



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/arbetsformedlingen/api/base_soap_client.rb', line 23

def client_request(name, args: {})
  soap_body = SOAPBuilder.wrap do |body| # rubocop:disable Lint/UnusedBlockArgument
    # HACK: Work around the XMLBuilder DSL
    ruby_statements = args.map do |key, value|
      if value.is_a?(Array)
        inner_ruby_statements = value.map do |(k, v)|
          "inode.#{k}('#{v}')"
        end

        <<~RUBY_EVAL
          node.#{key} { |inode| #{inner_ruby_statements.join(';')} }
        RUBY_EVAL
      else
        "node.#{key}('#{value}')"
      end
    end

    ruby_xml_builder_code = <<~RUBY_EVAL
      body.#{name}(xmlns: namespace) { |node| #{ruby_statements.join(';')} }
    RUBY_EVAL

    instance_eval(ruby_xml_builder_code)
  end

  request.post(soap_body.to_xml)
end