Sha256: bd6989a1b2b0cd0fdc64bbf0b4730e91dc435ea4bddc2fa49b5f52203ff8fa14

Contents?: true

Size: 1.37 KB

Versions: 34

Compression:

Stored size: 1.37 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, *args, &)
        return runnable.call(name, *args, &) 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

34 entries across 34 versions & 1 rubygems

Version Path
inferno_core-0.4.29 lib/inferno/dsl/http_client_builder.rb
inferno_core-0.4.28 lib/inferno/dsl/http_client_builder.rb
inferno_core-0.4.27 lib/inferno/dsl/http_client_builder.rb
inferno_core-0.4.26 lib/inferno/dsl/http_client_builder.rb
inferno_core-0.4.25 lib/inferno/dsl/http_client_builder.rb
inferno_core-0.4.24 lib/inferno/dsl/http_client_builder.rb
inferno_core-0.4.23 lib/inferno/dsl/http_client_builder.rb
inferno_core-0.4.22 lib/inferno/dsl/http_client_builder.rb
inferno_core-0.4.21 lib/inferno/dsl/http_client_builder.rb
inferno_core-0.4.20 lib/inferno/dsl/http_client_builder.rb
inferno_core-0.4.19 lib/inferno/dsl/http_client_builder.rb
inferno_core-0.4.18 lib/inferno/dsl/http_client_builder.rb
inferno_core-0.4.17 lib/inferno/dsl/http_client_builder.rb
inferno_core-0.4.16 lib/inferno/dsl/http_client_builder.rb
inferno_core-0.4.15 lib/inferno/dsl/http_client_builder.rb
inferno_core-0.4.14 lib/inferno/dsl/http_client_builder.rb
inferno_core-0.4.13 lib/inferno/dsl/http_client_builder.rb
inferno_core-0.4.12 lib/inferno/dsl/http_client_builder.rb
inferno_core-0.4.11 lib/inferno/dsl/http_client_builder.rb
inferno_core-0.4.10 lib/inferno/dsl/http_client_builder.rb