| 1 | package WebAPI::DBIC::Resource::Role::Root; | ||||||||||
| 2 | 25 | $WebAPI::DBIC::Resource::Role::Root::VERSION = '0.004002'; | |||||||||
| 3 | |||||||||||
| 4 | 25 | ▶ | use Moo::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | |||||||||||
| 7 | 25 | requires 'encode_json'; | |||||||||
| 8 | |||||||||||
| 9 | |||||||||||
| 10 | 25 | has content_types_provided => ( | |||||||||
| 11 | is => 'lazy', | ||||||||||
| 12 | ); | ||||||||||
| 13 | |||||||||||
| 14 | 4 | ▶ | sub _build_content_types_provided { | ||||||||
|
Subroutine
_build_content_types_provided: called CC=1 SCAR=0.0
| |||||||||||
| 15 | return [ | ||||||||||
| 16 | 4 | { 'application/vnd.wapid+json' => 'to_plain_json' }, | |||||||||
| 17 | { 'text/html' => 'to_html' }, # provide redirect to HAL browser | ||||||||||
| 18 | ] | ||||||||||
| 19 | } | ||||||||||
| 20 | |||||||||||
| 21 | |||||||||||
| 22 | 4 | ▶ | sub allowed_methods { return [ qw(GET HEAD) ] } | ||||||||
|
Subroutine
allowed_methods: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 23 | |||||||||||
| 24 | |||||||||||
| 25 | 2 | ▶ | sub to_html { | ||||||||
|
Subroutine
to_html: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 26 | 2 | my $self = shift; | |||||||||
| 27 | 2 | my $env = $self->request->env; | |||||||||
| 28 | 2 | my $router = $env->{'plack.router'}; | |||||||||
| 29 | 2 | my $path = $env->{REQUEST_URI}; # "/clients/v1/"; | |||||||||
| 30 | # XXX this location should not be hard-coded | ||||||||||
| 31 | 2 | $self->response->header(Location => "browser/browser.html#$path"); | |||||||||
| 32 | 2 | return \302; | |||||||||
| 33 | } | ||||||||||
| 34 | |||||||||||
| 35 | |||||||||||
| 36 | 1 | ▶ | sub to_plain_json { | ||||||||
|
Subroutine
to_plain_json: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 37 | 1 | return $_[0]->encode_json($_[0]->render_root_as_plain()); | |||||||||
| 38 | } | ||||||||||
| 39 | |||||||||||
| 40 | |||||||||||
| 41 | 1 | ▶ | sub render_root_as_plain { # informal JSON description, XXX liable to change | ||||||||
|
Subroutine
render_root_as_plain: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||
| 42 | 1 | my ($self) = @_; | |||||||||
| 43 | |||||||||||
| 44 | 1 | my $request = $self->request; | |||||||||
| 45 | 1 | my $path = $request->env->{REQUEST_URI}; # "/clients/v1/"; | |||||||||
| 46 | 1 | my %links; | |||||||||
| 47 | 1 | foreach my $route (@{$self->router->routes}) { | |||||||||
| 48 | 23 | my @parts; | |||||||||
| 49 | |||||||||||
| 50 | 23 | for my $c (@{ $route->components }) { | |||||||||
| 51 | 39 | ▶ | if ($route->is_component_variable($c)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 52 | 15 | push @parts, ":".$route->get_component_name($c); | |||||||||
| 53 | } else { | ||||||||||
| 54 | 24 | push @parts, "$c"; | |||||||||
| 55 | } | ||||||||||
| 56 | } | ||||||||||
| 57 | 23 | ▶ | next unless @parts; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 58 | |||||||||||
| 59 | 22 | my $url = $path . join("/", @parts); | |||||||||
| 60 | 22 | ▶ | die "Duplicate path: $url" if $links{$url}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | 22 | my $title = join(" ", (split /::/, $route->defaults->{result_class})[-3,-1]); | |||||||||
| 62 | 22 | $links{$url} = $title; | |||||||||
| 63 | } | ||||||||||
| 64 | |||||||||||
| 65 | return { | ||||||||||
| 66 | 1 | routes => \%links, | |||||||||
| 67 | }; | ||||||||||
| 68 | } | ||||||||||
| 69 | |||||||||||
| 70 | 25 | 1; | |||||||||
| 71 | |||||||||||
| 72 | __END__ | ||||||||||