Mojolicious::Plugin::Util::Endpoint - Use Template URIs in Mojolicious
# Mojolicious::Lite plugin 'Util::Endpoint'; # Mojolicious $self->plugin('Util::Endpoint'); my $rs = $mojo->routes; # Set endpoint my $r = $rs->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
Mojolicious::Plugin::Util::Endpoint is a plugin that allows for the simple establishment of endpoint URIs. This is similar to url_for, but includes support for template URIs with parameters following RFC6570 Level 1 (as used in, e.g., Host-Meta or OpenSearch).
# Mojolicious $app->plugin('Util::Endpoint'); # Mojolicious::Lite plugin 'Util::Endpoint';
Called when registering the plugin.
my $rs = $mojo->routes my $r = $rs->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. This also defines a name attribute to the route for using with url_for. Returns the route.
scheme
host
port
query
url_for
# In Controller: # Set endpoints: $self->endpoint(hub => 'http://sojolicio.us/search?q={searchTerm}'); $self->endpoint(hub => Mojo::URL->new('http://pubsubhubbub.appspot.com/')); # Get endpoints: return $self->endpoint('webfinger'); return $self->endpoint(webfinger => { user => 'me' } ); # Interpolate arbitrary template URIs return $self->endpoint( 'http://sojolicio.us/.well-known/webfinger?resource={uri}&rel={rel?}' => { 'uri' => 'acct:akron@sojolicio.us', '?' => undef }); # In Template: <%= endpoint 'webfinger' %>
Get or set endpoints defined for a specific service.
For setting it accepts the name of the endpoint and either a string with the endpoint URI or a Mojo::URL object.
For getting it accepts the name of the endpoint or an arbitrary template URI and additional stash values for the route as a hash reference. 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.
?
undef
If the defined endpoint can't be found, the value for url_for is returned.
# 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.
Note: This helper is EXPERIMENTAL and may be deprecated in further releases.
$ perl app.pl endpoints
Show all endpoints of the app established by this plugin.
Mojolicious (best with SSL support).
https://github.com/Akron/Mojolicious-Plugin-Util-Endpoint
Copyright (C) 2011-2014, Nils Diewald.
This program is free software, you can redistribute it and/or modify it under the same terms as Perl.
To install Mojolicious::Plugin::Util::Endpoint, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Mojolicious::Plugin::Util::Endpoint
CPAN shell
perl -MCPAN -e shell install Mojolicious::Plugin::Util::Endpoint
For more information on module installation, please visit the detailed CPAN module installation guide.