line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package WWW::WorldLingo; |
2
|
3
|
|
|
3
|
|
91252
|
use base qw( Class::Accessor::Fast ); |
|
3
|
|
|
|
|
9
|
|
|
3
|
|
|
|
|
3340
|
|
3
|
3
|
|
|
3
|
|
11703
|
use strict; |
|
3
|
|
|
|
|
10
|
|
|
3
|
|
|
|
|
77
|
|
4
|
3
|
|
|
3
|
|
16
|
use warnings; |
|
3
|
|
|
|
|
11
|
|
|
3
|
|
|
|
|
91
|
|
5
|
3
|
|
|
3
|
|
14
|
use Carp; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
1022
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our $VERSION = "0.03"; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
__PACKAGE__->mk_accessors(qw( server |
10
|
|
|
|
|
|
|
response |
11
|
|
|
|
|
|
|
mimetype encoding |
12
|
|
|
|
|
|
|
scheme |
13
|
|
|
|
|
|
|
subscription password |
14
|
|
|
|
|
|
|
srclang trglang srcenc trgenc |
15
|
|
|
|
|
|
|
dictno gloss |
16
|
|
|
|
|
|
|
data |
17
|
|
|
|
|
|
|
result |
18
|
|
|
|
|
|
|
)); |
19
|
|
|
|
|
|
|
|
20
|
3
|
|
|
3
|
|
2852
|
use HTTP::Request::Common; |
|
3
|
|
|
|
|
99857
|
|
|
3
|
|
|
|
|
267
|
|
21
|
3
|
|
|
3
|
|
2992
|
use HTTP::Response; |
|
3
|
|
|
|
|
24954
|
|
|
3
|
|
|
|
|
107
|
|
22
|
3
|
|
|
3
|
|
3633
|
use LWP::UserAgent; |
|
3
|
|
|
|
|
57521
|
|
|
3
|
|
|
|
|
132
|
|
23
|
|
|
|
|
|
|
|
24
|
3
|
|
|
3
|
|
34
|
use constant ERROR_CODE_HEADER => "X-WL-ERRORCODE"; |
|
3
|
|
|
|
|
8
|
|
|
3
|
|
|
|
|
258
|
|
25
|
3
|
|
|
3
|
|
18
|
use constant API_MODE_HEADER => "X-WL-API-MODE"; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
3500
|
|
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
my %Errors = ( |
28
|
|
|
|
|
|
|
0 => "Successful", # no error |
29
|
|
|
|
|
|
|
6 => "Subscription expired or suspended", # guessing here |
30
|
|
|
|
|
|
|
26 => "Incorrect password", |
31
|
|
|
|
|
|
|
28 => "Source language not in subscription", |
32
|
|
|
|
|
|
|
29 => "Target language not in subscription", |
33
|
|
|
|
|
|
|
176 => "Invalid language pair", |
34
|
|
|
|
|
|
|
177 => "No input data", |
35
|
|
|
|
|
|
|
502 => "Invalid Mime-type", |
36
|
|
|
|
|
|
|
1176 => "Translation timed out", |
37
|
|
|
|
|
|
|
); |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
sub new : method { |
41
|
2
|
|
|
2
|
1
|
27
|
my ( $class, $arg_hashref ) = @_; |
42
|
2
|
50
|
|
|
|
57
|
my $self = $class->SUPER::new({ |
43
|
|
|
|
|
|
|
subscription =>"S000.1", |
44
|
|
|
|
|
|
|
password => "secret", |
45
|
|
|
|
|
|
|
server => "www.worldlingo.com", |
46
|
|
|
|
|
|
|
scheme => "http", |
47
|
2
|
|
|
|
|
8
|
%{$arg_hashref || {}}, |
48
|
|
|
|
|
|
|
}); |
49
|
2
|
|
|
|
|
42
|
$self; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub agent : method { |
53
|
0
|
|
|
0
|
1
|
0
|
my ( $self, $agent ) = @_; |
54
|
0
|
|
|
|
|
0
|
$self->{_agent} = $agent; |
55
|
0
|
0
|
|
|
|
0
|
unless ( $self->{_agent} ) |
56
|
|
|
|
|
|
|
{ |
57
|
0
|
|
|
|
|
0
|
eval { require LWPx::ParanoidAgent; }; |
|
0
|
|
|
|
|
0
|
|
58
|
0
|
0
|
|
|
|
0
|
my $agent_class = $@ ? "LWP::UserAgent" : "LWPx::ParanoidAgent"; |
59
|
0
|
|
|
|
|
0
|
my $ua = $agent_class->new(agent => __PACKAGE__ ."/". $VERSION); |
60
|
0
|
|
|
|
|
0
|
$self->{_agent} = $ua; |
61
|
|
|
|
|
|
|
} |
62
|
0
|
|
|
|
|
0
|
return $self->{_agent}; |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub request : method { |
66
|
4
|
|
|
4
|
1
|
2692
|
my ( $self, $request ) = @_; |
67
|
4
|
50
|
|
|
|
17
|
$self->{_request} = $request if $request; |
68
|
4
|
|
66
|
|
|
30
|
$self->{_request} ||= POST $self->api, scalar $self->_arguments; |
69
|
4
|
|
|
|
|
30353
|
return $self->{_request}; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
sub parse : method { |
73
|
0
|
|
|
0
|
1
|
0
|
my ( $class, $response ) = @_; |
74
|
0
|
|
|
|
|
0
|
my $self = $class->new(); |
75
|
0
|
0
|
0
|
|
|
0
|
unless ( ref $response and $response->isa("HTTP::Response") ) |
76
|
|
|
|
|
|
|
{ |
77
|
0
|
|
|
|
|
0
|
$response = HTTP::Response->parse($response); |
78
|
0
|
|
|
|
|
0
|
carp "This " . ref($self) . " object has no memory of its original request"; |
79
|
|
|
|
|
|
|
} |
80
|
0
|
|
|
|
|
0
|
$self->_handle_response($response); |
81
|
|
|
|
|
|
|
# responses remade from strings have *no* memory of a request in them |
82
|
0
|
0
|
|
|
|
0
|
$self->request( $self->response->request ) if $self->response->request; |
83
|
0
|
|
|
|
|
0
|
return $self; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
sub api : method { |
87
|
6
|
|
|
6
|
1
|
4643
|
my ( $self ) = @_; |
88
|
6
|
|
|
|
|
29
|
return join("://", |
89
|
|
|
|
|
|
|
$self->scheme, |
90
|
|
|
|
|
|
|
join("/", |
91
|
|
|
|
|
|
|
$self->server, |
92
|
|
|
|
|
|
|
$self->subscription, |
93
|
|
|
|
|
|
|
"api") |
94
|
|
|
|
|
|
|
); |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
sub translate : method { |
98
|
0
|
|
|
0
|
1
|
0
|
my ( $self, $data ) = @_; |
99
|
0
|
|
|
|
|
0
|
$self->{_api_mode} = $self->{_error} = $self->{_error_code} = undef; |
100
|
0
|
0
|
|
|
|
0
|
$self->data($data) if $data; |
101
|
0
|
|
|
|
|
0
|
my $response = $self->agent->request($self->request); |
102
|
|
|
|
|
|
|
# use Data::Dumper; warn Dumper $response; |
103
|
0
|
|
|
|
|
0
|
$self->_handle_response($response); |
104
|
0
|
|
|
|
|
0
|
return $self->result; |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
sub _handle_response { |
108
|
0
|
|
|
0
|
|
0
|
my ( $self, $response ) = @_; |
109
|
0
|
|
|
|
|
0
|
$self->response($response); |
110
|
0
|
|
|
|
|
0
|
$self->{_error_code} = $response->header(ERROR_CODE_HEADER); |
111
|
0
|
|
|
|
|
0
|
$self->{_api_mode} = $response->header(API_MODE_HEADER); |
112
|
|
|
|
|
|
|
|
113
|
0
|
0
|
0
|
|
|
0
|
if ( $response->is_success |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
114
|
|
|
|
|
|
|
and $Errors{$self->{_error_code}} eq "Successful" ) |
115
|
|
|
|
|
|
|
{ |
116
|
0
|
|
|
|
|
0
|
eval { |
117
|
0
|
|
|
|
|
0
|
$self->result( $response->decoded_content ); |
118
|
|
|
|
|
|
|
}; |
119
|
0
|
0
|
|
|
|
0
|
if ( $@ ) |
120
|
|
|
|
|
|
|
{ |
121
|
0
|
|
|
|
|
0
|
carp "Couldn't decode content with LWP library"; |
122
|
0
|
|
|
|
|
0
|
$self->result( $response->content ); |
123
|
|
|
|
|
|
|
} |
124
|
0
|
|
|
|
|
0
|
$self->result; |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
elsif ( $self->{_error_code} ) # API error |
127
|
|
|
|
|
|
|
{ |
128
|
0
|
|
0
|
|
|
0
|
$self->{_error} = $Errors{$self->{_error_code}} || "Unknown error!"; |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
elsif ( not $response->is_success ) # Agent error |
131
|
|
|
|
|
|
|
{ |
132
|
|
|
|
|
|
|
|
133
|
0
|
|
0
|
|
|
0
|
$self->{_error} = $response->status_line || "Unknown error!"; |
134
|
0
|
|
|
|
|
0
|
$self->{_error_code} = $response->code; |
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
else # this is logically impossible to reach |
137
|
|
|
|
|
|
|
{ |
138
|
0
|
|
|
|
|
0
|
confess "Unhandled error"; |
139
|
|
|
|
|
|
|
} |
140
|
0
|
|
|
|
|
0
|
return undef; |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
sub api_mode : method { |
144
|
0
|
|
|
0
|
1
|
0
|
return $_[0]->{_api_mode}; |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
sub error : method { |
148
|
0
|
|
|
0
|
1
|
0
|
return $_[0]->{_error}; |
149
|
|
|
|
|
|
|
} |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
sub error_code : method { |
152
|
0
|
|
|
0
|
1
|
0
|
return $_[0]->{_error_code}; |
153
|
|
|
|
|
|
|
} |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
sub _arguments : method { |
156
|
2
|
|
|
2
|
|
27
|
my $self = shift; |
157
|
2
|
|
|
|
|
8
|
my @uri = ( "wl_errorstyle", 1 ); |
158
|
|
|
|
|
|
|
|
159
|
2
|
50
|
|
|
|
8
|
croak "No data given to translate" unless $self->data =~ /\w/; |
160
|
2
|
50
|
|
|
|
22
|
croak "No srclang set" unless $self->srclang; |
161
|
2
|
50
|
|
|
|
18
|
croak "No trglang set" unless $self->trglang; |
162
|
|
|
|
|
|
|
|
163
|
2
|
|
|
|
|
17
|
for my $arg ( qw( password srclang trglang mimetype srcenc trgenc |
164
|
|
|
|
|
|
|
data dictno gloss) ) |
165
|
|
|
|
|
|
|
{ |
166
|
18
|
100
|
|
|
|
128
|
next unless $self->$arg; |
167
|
8
|
|
|
|
|
56
|
push @uri, "wl_$arg", $self->$arg(); # arg pairs for HRC::POST |
168
|
|
|
|
|
|
|
} |
169
|
2
|
50
|
|
|
|
31
|
return wantarray ? @uri : \@uri; # HRC::POST handles encoding args |
170
|
|
|
|
|
|
|
} |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
1; |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
__END__ |