plug_best v0.3.0 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 charset based on the connection Accept-Charset HTTP header. Returns nil if none is found

Returns the best supported charset based on the connection Accept-Charset HTTP header. Returns the first supported charset if none is found

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

Returns the best supported encoding based on the connection Accept-Encoding HTTP header. Returns the first supported encoding if none is found

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

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

Returns the best supported type based on the connection Accept HTTP header. Returns the first supported type if none is found

Types

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

Functions

best_charset(conn, supported_values)

Specs

best_charset(%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, path_params: 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]) ::
  value |
  nil

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

best_charset_or_first(conn, supported_values)

Specs

best_charset_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, path_params: 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]) ::
  value |
  nil

Returns the best supported charset based on the connection Accept-Charset HTTP header. Returns the first supported charset if none is found.

best_encoding(conn, supported_values)

Specs

best_encoding(%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, path_params: 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]) ::
  value |
  nil

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

best_encoding_or_first(conn, supported_values)

Specs

best_encoding_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, path_params: 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]) ::
  value |
  nil

Returns the best supported encoding based on the connection Accept-Encoding HTTP header. Returns the first supported encoding if none is found.

best_language(conn, supported_values)

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, path_params: 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]) ::
  value |
  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_values)

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, path_params: 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]) ::
  value |
  nil

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

best_type(conn, supported_values)

Specs

best_type(%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, path_params: 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]) ::
  value |
  nil

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

best_type_or_first(conn, supported_values)

Specs

best_type_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, path_params: 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]) ::
  value |
  nil

Returns the best supported type based on the connection Accept HTTP header. Returns the first supported type if none is found.