File Coverage

blib/lib/WebAPI/DBIC/Route.pm
Criterion Covered Total %
statement 9 46 19.5
branch 0 10 0.0
condition 0 5 0.0
subroutine 3 7 42.8
pod 0 2 0.0
total 12 70 17.1


line stmt bran cond sub pod time code
1             package WebAPI::DBIC::Route;
2             $WebAPI::DBIC::Route::VERSION = '0.003002';
3              
4 4     4   3515123 use Moo;
  4         35739  
  4         27  
5 4     4   3919 use MooX::StrictConstructor;
  4         23686  
  4         57  
6              
7 4     4   38741 use Module::Runtime qw(use_module);
  4         8  
  4         22  
8              
9              
10             has path => (
11             is => 'ro',
12             required => 1,
13             );
14              
15             has resource_class => (
16             is => 'ro',
17             required => 1,
18             );
19              
20             has resource_args => (
21             is => 'ro',
22             required => 1,
23             );
24              
25             has route_defaults => (
26             is => 'ro',
27             default => sub { {} },
28             );
29              
30             has validations => (
31             is => 'ro',
32             default => sub { {} },
33             );
34              
35              
36             sub BUILD {
37 0     0 0   my $self = shift;
38              
39 0           my $resource_class = $self->resource_class;
40 0           my $route_defaults = $self->route_defaults;
41              
42 0 0         if ($ENV{WEBAPI_DBIC_DEBUG}) {
43 0           (my $class = $resource_class) =~ s/^WebAPI::DBIC::Resource//;
44 0           warn sprintf "/%s => %s (%s)\n",
45             $self->path, $class,
46 0           join(' ', map { "$_=$route_defaults->{$_}" } keys %$route_defaults);
47             }
48              
49 0           use_module $resource_class;
50              
51 0 0         if (my $set = $self->resource_args->{set}) {
52              
53             # we use the 'result_class' key in the route_defaults to lookup the route
54             # for a given result_class
55 0   0       $route_defaults->{result_class} = $set && $set->result_class;
56             }
57             else {
58 0 0         warn sprintf "/%s resource_class %s has 'set' method but resource_args does not include 'set'",
59             $self->path, $resource_class
60             if $resource_class->can('set');
61             }
62              
63 0           return;
64             }
65              
66              
67             sub as_add_route_args {
68 0     0 0   my $self = shift;
69              
70 0           my $resource_class = $self->resource_class;
71              
72             # introspect path to extract path param :names
73 0           my $prr = Path::Router::Route->new(path => $self->path);
74 0           my $path_var_names = [
75 0           map { $prr->get_component_name($_) }
76 0           grep { $prr->is_component_variable($_) }
77 0           @{ $prr->components }
78             ];
79              
80             # this logic ought to move into the resource_class
81             my $resource_args_from_route = sub {
82             # XXX we could try to generate more efficient code here
83 0     0     my $req = shift;
84 0           my $args = shift;
85 0           for (@$path_var_names) { #in path param name order
86 0 0         if (m/^[0-9]+$/) { # an id field
87 0           $args->{id}[$_-1] = shift @_;
88             }
89             else {
90 0           $args->{$_} = shift @_;
91             }
92             }
93 0           };
94              
95              
96             # this sub acts as the interface between the router and
97             # the Web::Machine instance handling the resource for that url path
98             my $target = sub {
99 0     0     my $request = shift; # URL args from router remain in @_
100              
101 0           my %resource_args_from_params;
102             # perform any required setup for this request & params in @_
103 0           $resource_args_from_route->($request, \%resource_args_from_params, @_);
104              
105 0           warn sprintf "/%s: running %s machine (extra args: %s; default args: %s)\n",
106             $self->path, $resource_class,
107             join(",", sort keys %resource_args_from_params),
108 0 0         join(",", sort keys %{$self->resource_args})
109             if $ENV{WEBAPI_DBIC_DEBUG};
110              
111 0           my $app = Web::Machine->new(
112             resource => $resource_class,
113 0           resource_args => [ %{$self->resource_args}, %resource_args_from_params ],
114             tracing => $ENV{WEBAPI_DBIC_DEBUG},
115             )->to_app;
116              
117             #local $SIG{__DIE__} = \&Carp::confess;
118             #Dwarn
119 0           my $resp = $app->($request->env);
120              
121 0           return $resp;
122 0           };
123              
124             return (
125 0   0       path => $self->path,
126             validations => $self->validations || {},
127             defaults => $self->route_defaults,
128             target => $target,
129             );
130             }
131              
132              
133             1;
134              
135             __END__
136              
137             =pod
138              
139             =encoding UTF-8
140              
141             =head1 NAME
142              
143             WebAPI::DBIC::Route
144              
145             =head1 VERSION
146              
147             version 0.003002
148              
149             =head1 DESCRIPTION
150              
151             =head1 NAME
152              
153             WebAPI::DBIC::Route - A URL path to a WebAPI::DBIC Resource
154              
155             =head1 AUTHOR
156              
157             Tim Bunce <Tim.Bunce@pobox.com>
158              
159             =head1 COPYRIGHT AND LICENSE
160              
161             This software is copyright (c) 2015 by Tim Bunce.
162              
163             This is free software; you can redistribute it and/or modify it under
164             the same terms as the Perl 5 programming language system itself.
165              
166             =cut