File Coverage

blib/lib/RapidApp/CoreSchema/ResultSet/Request.pm
Criterion Covered Total %
statement 12 36 33.3
branch 0 22 0.0
condition 0 33 0.0
subroutine 4 8 50.0
pod 0 2 0.0
total 16 101 15.8


line stmt bran cond sub pod time code
1             package RapidApp::CoreSchema::ResultSet::Request;
2 1     1   2713 use base 'DBIx::Class::ResultSet';
  1         2  
  1         118  
3              
4 1     1   7 use strict;
  1         2  
  1         20  
5 1     1   4 use warnings;
  1         2  
  1         30  
6              
7 1     1   5 use RapidApp::Util qw(:all);
  1         2  
  1         923  
8              
9             # Flexible handling (copied from My Clippard)
10             sub record_Request {
11 0     0 0   my $self = shift;
12 0 0         my $ReqData = shift or die "Missing Request Data";
13 0   0       my $extra = shift || {};
14            
15             # Also support full Catalyst Context arg:
16 0 0 0       my $c = (
17             blessed $ReqData &&
18             $ReqData->can('request') &&
19             $ReqData->request->isa('Catalyst::Request')
20             ) ? $ReqData : undef;
21 0 0         $ReqData = $c->request if ($c);
22            
23            
24             my $dt = $extra->{unix_timestamp} ?
25 0 0         DateTime->from_epoch(epoch => $extra->{unix_timestamp}, time_zone => 'local') :
26             DateTime->now( time_zone => 'local' );
27            
28             my $data = {
29             timestamp => $dt->ymd . ' ' . $dt->hms(':'),
30 0   0       serialized_request => $extra->{full_request_yaml} || ''
31             };
32            
33 0 0         if(ref $ReqData eq 'HASH') {
    0          
34             %$data = ( %$data,
35             client_ip => $ReqData->{address} || $ReqData->{client_ip},
36             uri => $ReqData->{uri},
37             method => $ReqData->{method},
38             user_agent => $ReqData->{user_agent},
39             referer => $ReqData->{referer},
40 0   0       );
41             }
42 0     0     elsif(try{$ReqData->isa('Catalyst::Request')}) {
43 0   0       %$data = ( %$data,
      0        
      0        
      0        
      0        
44             client_ip => ($ReqData->address || undef),
45             uri => ($ReqData->uri || undef),
46             method => ($ReqData->method || undef),
47             user_agent => ($ReqData->header('user-agent') || undef),
48             referer => ($ReqData->header('referer') || undef),
49             );
50 0 0 0 0     $data->{user_id} = $c->user->get_column('id') if ($c && try{$c->user});
  0            
51             }
52             else {
53 0           die "Expected Request Data as either a Hash or Catalyst::Request object";
54             }
55            
56 0 0         return $self->create($data) if(defined wantarray);
57            
58             # populate for speed in VOID context:
59 0           $self->populate([$data]);
60 0           return 1;
61             }
62              
63             # Records the request from the catalyst context object ($c)
64             sub record_ctx_Request {
65 0     0 0   my $self = shift;
66 0 0         my $c = shift or return undef;
67            
68 0 0         my $Request = $c->request or return undef;
69            
70 0   0       my $data = {
      0        
      0        
      0        
      0        
71             client_ip => ($Request->address || undef),
72             uri => ($Request->uri || undef),
73             method => ($Request->method || undef),
74             user_agent => ($Request->header('user-agent') || undef),
75             referer => ($Request->header('referer') || undef),
76             timestamp => DateTime->now( time_zone => 'local' )
77             };
78              
79 0 0         return $self->create($data) if(defined wantarray);
80            
81             # populate for speed in VOID context:
82 0           $self->populate([$data]);
83 0           return 1;
84             }
85              
86              
87             1;