blib/lib/WWW/Connpass/Agent.pm

filter: Statementstmt 45.1% 23 / 51 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 53.8% 7 / 13 Podpod 50.0% 2 / 4 totaltotal 38.1% 32 / 84 SCAR 29.9
CC
7
Cov
36%
request
12.0
__ANON__
12.0
new
6.0
CRAP 19.9
« blib/lib/WWW/Connpass/Event/Question.pm ↑ Summary blib/lib/WWW/Connpass/Event.pm »
1 package WWW::Connpass::Agent;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use parent qw/WWW::Mechanize/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Time::HiRes qw/gettimeofday tv_interval/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use JSON 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use constant DEBUG => $ENV{WWW_CONNPASS_DEBUG};
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 my $_JSON = JSON->new->utf8;
14  
15 sub new {
16 0 ▶ my ($class, %args) = @_;
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
17 0 ▶ my $interval = delete $args{interval} || 1.0;
Condition0%0/2
delete $args{'interval'} || 1.0
l!l
00
MC/DC0%0/1
delete $args{'interval'} || 1.0
delete $args{'interval'}
Truth table0%0/2
A
A= delete $args{'interval'}
Aresult
11
00
18 0 my $self = $class->SUPER::new(%args);
19 0 $self->{_interval} = $interval;
20 0 $self->{_last_req_at} = undef;
21 0 return $self;
22 }
23  
24 sub request {
25 0 ▶ my $self = shift;
Subroutine request: not called CC=3 SCAR=24.8
Pod documented
26 0 ▶ if (my $last_req_at = $self->{_last_req_at}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $last_req_at = $self->{"_last_req_at"}) 0 0 0
27 0 my $sec = tv_interval($last_req_at);
28 0 ▶ Time::HiRes::sleep $self->{_interval} - $sec if $sec < $self->{_interval};
Branch: 1 branch
BranchTrueFalseTotal
if $sec < $self->{"_interval"} 0 0 0
29 }
30 0 my $res = $self->SUPER::request(@_);
31 0 if (DEBUG) {
32 my $req = $res->request;
33 warn "============== DEBUG ==============";
34 warn $req->as_string;
35 warn $res->as_string;
36 warn "============== END ==============";
37 }
38 0 $self->{_last_req_at} = [gettimeofday];
39 0 return $res;
40 }
41  
42 sub extract_cookie {
43 0 ▶ my ($self, $expected_key) = @_;
Subroutine extract_cookie: not called CC=1 SCAR=6.9
Pod undocumented
44  
45 0 my $result;
46 $self->cookie_jar->scan(sub {
47 0 ▶ my ($key, $val) = @_[1..2];
Subroutine __ANON__: not called CC=3 SCAR=24.8
48 0 ▶ return if defined $result;
Branch: 1 branch
BranchTrueFalseTotal
if defined $result 0 0 0
49 0 ▶ return if $key ne $expected_key;
Branch: 1 branch
BranchTrueFalseTotal
if $key ne $expected_key 0 0 0
50 0 $result = $val;
51 0 });
52  
53 0 return $result;
54 }
55  
56 sub _csrf_token {
57 0 ▶ my $self = shift;
Subroutine _csrf_token: not called CC=2 SCAR=17.9
58 0 ▶ $self->{_csrf_token} ||= $self->extract_cookie('connpass-csrftoken');
Condition0%0/3
$self->{"_csrf_token"} ||= $self->extract_cookie("connpass-csrftoken")
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"_csrf_token"} ||= $self->extract_cookie("connpass-csrftoken")
$self->{"_csrf_token"} $self->extract_cookie("connpass-csrftoken")
Truth table0%0/3
A ||= B
A= $self->{"_csrf_token"}B= $self->extract_cookie("connpass-csrftoken")
ABresult
1X1
011
000
59 }
60  
61 sub request_like_xhr {
62 0 ▶ my ($self, $method, $url, $param) = @_;
Subroutine request_like_xhr: not called CC=1 SCAR=6.9
Pod undocumented
63 0 my $content = $_JSON->encode($param);
64  
65 0 my $req = HTTP::Request->new($method, $url, [
66 'Content-Type' => 'application/json',
67 'Content-Length' => length $content,
68 'X-CSRFToken' => $self->_csrf_token(),
69 'X-Requested-With' => 'XMLHttpRequest',
70 ], $content);
71 0 return $self->request($req);
72 }
73  
74 1 1;
75 __END__
« blib/lib/WWW/Connpass/Event/Question.pm ↑ Summary blib/lib/WWW/Connpass/Event.pm »