CookieMonster.pm

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