@@ -14,93 +14,6 @@ require "./exceptions/*"
1414alias ANG = Athena ::Negotiation
1515
1616# The `Athena::Negotiation` component allows an application to support [content negotiation](https://tools.ietf.org/html/rfc7231#section-5.3).
17- # The component has no dependencies and is framework agnostic; supporting various negotiators.
18- #
19- # ## Getting Started
20- #
21- # If using this component within the [Athena Framework][Athena::Framework], it is already installed and required for you.
22- # Checkout the [manual](../architecture/negotiation.md) for some additional information on how to use it within the framework.
23- #
24- # If using it outside of the framework, you will first need to add it as a dependency:
25- #
26- # ```yaml
27- # dependencies:
28- # athena-negotiation:
29- # github: athena-framework/negotiation
30- # version: ~> 0.1.0
31- # ```
32- #
33- # Then run `shards install`, being sure to require it via `require "athena-negotiation"`.
34- #
35- # ## Usage
36- #
37- # The main type of `Athena::Negotiation` is `ANG::AbstractNegotiator` which is used to implement negotiators for each `Accept*` header.
38- # `Athena::Negotiation` exposes class level getters for each negotiator; that return a lazily initialized singleton instance.
39- # Each negotiator exposes two methods: `ANG::AbstractNegotiator#best` and `ANG::AbstractNegotiator#ordered_elements`.
40- #
41- # ### Media Type
42- #
43- # ```
44- # negotiator = ANG.negotiator
45- #
46- # accept_header = "text/html, application/xhtml+xml, application/xml;q=0.9"
47- # priorities = ["text/html; charset=UTF-8", "application/json", "application/xml;q=0.5"]
48- #
49- # accept = negotiator.best(accept_header, priorities).not_nil!
50- #
51- # accept.media_range # => "text/html"
52- # accept.parameters # => {"charset" => "UTF-8"}
53- # ```
54- #
55- # The `ANG::Negotiator` type returns an `ANG::Accept`, or `nil` if negotiating the best media type has failed.
56- #
57- # ### Character Set
58- #
59- # ```
60- # negotiator = ANG.charset_negotiator
61- #
62- # accept_header = "ISO-8859-1, UTF-8; q=0.9"
63- # priorities = ["iso-8859-1;q=0.3", "utf-8;q=0.9", "utf-16;q=1.0"]
64- #
65- # accept = negotiator.best(accept_header, priorities).not_nil!
66- #
67- # accept.charset # => "utf-8"
68- # accept.quality # => 0.9
69- # ```
70- #
71- # The `ANG::CharsetNegotiator` type returns an `ANG::AcceptCharset`, or `nil` if negotiating the best character set has failed.
72- #
73- # ### Encoding
74- #
75- # ```
76- # negotiator = ANG.encoding_negotiator
77- #
78- # accept_header = "gzip;q=1.0, identity; q=0.5, *;q=0"
79- # priorities = ["gzip", "foo"]
80- #
81- # accept = negotiator.best(accept_header, priorities).not_nil!
82- #
83- # accept.coding # => "gzip"
84- # ```
85- #
86- # The `ANG::EncodingNegotiator` type returns an `ANG::AcceptEncoding`, or `nil` if negotiating the best encoding has failed.
87- #
88- # ### Language
89- #
90- # ```
91- # negotiator = ANG.language_negotiator
92- #
93- # accept_header = "en; q=0.1, fr; q=0.4, zh-Hans-CN; q=0.9, de; q=0.2"
94- # priorities = ["de", "zh-Hans-CN", "en"]
95- #
96- # accept = negotiator.best(accept_header, priorities).not_nil!
97- #
98- # accept.language # => "zh"
99- # accept.region # => "cn"
100- # accept.script # => "hans"
101- # ```
102- #
103- # The `ANG::LanguageNegotiator` type returns an `ANG::AcceptLanguage`, or `nil` if negotiating the best language has failed.
10417module Athena::Negotiation
10518 VERSION = " 0.1.4"
10619
0 commit comments