Sha256: b76cc0e5ddaa865b187a33882279cb4ee74764a20238e563d4d3fe8acff48956

Contents?: true

Size: 1.36 KB

Versions: 8

Compression:

Stored size: 1.36 KB

Contents

require 'faraday_middleware'

module Inferno
  module DSL
    # This module contains the HTTP DSL available to test writers.
    class HTTPClientBuilder
      attr_accessor :runnable

      # @private
      def build(runnable, block)
        self.runnable = runnable
        instance_exec(self, &block)

        params = { url: }
        params.merge!(headers:) if headers

        Faraday.new(params) do |f|
          f.request :url_encoded
          f.use FaradayMiddleware::FollowRedirects
        end
      end

      # Define the base url for an HTTP client. A string or symbol can be
      # provided. A string is interpreted as a url. A symbol is interpreted as
      # the name of an input to the Runnable.
      #
      # @param url [String, Symbol]
      # @return [void]
      def url(url = nil)
        @url ||=
          if url.is_a? Symbol
            runnable.send(url)
          else
            url
          end
      end

      # Define custom headers for a client
      #
      # @param headers [Hash]
      # @return [void]
      def headers(headers = nil)
        @headers ||= headers
      end

      # @private
      def method_missing(name, ...)
        return runnable.send(name, ...) if runnable.respond_to? name

        super
      end

      # @private
      def respond_to_missing?(name)
        runnable.respond_to?(name) || super
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
inferno_core-0.6.1 lib/inferno/dsl/http_client_builder.rb
inferno_core-0.6.0 lib/inferno/dsl/http_client_builder.rb
inferno_core-0.5.4 lib/inferno/dsl/http_client_builder.rb
inferno_core-0.5.3 lib/inferno/dsl/http_client_builder.rb
inferno_core-0.5.2 lib/inferno/dsl/http_client_builder.rb
inferno_core-0.5.1 lib/inferno/dsl/http_client_builder.rb
inferno_core-0.5.0 lib/inferno/dsl/http_client_builder.rb
inferno_core-0.4.44 lib/inferno/dsl/http_client_builder.rb