blib/lib/HTTP/CookieMonster.pm

filter: Statementstmt 97.4% 75 / 77 Branchbran 75.0% 18 / 24 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 19 / 19 Podpod 83.3% 5 / 6 totaltotal 92.8% 117 / 126 CC 14 SCAR 9.1
CC
14
Cov
92%
get_cookie
5.0
cookies
4.0
set_cookie
3.3
CRAP 2.5
« Build ↑ Summary blib/lib/HTTP/CookieMonster/Cookie.pm »
1 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package HTTP::CookieMonster;
5 2 $HTTP::CookieMonster::VERSION = '0.09';
6 2 $HTTP::CookieMonster::VERSION = '0.09';
7  
8 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use HTTP::Cookies;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use HTTP::CookieMonster::Cookie;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Safe::Isa;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Scalar::Util qw( reftype );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use Sub::Exporter -setup => { exports => ['cookies'] };
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use URI::Escape qw( uri_escape uri_unescape );
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 2 my @_cookies = ();
20 has 'cookie_jar' => (
21 required => 1,
22 is => 'ro',
23 isa => sub {
24 8 ▶ croak 'HTTP::Cookies object expected'
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
if not $_[0]->$HTTP::CookieMonster::_isa("HTTP::Cookies") 0 8 8
25 if !$_[0]->$_isa( 'HTTP::Cookies' );
26 }
27  
28 2 );
29  
30 sub BUILDARGS {
31 8 ▶ my ( $class, @args ) = @_;
Subroutine BUILDARGS: called CC=2 SCAR=6.9
Pod undocumented
32  
33 8 ▶ return { cookie_jar => shift @args } if @args == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @args == 1 7 1 8
34 1 return {@args};
35 }
36  
37 # all_cookies() is now a straight method rather than a Moo accessor in order to
38 # prevent the all_cookies list from getting out of sync with changes to the
39 # cookie_jar which happen outside of this module. Rather than trying to detect
40 # changes, we'll just create a fresh list each time. Performance penalties
41 # should be minimal and this keeps things simple.
42  
43 sub all_cookies {
44 13 ▶ my $self = shift;
Subroutine all_cookies: called CC=2 SCAR=6.9
Pod documented
45 13 @_cookies = ();
46 13 $self->cookie_jar->scan( \&_check_cookies );
47  
48 13 ▶ wantarray ? return @_cookies : return \@_cookies;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 9 4 13
49 }
50  
51 # my $cookie = cookies( $jar ); -- first cookie (makes no sense)
52 # my $session = cookies( $jar, 'session' );
53 # my @cookies = cookies( $jar );
54 # my @sessions = cookies( $jar, 'session' );
55  
56 sub cookies {
57 5 ▶ my ( $cookie_jar, $name ) = @_;
Subroutine cookies: called CC=4 SCAR=13.9
Pod documented
58 5 ▶ croak 'This function is not part of the OO interface'
Branch: 1 branch
BranchTrueFalseTotal
if $cookie_jar->$HTTP::CookieMonster::_isa("HTTP::CookieMonster") 0 5 5
59 if $cookie_jar->$_isa( 'HTTP::CookieMonster' );
60  
61 5 my $monster = HTTP::CookieMonster->new( $cookie_jar );
62  
63 5 ▶ if ( !$name ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($name) 2 3 5
64 2 ▶ if ( !wantarray ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (wantarray) 1 1 2
65 1 croak
66 'Please specify a cookie name when asking for a single cookie';
67 }
68 1 return @{ $monster->all_cookies };
69 }
70  
71 3 return $monster->get_cookie( $name );
72 }
73  
74 sub get_cookie {
75 8 ▶ my $self = shift;
Subroutine get_cookie: called CC=5 SCAR=16.1
Pod documented
76 8 my $name = shift;
77  
78 8 my @cookies = ();
79 8 foreach my $cookie ( $self->all_cookies ) {
80 16 ▶ if ( $cookie->key eq $name ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cookie->key eq $name) 10 6 16
81 10 ▶ return $cookie if !wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 4 6 10
82 6 push @cookies, $cookie;
83 }
84 }
85  
86 4 ▶ return shift @cookies if !wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 4 4
87 4 return @cookies;
88 }
89  
90 sub set_cookie {
91 5 ▶ my $self = shift;
Subroutine set_cookie: called CC=3 SCAR=12.0
Pod documented
92 5 my $cookie = shift;
93  
94 5 ▶ if ( !$cookie->$_isa( 'HTTP::CookieMonster::Cookie' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cookie->$HTTP::CookieMonster::_isa("HTTP::CookieMonster::Cookie")) 0 5 5
95 0 croak "$cookie is not a HTTP::CookieMonster::Cookie object";
96 }
97  
98 5 ▶ return $self->cookie_jar->set_cookie(
Branch: 1 branch
BranchTrueFalseTotal
$self->cookie_jar->set_cookie($cookie->version, $cookie->key, uri_escape($cookie->val), $cookie->path, $cookie->domain, $cookie->port, $cookie->path_spec, $cookie->secure, $cookie->expires, $cookie->discard, $cookie->hash) ? : 5 0 5
99 $cookie->version, $cookie->key,
100 uri_escape( $cookie->val ), $cookie->path,
101 $cookie->domain, $cookie->port,
102 $cookie->path_spec, $cookie->secure,
103 $cookie->expires, $cookie->discard,
104 $cookie->hash
105 ) ? 1 : 0;
106 }
107  
108 sub delete_cookie {
109 1 ▶ my $self = shift;
Subroutine delete_cookie: called CC=2 SCAR=7.2
Pod documented
110 1 my $cookie = shift;
111  
112 1 ▶ if ( !$cookie->$_isa( 'HTTP::CookieMonster::Cookie' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cookie->$HTTP::CookieMonster::_isa("HTTP::CookieMonster::Cookie")) 0 1 1
113 0 croak "$cookie is not a HTTP::CookieMonster::Cookie object";
114 }
115  
116 1 $cookie->expires( -1 );
117  
118 1 return $self->set_cookie( $cookie );
119 }
120  
121 sub _check_cookies {
122 32 ▶ my @args = @_;
Subroutine _check_cookies: called CC=1 SCAR=0.0
123  
124 32 push @_cookies,
125 HTTP::CookieMonster::Cookie->new(
126 version => $args[0],
127 key => $args[1],
128 val => uri_unescape( $args[2] ),
129 path => $args[3],
130 domain => $args[4],
131 port => $args[5],
132 path_spec => $args[6],
133 secure => $args[7],
134 expires => $args[8],
135 discard => $args[9],
136 hash => $args[10],
137 );
138  
139 32 return;
140 }
141  
142 2 1;
143  
144 # ABSTRACT: Easy read/write access to your jar of HTTP::Cookies
145 #
146  
147 __END__
« Build ↑ Summary blib/lib/HTTP/CookieMonster/Cookie.pm »