Sha256: 0ac3ba37e8d69a0a45940e68153b42820ffb72ecb9bcd5321a4fe777fd42a700

Contents?: true

Size: 1.13 KB

Versions: 1

Compression:

Stored size: 1.13 KB

Contents

# frozen_string_literal: true

require "faraday"

module Wasabi

  # = Wasabi::Resolver
  #
  # Resolves local and remote WSDL documents.
  class Resolver

    class HTTPError < StandardError
      def initialize(message, response=nil)
        super(message)
        @response = response
      end
      attr_reader :response
    end

    URL = /^http[s]?:/
    XML = /^</

    def initialize(document, request = nil, adapter = nil)
      @document = document
      @request  = request || Faraday.new
      @adapter  = adapter
    end

    attr_reader :document, :request, :adapter

    def resolve
      raise ArgumentError, "Unable to resolve: #{document.inspect}" unless document

      case document
        when URL then load_from_remote
        when XML then document
        else          load_from_disc
      end
    end

    private

    def load_from_remote
      request.adapter *adapter if adapter
      response = request.get(document)

      raise HTTPError.new("Error: #{response.status} for url #{document}", response) unless response.success?

      response.body
    end

    def load_from_disc
      File.read(document)
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
wasabi-5.0.0 lib/wasabi/resolver.rb