-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathRoot.pm
More file actions
74 lines (53 loc) · 1.73 KB
/
Root.pm
File metadata and controls
74 lines (53 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package WebAPI::DBIC::Resource::HAL::Role::Root;
=head1 NAME
WebAPI::DBIC::Resource::HAL::Role::Root - provide a description of the API for HAL browser
=head1 DESCRIPTION
=cut
use Moo::Role;
use JSON::MaybeXS qw(JSON);
requires '_build_content_types_provided';
requires 'encode_json';
around '_build_content_types_provided' => sub {
my $orig = shift;
my $self = shift;
my $types = $self->$orig();
unshift @$types, { 'application/hal+json' => 'to_json_as_hal' };
return $types;
};
sub to_json_as_hal { return $_[0]->encode_json($_[0]->render_api_as_hal()) }
sub render_api_as_hal {
my $self = shift;
my $request = $self->request;
my $router = $self->router;
my $path = $request->env->{REQUEST_URI}; # "/clients/v1/";
# we get here when the HAL Browser requests the root JSON
my %links = (self => { href => $path } );
foreach my $route (@{$router->routes}) {
my @parts;
my %attr;
for my $c (@{ $route->components }) {
if ($route->is_component_variable($c)) {
my $name = $route->get_component_name($c);
push @parts, "{/$name}";
$attr{templated} = JSON->true;
} else {
push @parts, "$c";
}
}
next unless @parts;
my $title;
if( exists $route->defaults->{result_class}) {
$title = join(" ", (split /::/, $route->defaults->{result_class})[-3,-1]);
} else {
($title) = split( /\?/, $route->path);
}
my $url = $path . join("", @parts);
$links{join("", @parts)} = {
href => $url,
title => $title,
%attr
};
}
return { _links => \%links, };
}
1;