blib/lib/VK/App.pm

filter: Statementstmt 18.8% 17 / 90 Branchbran 0.0% 0 / 64 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 33.3% 5 / 15 Podpod 100% 5 / 5 totaltotal 14.2% 27 / 190 CC 37 SCAR 41.0
CC
37
Cov
10%
new
156.0
_login
56.0
_authorize_app
42.0
CRAP 60.3
↑ Summary
1 package VK::App;
2
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use LWP;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use LWP::Protocol::https;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
8
9 1 our $VERSION = 0.12;
10
11 0 ▶sub new {
Subroutine new: not called CC=12 SCAR=50.5
Pod documented
12 0 my ($class, %args) = @_;
13 0 ▶ die "USAGE:\nVK::App->new(api_id => ... login => ... password => ...)\n",
Branch: 1 branch
BranchTrueFalseTotal
unless _valid_new_args(\%args) 0 0 0
14 "VK::App->new(api_id => ... cookie_file => ...)\n" unless _valid_new_args(\%args);
15
16 0 my $self;
17 0 ▶ $self->{api_id} = $args{api_id} if exists $args{api_id};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"api_id"} 0 0 0
18 0 ▶ $self->{login} = $args{login} if exists $args{login};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"login"} 0 0 0
19 0 ▶ $self->{password} = $args{password} if exists $args{password};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"password"} 0 0 0
20 0 ▶ $self->{cookie_file} = $args{cookie_file} if exists $args{cookie_file};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"cookie_file"} 0 0 0
21 0 ▶ (exists $args{scope})?($self->{scope} = $args{scope}):($self->{scope} = 'friends,photos,audio,video,wall,groups,messages,offline');
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'scope'} ? : 0 0 0
22 0 ▶ (exists $args{format})?($self->{format} = $args{format}):($self->{format} = 'Perl');
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'format'} ? : 0 0 0
23 0 ▶ (exists $args{cookie_file})?($self->{ua} = _create_ua($args{cookie_file})):($self->{ua} = _create_ua());
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'cookie_file'} ? : 0 0 0
24
25 0 bless $self, $class;
26
27 0 ▶ die 'ERROR: login failed' unless($self->_login());
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_login 0 0 0
28 0 ▶ die 'ERROR: authorize app failed' unless($self->_authorize_app());
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_authorize_app 0 0 0
29
30 0 ▶ $self->{ua}->cookie_jar()->save($self->{cookie_file}) if (exists $self->{cookie_file});
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"cookie_file"} 0 0 0
31
32 0 return $self;
33 }
34
35 0 ▶sub _login {
Subroutine _login: not called CC=7 SCAR=40.3
36 0 my $self = shift;
37 0 my $lpage = $self->{ua}->get('http://vk.com/login.php');
38 0 ▶ return 0 unless ($lpage->is_success); # network problem?
Branch: 1 branch
BranchTrueFalseTotal
unless $lpage->is_success 0 0 0
39 0 ▶ my $action = $1 if $lpage->content =~ /action=\"(.+?)\"/;
Branch: 1 branch
BranchTrueFalseTotal
if $lpage->content =~ /action=\"(.+?)\"/ 0 0 0
40 0 ▶ return 1 unless $action; # log in already? go to the next step
Branch: 1 branch
BranchTrueFalseTotal
unless $action 0 0 0
41 my $res = $self->{ua}->post($action, {
42 #my $res = $self->{ua}->post('https://login.vk.com/?act=login', {
43 email => $self->{login},
44 pass => $self->{password},
45 0 });
46 0 ▶ return 0 if $res->status_line ne "302 Found";
Branch: 1 branch
BranchTrueFalseTotal
if $res->status_line ne "302 Found" 0 0 0
47 0 ▶ return 0 if $res->header('location') !~ /__q_hash/;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->header("location") =~ /__q_hash/ 0 0 0
48 0 $res = $self->{ua}->get($res->header('location'));
49 0 ▶ return 0 unless $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->is_success 0 0 0
50 0 return $res->message;
51 }
52
53 0 ▶sub _authorize_app {
Subroutine _authorize_app: not called CC=6 SCAR=37.4
54 0 my $self = shift;
55 0 push @{ $self->{ua}->requests_redirectable }, 'POST';
56 0 my %authorize;
57 $authorize{request} = 'http://oauth.vk.com/authorize?'.
58 'client_id='.$self->{api_id}.
59 '&scope='.$self->{scope}.
60 0 '&redirect_uri=http://api.vk.com/blank.html'.
61 '&display=wap'.
62 '&response_type=token';
63 0 my $res = $self->{ua}->post($authorize{request});
64 0 ▶ return 0 unless $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->is_success 0 0 0
65 0 my $contect = $res->decoded_content;
66 0 ▶ $authorize{approve} = $1 if $contect =~ /action=\"(.+)\"/;
Branch: 1 branch
BranchTrueFalseTotal
if $contect =~ /action=\"(.+)\"/ 0 0 0
67 0 ▶ if (exists $authorize{approve}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $authorize{"approve"}) 0 0 0
68 0 $res = $self->{ua}->post($authorize{approve});
69 0 ▶ return 0 unless $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->is_success 0 0 0
70 }
71 0 ▶ if ($res->request()->uri() =~ /access_token=(.+)&expires_in=0&user_id=(\d+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->request->uri =~ /access_token=(.+)&expires_in=0&user_id=(\d+)/) 0 0 0
72 0 $self->{access_token} = $1;
73 0 $self->{uid} = $2;
74 }
75 0 return $res->message;
76 }
77
78 0 ▶sub _create_ua {
Subroutine _create_ua: not called CC=2 SCAR=17.9
79 0 my $ua = LWP::UserAgent->new(agent => "VK::App $VERSION");
80 #push @{ $ua->requests_redirectable }, 'POST';
81 #$ua->ssl_opts(verify_hostname => 0);
82 0 ▶ ($_[0])?($ua->cookie_jar( {file=>$_[0],autosave => 1} )):($ua->cookie_jar( { } ));
Branch: 1 branch
BranchTrueFalseTotal
$_[0] ? : 0 0 0
83 0 return $ua;
84 }
85
86 0 ▶sub _clean_cookie {
Subroutine _clean_cookie: not called CC=1 SCAR=6.9
87 0 my $self = shift;
88 0 $self->{ua}->cookie_jar()->clear();
89 0 return 1;
90 }
91
92 0 ▶sub _valid_new_args {
Subroutine _valid_new_args: not called CC=6 SCAR=37.4
93 0 my $args = shift;
94 0 ▶ return 0 unless ref($args) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args eq "HASH" 0 0 0
95 0 ▶ if (!$args->{api_id} ||
Branch: 1 branch
BranchTrueFalseTotal
unless ($args->{"api_id"} and $args->{"login"} && $args->{"password"} || $args->{"cookie_file"}) 0 0 0
Condition0%0/9
$args->{"api_id"} and $args->{"login"} && $args->{"password"} || $args->{"cookie_file"}
!ll&&!rl&&r
000
$args->{"login"} and $args->{"password"} or $args->{"cookie_file"}
l!l&&r!l&&!r
000
$args->{"login"} and $args->{"password"}
!ll&&!rl&&r
000
MC/DC0%0/4
$args->{"api_id"} and $args->{"login"} && $args->{"password"} || $args->{"cookie_file"}
$args->{"api_id"} $args->{"login"} $args->{"password"} $args->{"cookie_file"}
Truth table0%0/6
A and B and C or D
A= $args->{"api_id"}B= $args->{"login"}C= $args->{"password"}D= $args->{"cookie_file"}
ABCDresult
0XXX0
10X00
11000
111X1
10X11
11011
96 ((!$args->{login} || !$args->{password}) && !$args->{cookie_file}) ) {
97 0 return 0;
98 }
99 0 return 1;
100 }
101
102 0 ▶sub ua {
Subroutine ua: not called CC=2 SCAR=17.9
Pod documented
103 0 my $self = shift;
104 0 ▶ die "Can't get UserAgent object" unless exists $self->{ua};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"ua"} 0 0 0
105 0 return $self->{ua};
106 }
107
108 0 ▶sub access_token {
Subroutine access_token: not called CC=2 SCAR=17.9
Pod documented
109 0 my $self = shift;
110 0 ▶ die "Can't get access token" unless exists $self->{access_token};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"access_token"} 0 0 0
111 0 return $self->{access_token};
112 }
113
114 0 ▶sub uid {
Subroutine uid: not called CC=2 SCAR=17.9
Pod documented
115 0 my $self = shift;
116 0 ▶ die "Can't get user id" unless exists $self->{uid};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"uid"} 0 0 0
117 0 return $self->{uid};
118 }
119
120 0 ▶sub request {
Subroutine request: not called CC=6 SCAR=37.4
Pod documented
121 0 my $self = shift;
122 0 my $method = shift;
123 0 ▶ $method .= '.xml' if $self->{format} eq "XML";
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"format"} eq "XML" 0 0 0
124 0 ▶ my $params = shift || {};
Condition0%0/2
shift() || {}
l!l
00
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
125 0 my $url = 'https://api.vk.com/method/'.$method;
126 0 my $res = $self->{ua}->post($url, { %$params, access_token => $self->{access_token} });
127 0 ▶ return 0 unless $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->is_success 0 0 0
128 0 my $content = $res->content;
129 0 ▶ return $content if ($self->{format} eq "XML");
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"format"} eq "XML" 0 0 0
130 0 ▶ return $content if ($self->{format} eq "JSON");
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"format"} eq "JSON" 0 0 0
131 0 return decode_json($content);
132 }
133
134 1 1;
135
136 __END__
↑ Summary