File Coverage

lib/HTTP/Promise/Headers/Cookie.pm
Criterion Covered Total %
statement 32 32 100.0
branch 5 8 62.5
condition 2 3 66.6
subroutine 10 10 100.0
pod 3 3 100.0
total 52 56 92.8


line stmt bran cond sub pod time code
1             ##----------------------------------------------------------------------------
2             ## Asynchronous HTTP Request and Promise - ~/lib/HTTP/Promise/Headers/Cookie.pm
3             ## Version v0.1.0
4             ## Copyright(c) 2022 DEGUEST Pte. Ltd.
5             ## Author: Jacques Deguest <jack@deguest.jp>
6             ## Created 2022/05/07
7             ## Modified 2022/05/07
8             ## All rights reserved.
9             ##
10             ##
11             ## This program is free software; you can redistribute it and/or modify it
12             ## under the same terms as Perl itself.
13             ##----------------------------------------------------------------------------
14             package HTTP::Promise::Headers::Cookie;
15             BEGIN
16             {
17 3     3   2021 use strict;
  3         11  
  3         126  
18 3     3   18 use warnings;
  3         6  
  3         81  
19 3     3   19 use warnings::register;
  3         6  
  3         420  
20 3     3   19 use parent qw( HTTP::Promise::Headers::Generic );
  3         7  
  3         14  
21 3     3   263 our $VERSION = 'v0.1.0';
22             };
23              
24 3     3   15 use strict;
  3         10  
  3         66  
25 3     3   14 use warnings;
  3         18  
  3         831  
26              
27             sub init
28             {
29 2     2 1 1774 my $self = shift( @_ );
30 2         177 $self->{cookies} = [];
31 2 50 66     32 @_ = () if( @_ == 1 && $self->_is_a( $_[0] => 'Module::Generic::Null' ) );
32 2 100       46 if( @_ )
33             {
34 1         4 my $this = shift( @_ );
35 1 50       9 my $ref = $self->_is_array( $this ) ? $this : [split( /(?<!\\)\;[[:blank:]\h]*/, $this )];
36 1         30 $self->cookies( $ref );
37             }
38 2         881 $self->{_init_strict_use_sub} = 1;
39 2 50       24 $self->SUPER::init( @_ ) || return( $self->pass_error );
40 2         17 $self->_field_name( 'Cookie' );
41 2         1999 return( $self );
42             }
43              
44 2     2 1 1153 sub as_string { return( shift->cookies->join( '; ' )->scalar ); }
45              
46 5     5 1 2366 sub cookies { return( shift->_set_get_array_as_object( 'cookies', @_ ) ); }
47              
48             1;
49             # NOTE: POD
50             __END__
51              
52             =encoding utf-8
53              
54             =head1 NAME
55              
56             HTTP::Promise::Headers::Cookie - Cookie Header Field
57              
58             =head1 SYNOPSIS
59              
60             use HTTP::Promise::Headers::Cookie;
61             my $c = HTTP::Promise::Headers::Cookie->new( 'name=value; name2=value2; name3=value3' ) ||
62             die( HTTP::Promise::Headers::Cookie->error, "\n" );
63             # or
64             my $c = HTTP::Promise::Headers::Cookie->new( [qw( name=value name2=value2 name3=value3 )] );
65             $c->cookies->push( 'name4=value4' );
66             $c->cookies->remove( 'name2=value2' );
67             say "$c";
68             # This would return: name=value; name3=value3; name4=value4
69              
70             =head1 VERSION
71              
72             v0.1.0
73              
74             =head1 DESCRIPTION
75              
76             L<HTTP::Promise::Headers::Cookie> implements a simple interface to store the cookie name-value pairs sent by the user agent to the server. It uses L</cookies> which returns a L<Module::Generic::Array> to manage and contain all the cookie name-value pairs. It is up to you to provide the right cookie string.
77              
78             You can and maybe should use L<Cookie::Jar> to help you manage a cookie jar and format cookies.
79              
80             =head1 CONSTRUCTOR
81              
82             =head2 new
83              
84             Provided with an optional string or array reference and this will instantiate a new object and return it.
85              
86             If a string is provided, it is expected to be properly formatted, such as:
87              
88             name=value; name2=value2; name3=value3
89              
90             It will be properly split and added to L</cookies>. If an array is provided, it will be directly added to L</cookies>.
91              
92             =head1 METHODS
93              
94             =head2 as_string
95              
96             Returns a string representation for this C<Cookie> header field.
97              
98             =head2 cookies
99              
100             Returns an L<array object|Module::Generic::Array> containing all the cookies.
101              
102             =head1 AUTHOR
103              
104             Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
105              
106             =head1 SEE ALSO
107              
108             L<Cookie>, L<Cookie::Jar>, L<rfc6265, section 5.4|https://tools.ietf.org/html/rfc6265#section-5.4> and L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cookie>
109              
110             L<HTTP::Promise>, L<HTTP::Promise::Request>, L<HTTP::Promise::Response>, L<HTTP::Promise::Message>, L<HTTP::Promise::Entity>, L<HTTP::Promise::Headers>, L<HTTP::Promise::Body>, L<HTTP::Promise::Body::Form>, L<HTTP::Promise::Body::Form::Data>, L<HTTP::Promise::Body::Form::Field>, L<HTTP::Promise::Status>, L<HTTP::Promise::MIME>, L<HTTP::Promise::Parser>, L<HTTP::Promise::IO>, L<HTTP::Promise::Stream>, L<HTTP::Promise::Exception>
111              
112             =head1 COPYRIGHT & LICENSE
113              
114             Copyright(c) 2022 DEGUEST Pte. Ltd.
115              
116             All rights reserved.
117              
118             This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
119              
120             =cut