Sha256: 6eeca43922b5507f2544b0915d622d23a36a6017d3d3be144562571937f0454e

Contents?: true

Size: 1.44 KB

Versions: 1

Compression:

Stored size: 1.44 KB

Contents

# frozen_string_literal: true

require 'arbetsformedlingen/soap_builder'
require 'arbetsformedlingen/api/soap_request'

module Arbetsformedlingen
  module API
    # Base client for SOAP clients
    class BaseSOAPClient
      attr_reader :request, :namespace, :service_url

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

      # Perform client request
      # @param [String] name of the main node
      # @param args [Hash] optional arguments
      # @return [Response]
      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
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
arbetsformedlingen-0.7.0 lib/arbetsformedlingen/api/base_soap_client.rb