blib/lib/Net/VKontakte/Standalone.pm

filter: Statementstmt 22.9% 22 / 96 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 43.7% 7 / 16 Podpod 100% 8 / 8 totaltotal 20.2% 37 / 183 CC 28 SCAR 50.2
CC
28
Cov
14%
api
342.0
new
42.0
redirected
20.0
CRAP 151.8
↑ Summary
1 package Net::VKontakte::Standalone;
2  
3 1 ▶use 5.008000;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use WWW::Mechanize;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 our $VERSION = '0.11';
13  
14 0 ▶sub new {
Subroutine new: not called CC=6 SCAR=37.4
Pod documented
15 0 my $class = shift;
16 0 my $self = bless {},$class;
17 0 $self->{browser} = WWW::Mechanize::->new(
18 agent => __PACKAGE__.$VERSION,
19 autocheck => 1,
20 );
21 0 ▶ if (@_ == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 1) { } 0 0 0
elsif (@_ % 2 == 0) { } 0 0 0
22 0 $self->{api_id} = $_[0];
23 } elsif (@_ % 2 == 0) { # smells like hash
24 0 my %opt = @_;
25 0 for my $key (qw/api_id errors_noauto captcha_handler/) {
26 0 ▶ $self->{$key} = $opt{$key} if defined $opt{$key};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt{$key} 0 0 0
27 }
28 } else {
29 0 croak "wrong number of arguments to constructor";
30 }
31 0 ▶ croak "api_id is required" unless $self->{api_id};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"api_id"} 0 0 0
32 0 return $self;
33 }
34  
35 0 ▶sub _request {
Subroutine _request: not called CC=1 SCAR=6.9
36 0 my ($self, $params, $base) = @_;
37 0 (my $uri = URI::->new($base))->query_form($params);
38 0 return $self->{browser}->get($uri);
39 }
40  
41 0 ▶sub auth { # dirty hack
Subroutine auth: not called CC=2 SCAR=17.9
Pod documented
42 0 my ($self,$login,$password,$scope) = @_;
43 0 @{$self}{"login","password","scope"} = ($login, $password, $scope); # reuse in case of reauth
44 0 $self->{browser}->get($self->auth_uri($scope));
45 $self->{browser}->submit_form(
46 0 with_fields => {
47 email => $login,
48 pass => $password,
49 },
50 ); # log in
51 0 ▶ $self->{browser}->submit unless $self->{browser}->uri =~ m|^https://oauth.vk.com/blank.html|; # allow access if requested to
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"browser"}->uri =~ m[^https://oauth.vk.com/blank.html] 0 0 0
52 0 return $self->redirected($self->{browser}->uri);
53 }
54  
55 0 ▶sub auth_uri {
Subroutine auth_uri: not called CC=1 SCAR=6.9
Pod documented
56 0 my ($self, $scope, $display) = @_;
57 (my $uri = URI::->new("https://api.vkontakte.ru/oauth/authorize"))->query_form(
58 {
59 client_id => $self->{api_id},
60 0 redirect_uri => "blank.html",
61 scope => $scope,
62 response_type => "token",
63 display => $display,
64 }
65 );
66 0 return $uri->canonical;
67 }
68  
69 0 ▶sub redirected {
Subroutine redirected: not called CC=4 SCAR=30.0
Pod documented
70 0 my ($self, $uri) = @_;
71 0 ▶ my %params = map { split /=/,$_,2 } split /&/,$1 if $uri =~ m|https://oauth.vk.com/blank.html#(.*)|;
Branch: 1 branch
BranchTrueFalseTotal
if $uri =~ m[https://oauth.vk.com/blank.html#(.*)] 0 0 0
72 0 ▶ croak "No access_token returned (wrong login/password?)" unless defined $params{access_token};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params{"access_token"} 0 0 0
73 0 $self->{access_token} = $params{access_token};
74 0 ▶ croak "No token expiration time returned" unless $params{expires_in};
Branch: 1 branch
BranchTrueFalseTotal
unless $params{"expires_in"} 0 0 0
75 0 $self->{auth_time} = time;
76 0 $self->{expires_in} = $params{expires_in};
77 0 return $self;
78 }
79  
80  
81 0 ▶sub api {
Subroutine api: not called CC=18 SCAR=58.3
Pod documented
82 0 my ($self,$method,$params) = @_;
83 0 ▶ croak "Cannot make API calls unless authentificated" unless defined $self->{access_token};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"access_token"} 0 0 0
84 0 ▶ if (time - $self->{auth_time} > $self->{expires_in}) {
Branch: 1 branch
BranchTrueFalseTotal
if (time - $self->{"auth_time"} > $self->{"expires_in"}) 0 0 0
85 0 ▶ if ($self->{login} && $self->{password} && $self->{scope}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'login'} and $self->{'password'} and $self->{'scope'}) { } 0 0 0
Condition0%0/6
$self->{"login"} and $self->{"password"} and $self->{"scope"}
!ll&&!rl&&r
000
$self->{"login"} and $self->{"password"}
!ll&&!rl&&r
000
MC/DC0%0/3
$self->{"login"} and $self->{"password"} and $self->{"scope"}
$self->{"login"} $self->{"password"} $self->{"scope"}
Truth table0%0/4
A and B and C
A= $self->{"login"}B= $self->{"password"}C= $self->{"scope"}
ABCresult
0XX0
10X0
1100
1111
86 0 $self->auth($self->{"login","password","scope"});
87 } else {
88 0 ▶ if ($self->{errors_noauto}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'errors_noauto'}) { } 0 0 0
89 0 $self->{error} = "access_token expired";
90 0 ▶ if (ref $self->{errors_noauto} and ref $self->{errors_noauto} eq "CODE") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{"errors_noauto"} and ref $self->{"errors_noauto"} eq "CODE") 0 0 0
Condition0%0/3
ref $self->{"errors_noauto"} and ref $self->{"errors_noauto"} eq "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $self->{"errors_noauto"} and ref $self->{"errors_noauto"} eq "CODE"
ref $self->{"errors_noauto"} ref $self->{"errors_noauto"} eq "CODE"
Truth table0%0/3
A and B
A= ref $self->{"errors_noauto"}B= ref $self->{"errors_noauto"} eq "CODE"
ABresult
0X0
100
111
91 0 $self->{errors_noauto}->({error_code => "none", error_msg => "access_token expired"});
92 }
93 0 return;
94 } else {
95 0 croak "access_token expired";
96 }
97 }
98 }
99 0 $params->{access_token} = $self->{access_token};
100 REQUEST: {
101 0 my $response = decode_json $self->_request($params,"https://api.vk.com/method/$method")->decoded_content;
102 0 ▶ if ($response->{response}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($response->{'response'}) { } 0 0 0
elsif ($response->{'error'}) { } 0 0 0
103 0 return $response->{response};
104 } elsif ($response->{error}) {
105 0 ▶ if ($self->{errors_noauto}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'errors_noauto'}) { } 0 0 0
106 0 $self->{error} = $response->{error};
107 0 ▶ if (ref $self->{errors_noauto} and ref $self->{errors_noauto} eq "CODE") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{"errors_noauto"} and ref $self->{"errors_noauto"} eq "CODE") 0 0 0
Condition0%0/3
ref $self->{"errors_noauto"} and ref $self->{"errors_noauto"} eq "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $self->{"errors_noauto"} and ref $self->{"errors_noauto"} eq "CODE"
ref $self->{"errors_noauto"} ref $self->{"errors_noauto"} eq "CODE"
Truth table0%0/3
A and B
A= ref $self->{"errors_noauto"}B= ref $self->{"errors_noauto"} eq "CODE"
ABresult
0X0
100
111
108 0 $self->{errors_noauto}->($response->{error});
109 }
110 0 return;
111 } else {
112 0 ▶ if (6 == $response->{error}{error_code}) { # Too many requests per second.
Branch: 2 branches
BranchTrueFalseTotal
if (6 == $response->{'error'}{'error_code'}) { } 0 0 0
elsif (14 == $response->{'error'}{'error_code'}) { } 0 0 0
113 0 sleep 1;
114 0 redo REQUEST;
115 } elsif (14 == $response->{error}{error_code}) { # Captcha is needed
116 0 ▶ if ($self->{captcha_handler}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'captcha_handler'}) { } 0 0 0
117 0 $params->{captcha_key} = $self->{captcha_handler}->($response->{error}{captcha_img});
118 0 $params->{captcha_sid} = $response->{error}{captcha_sid};
119 0 redo REQUEST;
120 } else {
121 0 croak "Captcha is needed and no captcha handler specified";
122 }
123 } else {
124 0 croak "API call returned error ".$response->{error}{error_msg};
125 }
126 }
127 } else {
128 croak "API call didn't return response or error".
129 0 ▶ $Carp::Verbose ? eval { require Data::Dumper; Data::Dumper::Dumper($response) }
Branch: 1 branch
BranchTrueFalseTotal
q[API call didn't return response or error] . $Carp::Verbose ? : 0 0 0
130 : "";
131 }
132 }
133 }
134  
135 0 ▶sub captcha_handler {
Subroutine captcha_handler: not called CC=2 SCAR=17.9
Pod documented
136 0 my ($self, $handler) = @_;
137 0 ▶ croak "\$handler is not a subroutine reference" unless ref $handler eq "CODE";
Branch: 1 branch
BranchTrueFalseTotal
unless ref $handler eq "CODE" 0 0 0
138 0 $self->{captcha_handler} = $handler;
139 0 return $self;
140 }
141  
142 0 ▶sub error {
Subroutine error: not called CC=1 SCAR=6.9
Pod documented
143 0 return shift->{error};
144 }
145  
146 0 ▶sub errors_noauto {
Subroutine errors_noauto: not called CC=1 SCAR=6.9
Pod documented
147 0 my ($self, $noauto) = @_;
148 0 $self->{errors_noauto} = $noauto; # whatever this means
149 0 return $self;
150 }
151  
152 1 1;
153 __END__
↑ Summary