Sha256: 03e7d9577f8db01d239d7671c51f07da39fa1649ec69249b5fdda67ad5ada255

Contents?: true

Size: 855 Bytes

Versions: 1

Compression:

Stored size: 855 Bytes

Contents

# frozen_string_literal: true

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

class EzClient
  def initialize(on_complete: nil, on_error: nil)
    self.clients = {}
    self.on_complete = on_complete
    self.on_error = on_error
  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, on_complete: on_complete, on_error: on_error, **options)
  end

  private

  attr_accessor :clients, :on_complete, :on_error

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ezclient-0.1.0 lib/ezclient.rb