Sha256: c26149bc31982d5b99ee8b14b7116d7206db1ac40e1e5fe6f32fe5f8dfc65d9e

Contents?: true

Size: 1.09 KB

Versions: 3

Compression:

Stored size: 1.09 KB

Contents

require 'uri'
require 'net/http'

begin
  require 'nokogiri'
rescue LoadError
end

module Arbetsformedlingen
  module API
    class SOAPRequest
      Response = KeyStruct.new(:code, :body, :xml)

      attr_reader :locale, :uri, :url

      def initialize(url, locale: nil)
        unless Object.const_defined?(:Nokogiri)
          raise(ArgumentError, "unable to require 'nokogiri' gem, please install it")
        end

        @url = url
        @uri = URI(url)
        @locale = locale
      end

      def post(body)
        http = Net::HTTP.new(uri.host, uri.port)
        http.use_ssl = true if uri.scheme == 'https'

        request = Net::HTTP::Post.new(uri)
        request['Content-Type'] = 'text/xml'
        request['Accept-Language'] = locale if locale
        request.body = body

        response = http.request(request)

        Response.new(
          code: response.code,
          body: response.read_body,
          xml: parse_xml(response.read_body)
        )
      end

      def parse_xml(string)
        Nokogiri::XML(string).tap { |doc| doc.remove_namespaces! }
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
arbetsformedlingen-0.5.0 lib/arbetsformedlingen/api/soap_request.rb
arbetsformedlingen-0.4.1 lib/arbetsformedlingen/api/soap_request.rb
arbetsformedlingen-0.4.0 lib/arbetsformedlingen/api/soap_request.rb