Sha256: a0f3e46985b5f37e383a43098f4d9241197267480045f9224a2faa719e8aad7f

Contents?: true

Size: 1.23 KB

Versions: 10

Compression:

Stored size: 1.23 KB

Contents

# frozen_string_literal: true

module HTTPX
  module Chainable
    %i[head get post put delete trace options connect patch].each do |meth|
      define_method meth do |*uri, **options|
        request(meth, uri, **options)
      end
    end

    def request(verb, uri, **options)
      branch(default_options).request(verb, uri, **options)
    end

    def timeout(**args)
      branch(default_options.with_timeout(args))
    end

    def headers(headers)
      branch(default_options.with_headers(headers))
    end

    def accept(type)
      headers("accept" => String(type))
    end

    def wrap(&blk)
      branch(default_options).wrap(&blk)
    end

    def plugin(*plugins)
      klass = is_a?(Session) ? self.class : Session
      klass = Class.new(klass)
      klass.instance_variable_set(:@default_options, klass.default_options.merge(default_options))
      klass.plugins(plugins).new
    end
    alias_method :plugins, :plugin

    def with(options, &blk)
      branch(default_options.merge(options), &blk)
    end

    private

    def default_options
      @options || Options.new
    end

    # :nodoc:
    def branch(options, &blk)
      return self.class.new(options, &blk) if is_a?(Session)

      Session.new(options, &blk)
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
httpx-0.6.5 lib/httpx/chainable.rb
httpx-0.6.4 lib/httpx/chainable.rb
httpx-0.6.3 lib/httpx/chainable.rb
httpx-0.6.2 lib/httpx/chainable.rb
httpx-0.6.1 lib/httpx/chainable.rb
httpx-0.6.0 lib/httpx/chainable.rb
httpx-0.5.1 lib/httpx/chainable.rb
httpx-0.5.0 lib/httpx/chainable.rb
httpx-0.4.1 lib/httpx/chainable.rb
httpx-0.4.0 lib/httpx/chainable.rb