Antigate/V2.pm

filter: Statementstmt 78.8% 67 / 85 Branchbran 59.5% 25 / 42 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 9 / 9 Podpod 100% 5 / 5 totaltotal 73.2% 107 / 146 SCAR 36.2
CC
24
Cov
72%
try_upload
16.4
try_recognize
7.7
abuse
5.9
CRAP 37.3
« Antigate.pm ↑ Summary Antigate/V1.pm »
1 package WebService::Antigate::V2;
2
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use JSON::PP;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use parent 'WebService::Antigate';
Subroutine BEGIN: called CC=1 SCAR=0.0
7
8 sub new {
9 1 ▶ my ($class, %args) = @_;
Subroutine new: called CC=3 SCAR=11.4
Pod documented
10
11 # change some defaults
12 1 ▶ $args{scheme} = 'https' unless defined $args{scheme};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"scheme"} 1 0 1
13 1 ▶ $args{subdomain} = 'api.' unless defined $args{subdomain};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"subdomain"} 1 0 1
14
15 1 $class->SUPER::new(%args);
16 }
17
18 sub try_upload {
19 1 ▶ my ($self, %opts) = @_;
Subroutine try_upload: called CC=10 SCAR=28.0
Pod documented
20
21 1 ▶ if ( defined $opts{file} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"file"}) 1 0 1
22 1 $opts{content} = do {
23 1 local $/;
24 open my $fh, '<:raw', $opts{file}
25 1 ▶ or Carp::croak "open `$opts{file}': ", $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $opts{"file"} or Carp::croak(qq[open `$opts{"file"}': ], $!) 0 1 1
26 1 <$fh>;
27 };
28 }
29
30 1 ▶ if ( defined $opts{content} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"content"}) 1 0 1
31 1 $opts{body} = encode_base64( $opts{content}, '' );
32 }
33
34 1 ▶ if ( defined $opts{body} && !defined $opts{type} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"body"} and not defined $opts{"type"}) 1 0 1
Condition33%1/3
defined $opts{"body"} and not defined $opts{"type"}
!ll&&!rl&&r
001
MC/DC0%0/2
defined $opts{"body"} and not defined $opts{"type"}
defined $opts{"body"} not defined $opts{"type"}
Truth table33%1/3
A and not(B)
A= defined $opts{"body"}B= not defined $opts{"type"}
ABresult
0X0
100
111
35 1 $opts{type} = 'ImageToTextTask';
36 }
37
38 my $response = $self->{ua}->post(
39 "$self->{scheme}://$self->{subdomain}$self->{domain}/createTask",
40 Content => encode_json {
41 clientKey => $self->{key},
42 exists $opts{softId} ? ( softId => delete $opts{softId} ) : (),
43 1 ▶ exists $opts{languagePool} ? ( languagePool => delete $opts{languagePool} ) : (),
Branch: 2 branches
BranchTrueFalseTotal
exists $opts{'softId'} ? : 0 1 1
exists $opts{'languagePool'} ? : 0 1 1
44 task => {
45 %opts
46 }
47 }
48 );
49
50 1 ▶ unless($response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 1 1
51 0 $self->{errno} = 'HTTP_ERROR';
52 0 $self->{errstr} = $response->status_line;
53 0 return undef;
54 }
55
56 1 my $result = decode_json $response->decoded_content;
57 1 ▶ if ($result->{errorId}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->{"errorId"}) 0 1 1
58 0 $self->{errno} = $result->{errorCode};
59 0 $self->{errstr} = $result->{errorDescription};
60 0 return undef;
61 }
62
63 1 return $self->{last_captcha_id} = $result->{taskId};
64 }
65
66 sub try_recognize {
67 5 ▶ my ($self, $id) = @_;
Subroutine try_recognize: called CC=7 SCAR=20.4
Pod documented
68
69 5 ▶ Carp::croak "Captcha id should be specified" unless defined $id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 5 5
70
71 my $response = $self->{ua}->post(
72 "$self->{scheme}://$self->{subdomain}$self->{domain}/getTaskResult",
73 Content => encode_json {
74 clientKey => $self->{key},
75 5 taskId => $id
76 }
77 );
78
79 5 ▶ unless($response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 5 5
80 0 $self->{errno} = 'HTTP_ERROR';
81 0 $self->{errstr} = $response->status_line;
82 0 return undef;
83 }
84
85 5 my $result = decode_json $response->decoded_content;
86 5 ▶ if ($result->{errorId}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->{"errorId"}) 1 4 5
87 1 $self->{errno} = $result->{errorCode};
88 1 $self->{errstr} = $result->{errorDescription};
89 1 return undef;
90 }
91
92 4 ▶ if ($result->{status} ne 'ready') {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->{"status"} ne "ready") 2 2 4
93 2 $self->{errno} = 'CAPCHA_NOT_READY';
94 2 $self->{errstr} = 'captcha is not recognized yet';
95 2 return undef;
96 }
97
98 2 for my $key ( qw/text gRecaptchaResponse token/ ) {
99 2 ▶ return $result->{solution}{$key} if exists $result->{solution}{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $result->{"solution"}{$key} 2 0 2
100 }
101
102 0 return $result->{solution};
103 }
104
105 sub abuse {
106 2 ▶ my ($self, $id) = @_;
Subroutine abuse: called CC=5 SCAR=17.8
Pod documented
107
108 2 ▶ Carp::croak "Captcha id should be specified" unless defined $id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 2 2
109
110 my $response = $self->{ua}->post(
111 "$self->{scheme}://$self->{subdomain}$self->{domain}/reportIncorrectImageCaptcha",
112 Content => encode_json {
113 clientKey => $self->{key},
114 2 taskId => $id
115 }
116 );
117
118 2 ▶ unless($response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 2 2
119 0 $self->{errno} = 'HTTP_ERROR';
120 0 $self->{errstr} = $response->status_line;
121 0 return undef;
122 }
123
124 2 my $result = decode_json $response->decoded_content;
125 2 ▶ if ($result->{errorId}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->{"errorId"}) 1 1 2
126 1 ▶ if ($result->{errorCode}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->{'errorCode'}) { } 1 0 1
127 1 $self->{errno} = $result->{errorCode};
128 1 $self->{errstr} = $result->{errorDescription};
129 }
130 else {
131 0 $self->{errno} = 'ERROR_NO_SUCH_CAPCHA_ID';
132 0 $self->{errstr} = 'no such captcha id in the database';
133 }
134 1 return undef;
135 }
136
137 1 return $result->{status};
138 }
139
140 sub balance {
141 2 ▶ my $self = shift;
Subroutine balance: called CC=3 SCAR=11.4
Pod documented
142
143 my $response = $self->{ua}->post(
144 "$self->{scheme}://$self->{subdomain}$self->{domain}/getBalance",
145 Content => encode_json {
146 clientKey => $self->{key},
147 }
148 2 );
149
150 2 ▶ unless($response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 2 2
151 0 $self->{errno} = 'HTTP_ERROR';
152 0 $self->{errstr} = $response->status_line;
153 0 return undef;
154 }
155
156 2 my $result = decode_json $response->decoded_content;
157 2 ▶ if ($result->{errorId}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->{"errorId"}) 1 1 2
158 1 $self->{errno} = $result->{errorCode};
159 1 $self->{errstr} = $result->{errorDescription};
160 1 return undef;
161 }
162
163 1 return $result->{balance};
164 }
165
166 2 1;
167
168 __END__
« Antigate.pm ↑ Summary Antigate/V1.pm »