NAME

Mojolicious::Plugin::Util::Endpoint - Use template URIs in Mojolicious

SYNOPSIS

# Mojolicious
$self->plugin('Util::Endpoint');

# Mojolicious::Lite
plugin 'Util::Endpoint';

my $r = $mojo->routes;

# Set endpoint
$r->route('/:user')->endpoint(
  webfinger => {
    query  => [
      q => '{uri}'
    ]
  });

return $self->endpoint('webfinger');
# https://sojolicio.us/{user}?q={uri}

$self->stash(user => 'Akron');

return $self->endpoint('webfinger');
# https://sojolicio.us/Akron?q={uri}

return $self->endpoint(webfinger => {
  uri => 'acct:akron@sojolicio.us'
});
# https://sojolicio.us/Akron?q=acct%3Aakron%40sojolicio.us

DESCRIPTION

Mojolicious::Plugin::Util::Endpoint is a plugin that allows for the simple establishment of endpoint URIs. This is similar to the url_for method of Mojolicious::Controller, but includes support for template URIs with parameters (as used in, e.g., Host-Meta or OpenSearch).

METHODS

register

# Mojolicious
$app->plugin('Util::Endpoint');

# Mojolicious::Lite
plugin 'Util::Endpoint';

Called when registering the plugin.

SHORTCUTS

endpoint

my $r = $mojo->routes
$r->route('/suggest')->endpoint(opensearch => {
  scheme => 'https',
  host   => 'sojolicio.us',
  port   => 3000,
  query  => [
    q     => '{searchTerms}',
    start => '{startIndex?}'
  ]
});

Establishes an endpoint defined for a service. It accepts optional parameters scheme, host, a port and query parameters (query), overwriting the current values of url_for. Template parameters need curly brackets, optional template parameters need a question mark before the closing bracket. Optional path placeholders are currenty not supported.

HELPERS

endpoint

# In Controller:
return $self->endpoint('webfinger');
return $self->endpoint(webfinger => { user => 'me' } );

Returns the endpoint defined for a specific service. It accepts additional stash values for the route. These stash values override existing stash values from the controller and fill the template variables.

# In Controller:
return $self->endpoint('opensearch');
# https://sojolicio.us/suggest?q={searchTerms}&start={startIndex?}

return $self->endpoint(opensearch => {
  searchTerms => 'simpson',
  '?' => undef
});
# https://sojolicio.us/suggest?q=simpson

The special parameter ? can be set to undef to ignore all undefined optional template parameters.

If the defined endpoint can't be found, the value for url_for is returned.

get_endpoints

# In Controller:
my $hash = $self->get_endpoints;

while (my ($key, $value) = each %$hash) {
  print $key, ' => ', $value, "\n";
};

Returns a hash of all endpoints, interpolated with the current controller stash.

COMMANDS

endpoints

perl app.pl endpoints

Show all endpoints of the app established by this plugin.

DEPENDENCIES

Mojolicious (best with SSL support).

AVAILABILITY

https://github.com/Akron/Mojolicious-Plugin-Util-Endpoint

COPYRIGHT AND LICENSE

Copyright (C) 2011-2012, Nils Diewald.

This program is free software, you can redistribute it and/or modify it under the same terms as Perl.