File Coverage

blib/lib/WebAPI/DBIC/Resource/Role/DBIC.pm
Criterion Covered Total %
statement 12 48 25.0
branch 0 14 0.0
condition n/a
subroutine 4 9 44.4
pod 0 5 0.0
total 16 76 21.0


line stmt bran cond sub pod time code
1             package WebAPI::DBIC::Resource::Role::DBIC;
2             $WebAPI::DBIC::Resource::Role::DBIC::VERSION = '0.003002';
3              
4 2     2   18633248 use Carp qw(croak confess);
  2         17  
  2         304  
5 2     2   1174 use Devel::Dwarn;
  2         21424  
  2         12  
6 2     2   1233 use JSON::MaybeXS qw(JSON);
  2         1591  
  2         98  
7              
8 2     2   882 use Moo::Role;
  2         43511  
  2         11  
9              
10              
11             requires 'uri_for';
12             requires 'throwable';
13             requires 'request';
14             requires 'response';
15             requires 'get_url_for_item_relationship';
16             requires 'id_kvs_for_item';
17              
18              
19             has set => (
20             is => 'rw',
21             required => 1,
22             );
23              
24              
25             has prefetch => (
26             is => 'rw',
27             default => sub { [] },
28             );
29              
30              
31             # XXX perhaps shouldn't be a role, just functions, or perhaps a separate rendering object
32             # default render for DBIx::Class item
33             # https://metacpan.org/module/DBIx::Class::Manual::ResultClass
34             # https://metacpan.org/module/DBIx::Class::InflateColumn
35             sub render_item_as_plain_hash {
36 0     0 0   my ($self, $item) = @_;
37 0           my $data = { $item->get_columns }; # XXX ?
38             # DateTimes
39 0           return $data;
40             }
41              
42              
43             sub path_for_item {
44 0     0 0   my ($self, $item) = @_;
45              
46 0           my $result_source = $item->result_source;
47              
48 0           my @id_kvs = $self->id_kvs_for_item($item);
49              
50 0 0         my $url = $self->uri_for( @id_kvs, result_class => $result_source->result_class)
51             or confess sprintf("panic: no route found to result_class %s (%s)",
52             $result_source->result_class, join(", ", @id_kvs)
53             );
54              
55 0           return $url;
56             }
57              
58              
59              
60             # used for recursive rendering
61             sub web_machine_resource {
62 0     0 0   my ($self, %resource_args) = @_;
63              
64             # XXX shouldn't hard-code GenericItem here (should use router?)
65 0 0         my $resource_class = ($resource_args{item})
66             ? 'WebAPI::DBIC::Resource::GenericItem'
67             : 'WebAPI::DBIC::Resource::GenericSet';
68              
69 0           my $resource = $resource_class->new(
70             request => $self->request,
71             response => $self->request->new_response,
72             throwable => $self->throwable,
73             prefetch => [], # don't propagate prefetch by default
74             set => undef,
75             # XXX others? which and why? generalize
76             %resource_args
77             );
78              
79 0           return $resource;
80             }
81              
82              
83             sub render_item_into_body {
84 0     0 0   my ($self, %resource_args) = @_;
85              
86 0           my $item_resource = $self;
87             # if an item has been specified then we assume that it's not $self->item
88             # and probably relates to a different resource, so we create one for it
89             # that doesn't have the request params set, eg prefetch
90 0 0         if ($resource_args{item}) {
91 0           $item_resource = $self->web_machine_resource( %resource_args );
92             }
93              
94             # XXX temporary hack
95 0           my $body;
96 0 0         if ($self->request->headers->header('Accept') =~ /hal\+json/) {
97 0           $body = $item_resource->to_json_as_hal;
98             }
99             else {
100 0           $body = $item_resource->to_json_as_plain;
101             }
102              
103 0           $self->response->body($body);
104              
105 0           return;
106             }
107              
108              
109              
110             sub add_params_to_url { # XXX this is all a bit suspect
111 0     0 0   my ($self, $base, $passthru_params, $override_params) = @_;
112 0 0         $base || croak "no base";
113              
114 0           my $req_params = $self->request->query_parameters;
115 0           my @params = (%$override_params);
116              
117             # XXX turns 'foo~json' into 'foo', and 'me.bar' into 'me'.
118 0           my %override_param_basenames = map { (split(/\W/,$_,2))[0] => 1 } keys %$override_params;
  0            
119              
120             # TODO this logic should live elsewhere
121 0           for my $param (sort keys %$req_params) {
122              
123             # ignore request params that we have an override for
124 0           my $param_basename = (split(/\W/,$param,2))[0];
125 0 0         next if defined $override_param_basenames{$param_basename};
126              
127 0 0         next unless $passthru_params->{$param_basename};
128              
129 0           push @params, $param => $req_params->get($param);
130             }
131              
132 0           my $uri = URI->new($base);
133 0           $uri->query_form(@params);
134              
135 0           return $uri;
136             }
137              
138              
139             1;
140              
141             __END__
142              
143             =pod
144              
145             =encoding UTF-8
146              
147             =head1 NAME
148              
149             WebAPI::DBIC::Resource::Role::DBIC
150              
151             =head1 VERSION
152              
153             version 0.003002
154              
155             =head1 NAME
156              
157             WebAPI::DBIC::Resource::Role::DBIC - a role with core methods for DBIx::Class resources
158              
159             =head1 AUTHOR
160              
161             Tim Bunce <Tim.Bunce@pobox.com>
162              
163             =head1 COPYRIGHT AND LICENSE
164              
165             This software is copyright (c) 2015 by Tim Bunce.
166              
167             This is free software; you can redistribute it and/or modify it under
168             the same terms as the Perl 5 programming language system itself.
169              
170             =cut