File Coverage

blib/lib/WebService/Antigate/V1.pm
Criterion Covered Total %
statement 41 56 73.2
branch 17 30 56.6
condition 3 9 33.3
subroutine 6 6 100.0
pod 4 4 100.0
total 71 105 67.6


line stmt bran cond sub pod time code
1             package WebService::Antigate::V1;
2            
3 2     2   970 use strict;
  2         4  
  2         63  
4 2     2   10 use parent 'WebService::Antigate';
  2         3  
  2         28  
5            
6             my %MESSAGES = (
7             'ERROR_KEY_DOES_NOT_EXIST' => 'wrong service key used',
8             'ERROR_WRONG_USER_KEY' => 'wrong service key used',
9             'ERROR_NO_SLOT_AVAILABLE' => 'all recognizers are busy, try later',
10             'ERROR_ZERO_CAPTCHA_FILESIZE' => 'uploaded captcha size is zero',
11             'ERROR_TOO_BIG_CAPTCHA_FILESIZE' => 'uploaded captcha size is grater than 90 Kb',
12             'ERROR_WRONG_FILE_EXTENSION' => 'wrong extension of the uploaded captcha, allowed extensions are gif, jpg, png',
13             'ERROR_IP_NOT_ALLOWED' => 'this ip not allowed to use this account',
14             'ERROR_WRONG_ID_FORMAT' => 'captcha id should be number',
15             'ERROR_NO_SUCH_CAPCHA_ID' => 'no such captcha id in the database',
16             'ERROR_URL_METHOD_FORBIDDEN' => 'this upload method is already not supported',
17             'ERROR_IMAGE_IS_NOT_PNG' => 'captcha is not correct png file',
18             'ERROR_IMAGE_IS_NOT_JPEG' => 'captcha is not correct jpeg file',
19             'ERROR_IMAGE_IS_NOT_GIF' => 'captcha is not correct gif file',
20             'ERROR_ZERO_BALANCE' => 'you have a zero balance',
21             'CAPCHA_NOT_READY' => 'captcha is not recognized yet',
22             'OK_REPORT_RECORDED' => 'your abuse recorded',
23             'ERROR_CAPTCHA_UNSOLVABLE' => 'captcha can\'t be recognized',
24             'ERROR_BAD_DUPLICATES' => 'captcha duplicates limit reached'
25             );
26            
27             sub try_upload {
28 1     1 1 2 my ($self, %opts) = @_;
29            
30             Carp::croak "Captcha file or content should be specified and exists"
31 1 50 33     19 if (!defined($opts{file}) && !defined($opts{content})) || (defined($opts{file}) && ! -e $opts{file});
      33        
      33        
32            
33 1         3 my $file;
34             my $response = $self->{ua}->post
35             (
36             "$self->{scheme}://$self->{subdomain}$self->{domain}/in.php",
37             Content_Type => "form-data",
38             Content =>
39             [
40             key => $self->{key},
41             method => 'post',
42             file =>
43             [
44             defined($opts{file}) ?
45             (
46             $file = delete $opts{file},
47             defined($opts{name}) ?
48             delete $opts{name}
49             :
50             $file !~ /\..{1,5}$/ ? # filename without extension
51             $self->_name_by_file_signature($file)
52             :
53             undef
54             )
55             :
56             (
57             undef,
58             defined($opts{name}) ?
59             delete $opts{name}
60             :
61             $self->_name_by_signature($opts{content}),
62             Content => delete $opts{content}
63             )
64 1 50       22 ],
    50          
    0          
    50          
65             %opts
66             ]
67             );
68            
69 1 50       70353 unless($response->is_success) {
70 0         0 $self->{errno} = 'HTTP_ERROR';
71 0         0 $self->{errstr} = $response->status_line;
72 0         0 return undef;
73             }
74            
75 1         15 my $captcha_id;
76 1 50       8 unless(($captcha_id) = $response->content =~ /OK\|(\d+)/) {
77 0         0 $self->{errno} = $response->content;
78 0         0 $self->{errstr} = $MESSAGES{ $self->{errno} };
79 0         0 return undef;
80             }
81            
82 1         35 return $self->{last_captcha_id} = $captcha_id;
83             }
84            
85             sub try_recognize {
86 2     2 1 15 my ($self, $id) = @_;
87            
88 2 50       14 Carp::croak "Captcha id should be specified" unless defined $id;
89            
90 2         38 my $response = $self->{ua}->get("$self->{scheme}://$self->{subdomain}$self->{domain}/res.php?key=$self->{key}&action=get&id=$id");
91            
92 2 50       9752 unless($response->is_success) {
93 0         0 $self->{errno} = 'HTTP_ERROR';
94 0         0 $self->{errstr} = $response->status_line;
95 0         0 return undef;
96             }
97            
98 2         24 my $captcha_text;
99 2 100       9 unless(($captcha_text) = $response->content =~ /OK\|(.+)/) {
100 1         16 $self->{errno} = $response->content;
101 1         13 $self->{errstr} = $MESSAGES{ $self->{errno} };
102 1         24 return undef;
103             }
104            
105 1         27 return $captcha_text;
106             }
107            
108             sub abuse {
109 2     2 1 6 my ($self, $id) = @_;
110            
111 2 50       13 Carp::croak "Captcha id should be specified" unless defined $id;
112            
113 2         16 my $response = $self->{ua}->get("$self->{scheme}://$self->{subdomain}$self->{domain}/res.php?key=$self->{key}&action=reportbad&id=$id");
114            
115 2 50       7651 unless($response->is_success) {
116 0         0 $self->{errno} = 'HTTP_ERROR';
117 0         0 $self->{errstr} = $response->status_line;
118 0         0 return undef;
119             }
120            
121 2 100       29 unless($response->content eq 'OK_REPORT_RECORDED') {
122 1         13 $self->{errno} = $response->content;
123 1         10 $self->{errstr} = $MESSAGES{ $self->{errno} };
124 1         9 return undef;
125             }
126            
127 1         29 return 1;
128             }
129            
130            
131             sub balance {
132 2     2 1 9 my $self = shift;
133            
134 2         15 my $response = $self->{ua}->get("$self->{scheme}://$self->{subdomain}$self->{domain}/res.php?key=$self->{key}&action=getbalance");
135            
136 2 50       7725 unless($response->is_success) {
137 0         0 $self->{errno} = 'HTTP_ERROR';
138 0         0 $self->{errstr} = $response->status_line;
139 0         0 return undef;
140             }
141            
142 2 100       22 if($response->content =~ /^ERROR_/) {
143 1         15 $self->{errno} = $response->content;
144 1         13 $self->{errstr} = $MESSAGES{ $self->{errno} };
145 1         9 return undef;
146             }
147            
148 1         16 return $response->content;
149             }
150            
151             1;
152            
153             __END__