File Coverage

blib/lib/Archive/Har/Entry/Cache/Request.pm
Criterion Covered Total %
statement 9 92 9.7
branch 0 42 0.0
condition 0 3 0.0
subroutine 3 11 27.2
pod 5 7 71.4
total 17 155 10.9


line stmt bran cond sub pod time code
1             package Archive::Har::Entry::Cache::Request;
2              
3 1     1   5 use warnings;
  1         2  
  1         28  
4 1     1   5 use strict;
  1         3  
  1         18  
5 1     1   4 use Carp();
  1         2  
  1         1108  
6              
7             our $VERSION = 0.19;
8              
9             sub new {
10 0     0 1   my ( $class, $params ) = @_;
11 0           my $self = {};
12 0           bless $self, $class;
13 0 0         if ( defined $params ) {
14 0 0         if ( defined $params->{expires} ) {
15 0           $self->expires( $params->{expires} );
16             }
17 0           $self->last_access( $params->{lastAccess} );
18 0           $self->etag( $params->{eTag} );
19 0           $self->hit_count( $params->{hitCount} );
20 0 0         if ( defined $params->{comment} ) {
21 0           $self->comment( $params->{comment} );
22             }
23 0           foreach my $key ( sort { $a cmp $b } keys %{$params} ) {
  0            
  0            
24 0 0         if ( $key =~ /^_[[:alnum:]]+$/smx ) { # private fields
25 0           $self->$key( $params->{$key} );
26             }
27             }
28             }
29 0           return $self;
30             }
31              
32             sub expires {
33 0     0 1   my ( $self, $new ) = @_;
34 0           my $old = $self->{expires};
35 0 0         if ( @_ > 1 ) {
36 0           $self->{expires} = $new;
37             }
38 0           return $old;
39             }
40              
41             sub last_access {
42 0     0 1   my ( $self, $new ) = @_;
43 0           my $old = $self->{lastAccess};
44 0 0         if ( @_ > 1 ) {
45 0 0         if ( defined $new ) {
46 0           my $date_regex = qr/\d{4}[-]\d{2}[-]\d{2}/smx;
47 0           my $time_regex = qr/\d{2}:\d{2}:\d{2}[.]\d+/smx;
48 0           my $zone_regex = qr/(?:[+]\d{2}:\d{2}|Z)/smx;
49 0 0         if ( $new =~ /^${date_regex}T${time_regex}${zone_regex}$/smx ) {
50 0           $self->{lastAccess} = $new;
51             }
52             else {
53 0           Carp::croak('last_access is not formatted correctly');
54             }
55             }
56             else {
57 0           $self->{lastAccess} = '0000-00-00T00-00-00';
58             }
59             }
60 0 0 0       if ( ( defined $old ) && ( $old eq '0000-00-00T00-00-00' ) ) {
61 0           return;
62             }
63             else {
64 0           return $old;
65             }
66             }
67              
68             sub etag {
69 0     0 1   my ( $self, $new ) = @_;
70 0           my $old = $self->{eTag};
71 0 0         if ( @_ > 1 ) {
72 0           $self->{eTag} = $new;
73             }
74 0 0         if ( defined $old ) {
75 0           return $old;
76             }
77             else {
78 0           return q[];
79             }
80             }
81              
82             sub hit_count {
83 0     0 0   my ( $self, $new ) = @_;
84 0           my $old = $self->{hitCount};
85 0 0         if ( @_ > 1 ) {
86 0           $self->{hitCount} = $new;
87             }
88 0 0         if ( defined $old ) {
89 0           return $old;
90             }
91             else {
92 0           return 0;
93             }
94             }
95              
96             sub comment {
97 0     0 1   my ( $self, $new ) = @_;
98 0           my $old = $self->{comment};
99 0 0         if ( @_ > 1 ) {
100 0           $self->{comment} = $new;
101             }
102 0           return $old;
103             }
104              
105             sub AUTOLOAD {
106 0     0     my ( $self, $new ) = @_;
107              
108 0           my $name = $Archive::Har::Entry::Cache::Request::AUTOLOAD;
109 0           $name =~ s/.*://smx; # strip fully-qualified portion
110              
111 0           my $old;
112 0 0         if ( $name =~ /^_[[:alnum:]]+$/smx ) { # private fields
113 0           $old = $self->{$name};
114 0 0         if ( @_ > 1 ) {
115 0           $self->{$name} = $new;
116             }
117             }
118             else {
119 0           Carp::croak(
120             "$name is not specified in the HAR 1.2 spec and does not start with an underscore"
121             );
122             }
123 0           return $old;
124             }
125              
126             sub TO_JSON {
127 0     0 0   my ($self) = @_;
128 0           my $json = {};
129 0 0         if ( defined $self->expires() ) {
130 0           $json->{expires} = $self->expires();
131             }
132 0 0         if ( $self->last_access() ) {
133 0           $json->{lastAccess} = $self->last_access();
134             }
135             else {
136 0           $json->{lastAccess} = '0000-00-00T00-00-00';
137             }
138 0           $json->{eTag} = $self->etag();
139 0           $json->{hitCount} = $self->hit_count();
140 0 0         if ( defined $self->comment() ) {
141 0           $json->{comment} = $self->comment();
142             }
143 0           foreach my $key ( sort { $a cmp $b } keys %{$self} ) {
  0            
  0            
144 0 0         next if ( !defined $self->{$key} );
145 0 0         if ( $key =~ /^_[[:alnum:]]+$/smx ) { # private fields
146 0           $json->{$key} = $self->{$key};
147             }
148             }
149 0           return $json;
150             }
151              
152             1;
153             __END__