File Coverage

blib/lib/WebService/Antigate/V1.pm
Criterion Covered Total %
statement 41 56 73.2
branch 19 32 59.3
condition 4 6 66.6
subroutine 6 6 100.0
pod 4 4 100.0
total 74 104 71.1


line stmt bran cond sub pod time code
1             package WebService::Antigate::V1;
2            
3 2     2   1121 use strict;
  2         6  
  2         77  
4 2     2   10 use parent 'WebService::Antigate';
  2         4  
  2         46  
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 2     2 1 6 my ($self, %opts) = @_;
29            
30             Carp::croak "Specified captcha file doesn't exist"
31 2 50 66     29 if defined($opts{file}) && ! -e $opts{file};
32            
33 2         4 my $file;
34             my $response = $self->{ua}->post
35             (
36             "$self->{scheme}://$self->{subdomain}$self->{domain}/in.php",
37             defined $opts{file} || defined $opts{content}
38             ? (
39             Content_Type => "form-data",
40             Content =>
41             [
42             key => $self->{key},
43             method => 'post',
44             file =>
45             [
46             defined($opts{file}) ?
47             (
48             $file = delete $opts{file},
49             defined($opts{name}) ?
50             delete $opts{name}
51             :
52             $file !~ /\..{1,5}$/ ? # filename without extension
53             $self->_name_by_file_signature($file)
54             :
55             undef
56             )
57             :
58             (
59             undef,
60             defined($opts{name}) ?
61             delete $opts{name}
62             :
63             $self->_name_by_signature($opts{content}),
64             Content => delete $opts{content}
65             )
66 2 50 66     43 ],
    50          
    0          
    50          
    100          
67             %opts
68             ]
69             )
70             : \%opts
71             );
72            
73 2 50       100984 unless($response->is_success) {
74 0         0 $self->{errno} = 'HTTP_ERROR';
75 0         0 $self->{errstr} = $response->status_line;
76 0         0 return undef;
77             }
78            
79 2         26 my $captcha_id;
80 2 50       13 unless(($captcha_id) = $response->content =~ /OK\|(\d+)/) {
81 0         0 $self->{errno} = $response->content;
82 0         0 $self->{errstr} = $MESSAGES{ $self->{errno} };
83 0         0 return undef;
84             }
85            
86 2         73 return $self->{last_captcha_id} = $captcha_id;
87             }
88            
89             sub try_recognize {
90 2     2 1 18 my ($self, $id) = @_;
91            
92 2 50       20 Carp::croak "Captcha id should be specified" unless defined $id;
93            
94 2         55 my $response = $self->{ua}->get("$self->{scheme}://$self->{subdomain}$self->{domain}/res.php?key=$self->{key}&action=get&id=$id");
95            
96 2 50       10871 unless($response->is_success) {
97 0         0 $self->{errno} = 'HTTP_ERROR';
98 0         0 $self->{errstr} = $response->status_line;
99 0         0 return undef;
100             }
101            
102 2         28 my $captcha_text;
103 2 100       10 unless(($captcha_text) = $response->content =~ /OK\|(.+)/) {
104 1         20 $self->{errno} = $response->content;
105 1         14 $self->{errstr} = $MESSAGES{ $self->{errno} };
106 1         36 return undef;
107             }
108            
109 1         37 return $captcha_text;
110             }
111            
112             sub abuse {
113 2     2 1 7 my ($self, $id) = @_;
114            
115 2 50       8 Carp::croak "Captcha id should be specified" unless defined $id;
116            
117 2         15 my $response = $self->{ua}->get("$self->{scheme}://$self->{subdomain}$self->{domain}/res.php?key=$self->{key}&action=reportbad&id=$id");
118            
119 2 50       7856 unless($response->is_success) {
120 0         0 $self->{errno} = 'HTTP_ERROR';
121 0         0 $self->{errstr} = $response->status_line;
122 0         0 return undef;
123             }
124            
125 2 100       27 unless($response->content eq 'OK_REPORT_RECORDED') {
126 1         14 $self->{errno} = $response->content;
127 1         14 $self->{errstr} = $MESSAGES{ $self->{errno} };
128 1         12 return undef;
129             }
130            
131 1         23 return 1;
132             }
133            
134            
135             sub balance {
136 2     2 1 7 my $self = shift;
137            
138 2         14 my $response = $self->{ua}->get("$self->{scheme}://$self->{subdomain}$self->{domain}/res.php?key=$self->{key}&action=getbalance");
139            
140 2 50       7732 unless($response->is_success) {
141 0         0 $self->{errno} = 'HTTP_ERROR';
142 0         0 $self->{errstr} = $response->status_line;
143 0         0 return undef;
144             }
145            
146 2 100       24 if($response->content =~ /^ERROR_/) {
147 1         17 $self->{errno} = $response->content;
148 1         15 $self->{errstr} = $MESSAGES{ $self->{errno} };
149 1         10 return undef;
150             }
151            
152 1         16 return $response->content;
153             }
154            
155             1;
156            
157             __END__