File Coverage

blib/lib/WebAPI/DBIC/Resource/HAL/Role/Root.pm
Criterion Covered Total %
statement 6 27 22.2
branch 0 4 0.0
condition n/a
subroutine 2 4 50.0
pod 0 2 0.0
total 8 37 21.6


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.003002';
3              
4 2     2   23104802 use Moo::Role;
  2         43840  
  2         11  
5              
6 2     2   1438 use JSON::MaybeXS qw(JSON);
  2         1103  
  2         819  
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 = join(" ", (split /::/, $route->defaults->{result_class})[-3,-1]);
49              
50 0           my $url = $path . join("", @parts);
51 0           $links{join("", @parts)} = {
52             href => $url,
53             title => $title,
54             %attr
55             };
56             }
57              
58 0           return { _links => \%links, };
59             }
60              
61              
62             1;
63              
64             __END__
65              
66             =pod
67              
68             =encoding UTF-8
69              
70             =head1 NAME
71              
72             WebAPI::DBIC::Resource::HAL::Role::Root
73              
74             =head1 VERSION
75              
76             version 0.003002
77              
78             =head1 DESCRIPTION
79              
80             =head1 NAME
81              
82             WebAPI::DBIC::Resource::HAL::Role::Root - provide a description of the API for HAL browser
83              
84             =head1 AUTHOR
85              
86             Tim Bunce <Tim.Bunce@pobox.com>
87              
88             =head1 COPYRIGHT AND LICENSE
89              
90             This software is copyright (c) 2015 by Tim Bunce.
91              
92             This is free software; you can redistribute it and/or modify it under
93             the same terms as the Perl 5 programming language system itself.
94              
95             =cut