File Coverage

blib/lib/WebAPI/DBIC/Resource/HAL/Role/Root.pm
Criterion Covered Total %
statement 6 30 20.0
branch 0 6 0.0
condition n/a
subroutine 2 4 50.0
pod 0 2 0.0
total 8 42 19.0


line stmt bran cond sub pod time code
1             package WebAPI::DBIC::Resource::HAL::Role::Root;
2             $WebAPI::DBIC::Resource::HAL::Role::Root::VERSION = '0.004001';
3              
4 2     2   35098469 use Moo::Role;
  2         42376  
  2         10  
5              
6 2     2   1583 use JSON::MaybeXS qw(JSON);
  2         1144  
  2         767  
7              
8             requires '_build_content_types_provided';
9             requires 'encode_json';
10              
11              
12             around '_build_content_types_provided' => sub {
13             my $orig = shift;
14             my $self = shift;
15             my $types = $self->$orig();
16             unshift @$types, { 'application/hal+json' => 'to_json_as_hal' };
17             return $types;
18             };
19              
20              
21 0     0 0   sub to_json_as_hal { return $_[0]->encode_json($_[0]->render_api_as_hal()) }
22              
23              
24             sub render_api_as_hal {
25 0     0 0   my $self = shift;
26              
27 0           my $request = $self->request;
28 0           my $router = $self->router;
29 0           my $path = $request->env->{REQUEST_URI}; # "/clients/v1/";
30              
31             # we get here when the HAL Browser requests the root JSON
32 0           my %links = (self => { href => $path } );
33 0           foreach my $route (@{$router->routes}) {
  0            
34 0           my @parts;
35             my %attr;
36              
37 0           for my $c (@{ $route->components }) {
  0            
38 0 0         if ($route->is_component_variable($c)) {
39 0           my $name = $route->get_component_name($c);
40 0           push @parts, "{/$name}";
41 0           $attr{templated} = JSON->true;
42             } else {
43 0           push @parts, "$c";
44             }
45             }
46 0 0         next unless @parts;
47              
48 0           my $title;
49 0 0         if (exists $route->defaults->{result_class}) {
50 0           $title = join(" ", (split /::/, $route->defaults->{result_class})[-3,-1]);
51             } else {
52 0           ($title) = split( /\?/, $route->path);
53             }
54              
55 0           my $url = $path . join("", @parts);
56 0           $links{join("", @parts)} = {
57             href => $url,
58             title => $title,
59             %attr
60             };
61             }
62              
63 0           return { _links => \%links, };
64             }
65              
66              
67             1;
68              
69             __END__
70              
71             =pod
72              
73             =encoding UTF-8
74              
75             =head1 NAME
76              
77             WebAPI::DBIC::Resource::HAL::Role::Root
78              
79             =head1 VERSION
80              
81             version 0.004001
82              
83             =head1 DESCRIPTION
84              
85             =head1 NAME
86              
87             WebAPI::DBIC::Resource::HAL::Role::Root - provide a description of the API for HAL browser
88              
89             =head1 AUTHOR
90              
91             Tim Bunce <Tim.Bunce@pobox.com>
92              
93             =head1 COPYRIGHT AND LICENSE
94              
95             This software is copyright (c) 2015 by Tim Bunce.
96              
97             This is free software; you can redistribute it and/or modify it under
98             the same terms as the Perl 5 programming language system itself.
99              
100             =cut