File Coverage

blib/lib/WWW/Connpass/Agent.pm
Criterion Covered Total %
statement 21 49 42.8
branch 0 8 0.0
condition 0 5 0.0
subroutine 7 13 53.8
pod 2 4 50.0
total 30 79 37.9


line stmt bran cond sub pod time code
1             package WWW::Connpass::Agent;
2 1     1   3 use strict;
  1         2  
  1         21  
3 1     1   3 use warnings;
  1         1  
  1         21  
4              
5 1     1   3 use parent qw/WWW::Mechanize/;
  1         1  
  1         5  
6              
7 1     1   64600 use Time::HiRes qw/gettimeofday tv_interval/;
  1         971  
  1         4  
8 1     1   126 use HTTP::Request;
  1         2  
  1         18  
9 1     1   3 use JSON 2;
  1         11  
  1         6  
10              
11 1     1   91 use constant DEBUG => $ENV{WWW_CONNPASS_DEBUG};
  1         1  
  1         341  
12              
13             my $_JSON = JSON->new->utf8;
14              
15             sub new {
16 0     0 1   my ($class, %args) = @_;
17 0   0       my $interval = delete $args{interval} || 1.0;
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     0 1   my $self = shift;
26 0 0         if (my $last_req_at = $self->{_last_req_at}) {
27 0           my $sec = tv_interval($last_req_at);
28 0 0         Time::HiRes::sleep $self->{_interval} - $sec if $sec < $self->{_interval};
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     0 0   my ($self, $expected_key) = @_;
44              
45 0           my $result;
46             $self->cookie_jar->scan(sub {
47 0     0     my ($key, $val) = @_[1..2];
48 0 0         return if defined $result;
49 0 0         return if $key ne $expected_key;
50 0           $result = $val;
51 0           });
52              
53 0           return $result;
54             }
55              
56             sub _csrf_token {
57 0     0     my $self = shift;
58 0   0       $self->{_csrf_token} ||= $self->extract_cookie('connpass-csrftoken');
59             }
60              
61             sub request_like_xhr {
62 0     0 0   my ($self, $method, $url, $param) = @_;
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;
75             __END__