1
1
# Base negotiator type. Implements logic common to all negotiators.
2
- abstract class Athena::Negotiation::AbstractNegotiator
2
+ abstract class Athena::Negotiation::AbstractNegotiator ( HeaderType )
3
3
private record OrderKey , quality : Float32 , index : Int32 , value : String do
4
4
include Comparable (self )
5
5
@@ -9,24 +9,22 @@ abstract class Athena::Negotiation::AbstractNegotiator
9
9
end
10
10
end
11
11
12
- private abstract def create_header (header : String ) : ANG ::BaseAccept
13
-
14
- # Returns the best `ANG::BaseAccept` type based on the provided *header* value and *priorities*.
12
+ # Returns the best `HeaderType` type based on the provided *header* value and *priorities*.
15
13
#
16
14
# See `Athena::Negotiation` for examples.
17
- def best (header : String , priorities : Indexable (String ), strict : Bool = false ) : ANG :: BaseAccept ?
15
+ def best (header : String , priorities : Indexable (String ), strict : Bool = false ) : HeaderType ?
18
16
raise ArgumentError .new " priorities should not be empty." if priorities.empty?
19
17
raise ArgumentError .new " The header string should not be empty." if header.blank?
20
18
21
- accepted_headers = Array (ANG :: BaseAccept ).new
19
+ accepted_headers = Array (HeaderType ).new
22
20
23
21
self .parse_header(header) do |h |
24
- accepted_headers << self .create_header h
22
+ accepted_headers << HeaderType .new h
25
23
rescue ex
26
24
raise ex if strict
27
25
end
28
26
29
- accepted_priorties = priorities.map &- > create_header( String )
27
+ accepted_priorties = priorities.map { | p | HeaderType .new p }
30
28
31
29
matches = self .find_matches accepted_headers, accepted_priorties
32
30
@@ -41,18 +39,28 @@ abstract class Athena::Negotiation::AbstractNegotiator
41
39
match.nil? ? nil : accepted_priorties[match.index]
42
40
end
43
41
44
- # Returns an array of `ANG::BaseAccept ` types that the provided *header* allows, ordered so that the `#best` match is first.
42
+ # Returns an array of `HeaderType ` types that the provided *header* allows, ordered so that the `#best` match is first.
45
43
#
46
- # See `Athena::Negotiation` for examples.
47
- def ordered_elements (header : String ) : Array (ANG ::BaseAccept )
44
+ # ```
45
+ # header = "text/*;q=0.3, text/html;q=0.7, text/html;level=1, text/html;level=2;q=0.4, */*;q=0.5"
46
+ #
47
+ # ordered_elements = ANG.negotiator.ordered_elements header
48
+ #
49
+ # ordered_elements[0].media_range # => "text/html"
50
+ # ordered_elements[1].media_range # => "text/html"
51
+ # ordered_elements[2].media_range # => "*/*"
52
+ # ordered_elements[3].media_range # => "text/html"
53
+ # ordered_elements[4].media_range # => "text/*"
54
+ # ```
55
+ def ordered_elements (header : String ) : Array (HeaderType )
48
56
raise ArgumentError .new " The header string should not be empty." if header.blank?
49
57
50
- elements = Array (ANG :: BaseAccept ).new
58
+ elements = Array (HeaderType ).new
51
59
order_keys = Array (OrderKey ).new
52
60
53
61
idx = 0
54
62
self .parse_header(header) do |h |
55
- element = self .create_header h
63
+ element = HeaderType .new h
56
64
elements << element
57
65
order_keys << OrderKey .new element.quality, idx, element.header
58
66
rescue ex
@@ -85,7 +93,7 @@ abstract class Athena::Negotiation::AbstractNegotiator
85
93
end
86
94
end
87
95
88
- private def find_matches (headers : Array (ANG :: BaseAccept ), priorities : Indexable (ANG :: BaseAccept )) : Array (ANG ::AcceptMatch )
96
+ private def find_matches (headers : Array (HeaderType ), priorities : Indexable (HeaderType )) : Array (ANG ::AcceptMatch )
89
97
matches = [] of ANG ::AcceptMatch
90
98
91
99
priorities.each_with_index do |priority , idx |
0 commit comments