| 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 | my $_JSON = JSON->new->utf8; | |||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | sub new { | ||||||||||||||||||||
| 14 | 0 | ▶ | my ($class, %args) = @_; | ||||||||||||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 15 | 0 | ▶ | my $interval = delete $args{interval} || 1.0; | ||||||||||||||||||
|
Condition0%0/2
delete $args{'interval'} || 1.0
MC/DC0%0/1
delete $args{'interval'} || 1.0
delete $args{'interval'}
Truth table0%0/2
A
A= delete $args{'interval'}
| |||||||||||||||||||||
| 16 | 0 | my $self = $class->SUPER::new(%args); | |||||||||||||||||||
| 17 | 0 | $self->{_interval} = $interval; | |||||||||||||||||||
| 18 | 0 | $self->{_last_req_at} = undef; | |||||||||||||||||||
| 19 | 0 | return $self; | |||||||||||||||||||
| 20 | } | ||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | sub request { | ||||||||||||||||||||
| 23 | 0 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
request: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||
| 24 | 0 | ▶ | if (my $last_req_at = $self->{_last_req_at}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 25 | 0 | my $sec = tv_interval($last_req_at); | |||||||||||||||||||
| 26 | 0 | ▶ | Time::HiRes::sleep $self->{_interval} - $sec if $sec < $self->{_interval}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 27 | } | ||||||||||||||||||||
| 28 | 0 | my $res = $self->SUPER::request(@_); | |||||||||||||||||||
| 29 | 0 | $self->{_last_req_at} = [gettimeofday]; | |||||||||||||||||||
| 30 | 0 | return $res; | |||||||||||||||||||
| 31 | } | ||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | sub extract_cookie { | ||||||||||||||||||||
| 34 | 0 | ▶ | my ($self, $expected_key) = @_; | ||||||||||||||||||
|
Subroutine
extract_cookie: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | 0 | my $result; | |||||||||||||||||||
| 37 | $self->cookie_jar->scan(sub { | ||||||||||||||||||||
| 38 | 0 | ▶ | my ($key, $val) = @_[1..2]; | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||
| 39 | 0 | ▶ | return if defined $result; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 40 | 0 | ▶ | return if $key ne $expected_key; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 41 | 0 | $result = $val; | |||||||||||||||||||
| 42 | 0 | }); | |||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | 0 | return $result; | |||||||||||||||||||
| 45 | } | ||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | sub _csrf_token { | ||||||||||||||||||||
| 48 | 0 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
_csrf_token: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 49 | 0 | ▶ | $self->{_csrf_token} ||= $self->extract_cookie('connpass-csrftoken'); | ||||||||||||||||||
|
Condition0%0/3
$self->{"_csrf_token"} ||= $self->extract_cookie("connpass-csrftoken")
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")
| |||||||||||||||||||||
| 50 | } | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | sub request_like_xhr { | ||||||||||||||||||||
| 53 | 0 | ▶ | my ($self, $method, $url, $param) = @_; | ||||||||||||||||||
|
Subroutine
request_like_xhr: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 54 | 0 | my $content = $_JSON->encode($param); | |||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | 0 | my $req = HTTP::Request->new($method, $url, [ | |||||||||||||||||||
| 57 | 'Content-Type' => 'application/json', | ||||||||||||||||||||
| 58 | 'Content-Length' => length $content, | ||||||||||||||||||||
| 59 | 'X-CSRFToken' => $self->_csrf_token(), | ||||||||||||||||||||
| 60 | 'X-Requested-With' => 'XMLHttpRequest', | ||||||||||||||||||||
| 61 | ], $content); | ||||||||||||||||||||
| 62 | 0 | return $self->request($req); | |||||||||||||||||||
| 63 | } | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | 1 | 1; | |||||||||||||||||||
| 66 | __END__ | ||||||||||||||||||||