class Endpoint

Represents a named HTTP backend endpoint.

Definitions

def self.coerce(description)

Coerce an endpoint or endpoint description into an endpoint.

Signature

parameter description Endpoint | Hash

The endpoint or serialized endpoint description.

returns Endpoint

The coerced endpoint.

Implementation

def self.coerce(description)
	return description if description.is_a?(self)
	
	name = description[:name] || description["name"]
	url = description[:url] || description["url"]
	protocol = description[:protocol] || description["protocol"]
	
	self.new(name, url, protocol: protocol)
end

def initialize(name, url, protocol: nil)

Initialize a named backend endpoint.

Signature

parameter name String

The stable endpoint identity.

parameter url String

The absolute backend URL.

parameter protocol String | Nil

The upstream HTTP protocol name.

Implementation

def initialize(name, url, protocol: nil)
	raise ArgumentError, "Endpoint name is required!" unless name
	raise ArgumentError, "Endpoint URL is required!" unless url
	
	@name = name.to_s
	@url = url.to_s
	@protocol = protocol&.to_s
	
	freeze
end

attr :name

Signature

attribute String

The stable endpoint identity.

attr :url

Signature

attribute String

The absolute backend URL.

attr :protocol

Signature

attribute String | Nil

The upstream HTTP protocol name.

def make_client(exchange_limit:)

Build an HTTP client for this endpoint.

Signature

parameter exchange_limit Integer

The maximum number of outstanding response exchanges.

returns Async::HTTP::Client

A client connected to this endpoint.

Implementation

def make_client(exchange_limit:)
	endpoint = Async::HTTP::Endpoint.parse(@url, protocol: protocol_module)
	
	Async::HTTP::Client.new(endpoint, limit: exchange_limit, retries: 0)
end

def to_h

Convert this endpoint into a transport-safe description.

Signature

returns Hash

A serialized endpoint description.

Implementation

def to_h
	{
		"name" => @name,
		"url" => @url,
		"protocol" => @protocol,
	}.compact
end

def ==(other)

Compare endpoints by their serialized configuration.

Implementation

def ==(other)
	other.is_a?(Endpoint) && other.to_h == self.to_h
end