File Coverage

blib/lib/WebAPI/DBIC/Resource/Role/Root.pm
Criterion Covered Total %
statement 3 30 10.0
branch 0 6 0.0
condition n/a
subroutine 1 6 16.6
pod 0 4 0.0
total 4 46 8.7


line stmt bran cond sub pod time code
1             package WebAPI::DBIC::Resource::Role::Root;
2             $WebAPI::DBIC::Resource::Role::Root::VERSION = '0.003002';
3              
4 2     2   17803413 use Moo::Role;
  2         44635  
  2         10  
5              
6              
7             requires 'encode_json';
8              
9              
10             has content_types_provided => (
11             is => 'lazy',
12             );
13              
14             sub _build_content_types_provided {
15             return [
16 0     0     { 'application/json' => 'to_plain_json' },
17             { 'text/html' => 'to_html' }, # provide redirect to HAL browser
18             ]
19             }
20              
21              
22 0     0 0   sub allowed_methods { return [ qw(GET HEAD) ] }
23              
24              
25             sub to_html {
26 0     0 0   my $self = shift;
27 0           my $env = $self->request->env;
28 0           my $router = $env->{'plack.router'};
29 0           my $path = $env->{REQUEST_URI}; # "/clients/v1/";
30             # XXX this location should not be hard-coded
31 0           $self->response->header(Location => "browser/browser.html#$path");
32 0           return \302;
33             }
34              
35              
36             sub to_plain_json {
37 0     0 0   return $_[0]->encode_json($_[0]->render_root_as_plain());
38             }
39              
40              
41             sub render_root_as_plain { # informal JSON description, XXX liable to change
42 0     0 0   my ($self) = @_;
43              
44 0           my $request = $self->request;
45 0           my $path = $request->env->{REQUEST_URI}; # "/clients/v1/";
46 0           my %links;
47 0           foreach my $route (@{$self->router->routes}) {
  0            
48 0           my @parts;
49              
50 0           for my $c (@{ $route->components }) {
  0            
51 0 0         if ($route->is_component_variable($c)) {
52 0           push @parts, ":".$route->get_component_name($c);
53             } else {
54 0           push @parts, "$c";
55             }
56             }
57 0 0         next unless @parts;
58              
59 0           my $url = $path . join("/", @parts);
60 0 0         die "Duplicate path: $url" if $links{$url};
61 0           my $title = join(" ", (split /::/, $route->defaults->{result_class})[-3,-1]);
62 0           $links{$url} = $title;
63             }
64              
65             return {
66 0           routes => \%links,
67             };
68             }
69              
70             1;
71              
72             __END__
73              
74             =pod
75              
76             =encoding UTF-8
77              
78             =head1 NAME
79              
80             WebAPI::DBIC::Resource::Role::Root
81              
82             =head1 VERSION
83              
84             version 0.003002
85              
86             =head1 DESCRIPTION
87              
88             Handles GET and HEAD requests for requests representing the root resource, e.g. C</>.
89              
90             Supports the C<application/json> content type.
91              
92             =head1 NAME
93              
94             WebAPI::DBIC::Resource::Role::Root - methods to handle requests for the root resource
95              
96             =head1 AUTHOR
97              
98             Tim Bunce <Tim.Bunce@pobox.com>
99              
100             =head1 COPYRIGHT AND LICENSE
101              
102             This software is copyright (c) 2015 by Tim Bunce.
103              
104             This is free software; you can redistribute it and/or modify it under
105             the same terms as the Perl 5 programming language system itself.
106              
107             =cut