Sha256: 22b3c2c710afe53e53765d13a1a1620a0b48dc68cf172051b4da5ceb99778e91

Contents?: true

Size: 1012 Bytes

Versions: 1

Compression:

Stored size: 1012 Bytes

Contents

# frozen_string_literal: true

require "http"
require "ezclient/version"
require "ezclient/request"
require "ezclient/response"

class EzClient
  def initialize(options = {})
    self.options = options
    self.clients = {}
  end

  def request(verb, url, **options)
    keep_alive_timeout = options.delete(:keep_alive)

    if keep_alive_timeout
      client = persistent_client_for(url, timeout: keep_alive_timeout)
    else
      client = HTTP::Client.new
    end

    Request.new(verb, url, client: client, **default_options, **options)
  end

  private

  attr_accessor :options, :clients

  def persistent_client_for(url, timeout: 600)
    uri = HTTP::URI.parse(url)
    clients[uri.origin] ||= HTTP.persistent(uri.origin, timeout: timeout)
  end

  def default_options
    {
      on_complete: options[:on_complete],
      on_error: options[:on_error],
      timeout: options[:default_timeout],
      retry_exceptions: options[:retry_exceptions],
      max_retries: options[:max_retries],
    }
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ezclient-0.4.0 lib/ezclient.rb