File Coverage

blib/lib/EntityModel/Web/Request.pm
Criterion Covered Total %
statement 56 56 100.0
branch 12 12 100.0
condition n/a
subroutine 10 10 100.0
pod 1 5 20.0
total 79 83 95.1


line stmt bran cond sub pod time code
1             package EntityModel::Web::Request;
2             {
3             $EntityModel::Web::Request::VERSION = '0.004';
4             }
5             use EntityModel::Class {
6 2         52 post => { type => 'hash' },
7             get => { type => 'hash' },
8             uri => { type => 'URI::URL' },
9             path => { type => 'string' },
10             hostname => { type => 'string' },
11             method => { type => 'string' },
12             content_type => { type => 'string' },
13             no_cache => { type => 'data' },
14             protocol => { type => 'data' },
15             version => { type => 'string' },
16             redirect => { type => 'string' },
17             header => { type => 'array', subclass => 'EntityModel::Web::Header' },
18             header_by_name => { type => 'hash', scope => 'private', watch => { header => 'name' } },
19 2     2   22894 };
  2         67845  
20              
21             =head1 NAME
22              
23             EntityModel::Web::Request - abstraction for incoming HTTP request
24              
25             =head1 VERSION
26              
27             version 0.004
28              
29             =head1 SYNOPSIS
30              
31             use EntityModel::Web::Request;
32             my $req = EntityModel::Web::Request->new(
33             );
34              
35             =head1 DESCRIPTION
36              
37             =cut
38              
39 2     2   263926 use URI;
  2         6  
  2         31  
40 2     2   1820 use URI::QueryParam;
  2         1548  
  2         52  
41 2     2   622226 use HTTP::Date;
  2         65264  
  2         171  
42 2     2   22 use EntityModel::Web::Header;
  2         4  
  2         21  
43              
44             =head1 METHODS
45              
46             =cut
47              
48             sub new {
49 4     4 1 2835 my $class = shift;
50 4         30 my $self = $class->SUPER::new;
51 4         85 my %args = @_;
52 4         24 $self->{uri} = URI::URL->new;
53 4         4688 $self->{uri}->scheme('http');
54 4 100       3652 if(my $uri = delete $args{uri}) {
55 2         19 $self->uri($uri);
56             }
57 4         8 $self->$_(delete $args{$_}) for grep { exists $args{$_} } qw{method path version hostname};
  16         44  
58 4 100       55 if(my $header = delete $args{header}) {
59 3         5 my @hdr;
60 3         6 foreach my $item (@$header) {
61 5         59 push @hdr, EntityModel::Web::Header->new(
62             name => $item->{name},
63             value => $item->{value},
64             );
65             }
66 3         56 $self->header->push(@hdr);
67             }
68 4 100       1075 if(my $host = $self->header_by_name->get('Host')) {
69 1         17 $self->hostname($host->value);
70             }
71 4         92 return $self;
72             }
73              
74             sub path {
75 6     6 0 2992 my $self = shift;
76 6 100       15 if(@_) {
77 3         4 $self->{path} = shift;
78 3         8 $self->uri->path($self->{path});
79 3         114 return $self;
80             }
81 3         15 return $self->{path};
82             }
83              
84             sub hostname {
85 6     6 0 1531 my $self = shift;
86 6 100       15 if(@_) {
87 3         6 $self->{hostname} = shift;
88 3         9 $self->uri->host($self->{hostname});
89 3         238 return $self;
90             }
91 3         14 return $self->{hostname};
92             }
93              
94             sub uri {
95 11     11 0 16 my $self = shift;
96 11 100       19 if(@_) {
97 2         24 my $uri = shift;
98 2         7 $self->update_uri_from($uri);
99 2         3 return $self;
100             }
101 9         44 return $self->{uri};
102             }
103              
104             sub update_uri_from {
105 2     2 0 4 my ($self, $uri) = @_;
106 2         4 $self->{uri} = $uri;
107 2         9 $self->hostname($uri->host);
108 2         6 $self->path($uri->path);
109 2         17 $self->get->set($_, $uri->query_param($_)) for sort $uri->query_param;
110 2         152 return $self;
111             }
112              
113             1;
114              
115             __END__