Mojolicious-Plugin-Host

 view release on metacpan or  search on metacpan

MANIFEST  view on Meta::CPAN

Changes
LICENSE
MANIFEST
META.json
META.yml
README
cpanfile
dist.ini
lib/Mojolicious/Plugin/Host.pm
t/author-pod-syntax.t
t/config_validate.t
t/helper.t
t/www_always.t
t/www_never.t
t/www_unspecified.t

lib/Mojolicious/Plugin/Host.pm  view on Meta::CPAN

package Mojolicious::Plugin::Host;
use Mojo::Base 'Mojolicious::Plugin';
use Carp ();
use Mojo::Util ();

our $VERSION = '0.01';

sub register {
    my (undef, $app, $config) = @_;

    my ($helper, $www) = _parse_and_validate_config($config);
    my $sub;
    if (not defined $www) {
        $sub = sub { $_[0]->req->url->to_abs->host };
    } elsif ($www eq 'always') {
        $sub = sub {
            my $host = $_[0]->req->url->to_abs->host;
            if (index($host, 'www.') != 0) {
                $host = "www.$host";
            }

lib/Mojolicious/Plugin/Host.pm  view on Meta::CPAN


            return $host;
        };
    } else {
        Carp::croak qq{unknown value provided for www: '$www'};
    }

    $app->helper($helper => $sub);
}

sub _parse_and_validate_config {
    my ($config) = @_;

    my $helper;
    if (exists $config->{helper}) {
        $helper = delete $config->{helper};

        my $ref = ref $helper;
        Carp::croak qq{helper must be a string, but was '$ref'} if $ref;
        Carp::croak 'helper must be non-empty' if not defined $helper or $helper eq '';
    } else {



( run in 1.302 second using v1.01-cache-2.11-cpan-39bf76dae61 )