The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Dancer2::Plugin::Negotiate - Content negotiation plugin for Dancer2

VERSION

version 0.002

SYNOPSIS

    use Dancer2::Plugin::Negotiate;
	
	get '...' => sub {
		choose_variant(
			var1 => {
				Quality => 1.000,
				Type => 'text/html',
				Charset => 'iso-8859-1',
				Language => 'en',
				Size => 3000
			},
			var2 => {
				Quality => 0.950,
				Type => 'text/plain',
				Charset => 'us-ascii',
				Language => 'no',
				Size => 400
			},
			var3 => {
				Quality => 0.3,
				Type => 'image/gif',
				Size => 43555
			}
		); # returns 'var1' or 'var2' or 'var3' or undef
	}

DESCRIPTION

This module is a wrapper for HTTP::Negotiate.

METHODS

choose_variant(%variants)

%options is a hash like this:

%variants = (
	$identifier => \%options
)

The key $identifier is a string that will be returned by choose_variant().

Valid keywords of hashref \%options:

Quality

A float point value between 0.000 and 1.000, describing the source quality (defaults to 1)

Type

A MIME media type (with no charset attributes, but other attributes like version)

Encoding

An encoding like gzip or compress

Charset

An encoding like utf-8 or iso-8859-1

Language

A language tag conforming to RFC 3066

Size

Number of bytes used to represent

Returns undef if no variant matched.

See HTTP::Negotiate for more information.

apply_variant(%options)

This method behaves like choose_variant but sets the according response headers if a variant matched.

negotiate($template_name)

This method returns $template_name with a suffixed language tag. The file needs to exist. This method behaves similiary to mod_negotiate of apache httpd.

Language tags must be specified in plugin settings and ordered by priority:

plugins:
  Negotiate:
    languages:
      - en
      - de
      - fr

The result of this method can be propagated to template() in order to render a localized version of the file.

get '/index' => sub {
	return template negotiate 'index';
}; # renders index.de.tt or index.en.tt or index.fr.tt or index.tt 

Falls back to $template_name if negotiaten fails.

Hint: additional arguments applies to template():

template negotiate index => { foo => 'bar' };
# is the same as
template(negotiate('index'), { foo => 'bar' });

BUGS

Please report any bugs or feature requests on the bugtracker website https://github.com/zurborg/libdancer2-plugin-negotiate-perl/issues

When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.

AUTHOR

David Zurborg <zurborg@cpan.org>

COPYRIGHT AND LICENSE

This software is Copyright (c) 2015 by David Zurborg.

This is free software, licensed under:

The ISC License