plug_best v0.2.1 PlugBest

A library that parses HTTP Accept-* headers and returns the best match based on a list of values.

Examples

iex> conn = %Plug.Conn{req_headers: [{"accept-language", "fr-CA,fr;q=0.8,en;q=0.6,en-US;q=0.4"}]}
iex> conn |> PlugBest.best_language(["en", "fr"])
{"fr-CA", "fr", 1.0}

iex> conn = %Plug.Conn{req_headers: [{"accept-language", "es"}]}
iex> conn |> PlugBest.best_language(["fr", "ru"])
nil

iex> conn = %Plug.Conn{req_headers: [{"accept-language", "es"}]}
iex> conn |> PlugBest.best_language_or_first(["ru", "fr"])
{"ru", "ru", 0.0}

Summary

Functions

Returns the best supported langage based on the connection Accept-Language HTTP header. Returns nil if none is found

Returns the best supported langage based on the connection Accept-Language HTTP header. Returns the first supported language if none is found

Types

language :: {String.t, String.t, float}

Functions

best_language(conn, supported_languages)

Specs

best_language(%Plug.Conn{adapter: term, assigns: term, before_send: term, body_params: term, cookies: term, halted: term, host: term, method: term, owner: term, params: term, path_info: term, peer: term, port: term, private: term, query_params: term, query_string: term, remote_ip: term, req_cookies: term, req_headers: term, request_path: term, resp_body: term, resp_cookies: term, resp_headers: term, scheme: term, script_name: term, secret_key_base: term, state: term, status: term}, [String.t, ...]) ::
  language |
  nil

Returns the best supported langage based on the connection Accept-Language HTTP header. Returns nil if none is found.

best_language_or_first(conn, supported_languages)

Specs

best_language_or_first(%Plug.Conn{adapter: term, assigns: term, before_send: term, body_params: term, cookies: term, halted: term, host: term, method: term, owner: term, params: term, path_info: term, peer: term, port: term, private: term, query_params: term, query_string: term, remote_ip: term, req_cookies: term, req_headers: term, request_path: term, resp_body: term, resp_cookies: term, resp_headers: term, scheme: term, script_name: term, secret_key_base: term, state: term, status: term}, [String.t, ...]) :: language

Returns the best supported langage based on the connection Accept-Language HTTP header. Returns the first supported language if none is found.