-
-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add configurable and defaultable concepts.
- Loading branch information
Showing
9 changed files
with
157 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# frozen_string_literal: true | ||
|
||
# Released under the MIT License. | ||
# Copyright, 2025, by Samuel Williams. | ||
|
||
module Async | ||
module HTTP | ||
module Protocol | ||
class Configured | ||
def initialize(protocol, **options) | ||
@protocol = protocol | ||
@options = options | ||
end | ||
|
||
# @attribute [Protocol] The underlying protocol. | ||
attr :protocol | ||
|
||
# @attribute [Hash] The options to pass to the protocol. | ||
attr :options | ||
|
||
def client(peer, **options) | ||
options = @options.merge(options) | ||
@protocol.client(peer, **options) | ||
end | ||
|
||
def server(peer, **options) | ||
options = @options.merge(options) | ||
@protocol.server(peer, options) | ||
end | ||
|
||
def names | ||
@protocol.names | ||
end | ||
end | ||
|
||
module Configurable | ||
def new(**options) | ||
Configured.new(self, **options) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# frozen_string_literal: true | ||
|
||
# Released under the MIT License. | ||
# Copyright, 2025, by Samuel Williams. | ||
|
||
module Async | ||
module HTTP | ||
module Protocol | ||
module Defaultable | ||
def self.extended(base) | ||
base.const_set(:DEFAULT, base.new) | ||
end | ||
|
||
# The default instance of the protocol. | ||
def default | ||
self::DEFAULT | ||
end | ||
|
||
# Create a client for an outbound connection, using the default instance. | ||
def client(peer, **options) | ||
default.client(peer, **options) | ||
end | ||
|
||
# Create a server for an inbound connection, using the default instance. | ||
def server(peer, **options) | ||
default.server(peer, **options) | ||
end | ||
|
||
# @returns [Array] The names of the supported protocol, used for Application Layer Protocol Negotiation (ALPN), using the default instance. | ||
def names | ||
default.names | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# frozen_string_literal: true | ||
|
||
# Released under the MIT License. | ||
# Copyright, 2024, by Thomas Morgan. | ||
# Copyright, 2024, by Samuel Williams. | ||
|
||
require "async/http/protocol/http" | ||
require "async/http/a_protocol" | ||
|
||
describe Async::HTTP::Protocol::HTTP1 do | ||
with ".new" do | ||
it "can configure the protocol" do | ||
protocol = subject.new( | ||
persistent: false, | ||
maximum_line_length: 4096, | ||
) | ||
|
||
expect(protocol.options).to have_keys( | ||
persistent: be == false, | ||
maximum_line_length: be == 4096, | ||
) | ||
end | ||
end | ||
end |