blib/lib/Yandex/Translate.pm

filter: Statementstmt 43.0% 53 / 123 Branchbran 9.7% 9 / 92 Conditioncond 10.5% 4 / 38 MC/DCmcdc 0.0% 0 / 25 Subroutinesub 50.0% 11 / 22 Podpod 100% 14 / 14 totaltotal 28.9% 91 / 314 CC 59 SCAR 42.3
CC
59
Cov
26%
translate
240.0
detect_lang
90.0
get_langs_list
56.0
CRAP 68.8
↑ Summary
1 package Yandex::Translate;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 ▶use HTML::Entities qw{encode_entities};
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use POSIX qw{:locale_h};
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use URI::Escape qw{uri_escape_utf8};
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 #
14 # The “my” keyword is on a separate line so that the VERSION_FROM attribute
15 # of ExtUtils::MakeMaker->WriteMakefile() will accurately detect $VERSION.
16 #
17 my
18 5 $VERSION = '1.002';
19  
20 #
21 # See https://tech.yandex.ru/translate/doc/dg/concepts/api-overview-docpage/
22 # for the supported language codes.
23 #
24 5 my %valid_lang = map { $_ => 1 } qw{
25 az sq am en ar hy af eu ba be bn my bg bs cy hu vi ht gl nl
26 mrj el ka gu da he yi id ga it is es kk kn ca ky zh ko xh km
27 lo la lv lt lb mg ms
28 ml mt mk mi mr mhr mn de ne no pa pap fa pl pt ro ru ceb sr si
29 sk sl sw su tg th tl ta tt te tr udm uz uk ur fi fr hi hr cs
30 sv gd et eo jv ja
31 };
32  
33 5 my %valid_format = map { $_ => 1 } qw { plain html };
34  
35 5 my %valid_options = map { $_ => 1 } qw { 1 };
36  
37 5 my %valid_default_ui = map { $_ => 1 } qw{ en ru tr };
38  
39 #
40 # Set the default UI to Russian if the locale is Russian;
41 # Set the default UI to Turkish if the locale is Turkish;
42 # otherwise, set it to English.
43 #
44 5 ▶(my $default_ui = setlocale(LC_CTYPE) || 'en') =~ s/_.*$//;
Condition50%1/2
setlocale(0) || 'en'
l!l
50
MC/DC0%0/1
setlocale(0) || 'en'
setlocale(0)
Truth table50%1/2
A
A= setlocale(0)
Aresult
11
00
45 5 ▶$default_ui = 'en' if (!exists $valid_default_ui{$default_ui});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $valid_default_ui{$default_ui} 5 0 5
46  
47 sub new
48 1 ▶{
Subroutine new: called CC=3 SCAR=12.0
Pod documented
49 1 my $class = shift;
50 1 ▶ my $self = {
Condition33%1/3
shift() || $default_ui
l!l&&r!l&&!r
010
MC/DC0%0/2
shift() || $default_ui
shift() $default_ui
Truth table33%1/3
A || B
A= shift()B= $default_ui
ABresult
1X1
011
000
51 _key_ => shift,
52 _text_ => shift,
53 _from_lang_ => shift,
54 _to_lang_ => shift,
55 _ui_ => shift || $default_ui,
56 _hint_ => shift,
57 _format_ => shift,
58 _options_ => shift,
59 _base_ => 'https://translate.yandex.net/api/v1.5/tr.json',
60 _post_ => undef,
61 _http_ => HTTP::Tiny->new
62 };
63  
64 1 ▶ $self->{_text_} = uri_escape_utf8($self->{_text_}) if (defined $self->{_text_});
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"_text_"} 0 1 1
65  
66 1 return bless $self, $class;
67 }
68  
69 sub set_key
70 0 ▶{
Subroutine set_key: not called CC=2 SCAR=17.9
Pod documented
71 0 my ( $self, $key ) = @_;
72 0 ▶ $self->{_key_} = $key if (defined $key);
Branch: 1 branch
BranchTrueFalseTotal
if defined $key 0 0 0
73 }
74  
75 sub set_ui
76 3 ▶{
Subroutine set_ui: called CC=3 SCAR=13.1
Pod documented
77 3 my ( $self, $ui ) = @_;
78 3 ▶ $self->{_ui_} = (defined $ui && exists $valid_lang{$ui}) ? $ui : $default_ui;
Branch: 1 branch
BranchTrueFalseTotal
defined $ui && exists $valid_lang{$ui} ? : 3 0 3
Condition33%1/3
defined $ui and exists $valid_lang{$ui}
!ll&&!rl&&r
003
MC/DC0%0/2
defined $ui and exists $valid_lang{$ui}
defined $ui exists $valid_lang{$ui}
Truth table33%1/3
A and B
A= defined $uiB= exists $valid_lang{$ui}
ABresult
0X0
100
111
79 }
80  
81 sub set_default_ui
82 0 ▶{
Subroutine set_default_ui: not called CC=3 SCAR=24.8
Pod documented
83 0 my ( $self, $this_default_ui ) = @_;
84 0 ▶ $default_ui = $this_default_ui if (defined $this_default_ui && exists $valid_default_ui{$this_default_ui});
Branch: 1 branch
BranchTrueFalseTotal
if defined $this_default_ui and exists $valid_default_ui{$this_default_ui} 0 0 0
Condition0%0/3
defined $this_default_ui and exists $valid_default_ui{$this_default_ui}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $this_default_ui and exists $valid_default_ui{$this_default_ui}
defined $this_default_ui exists $valid_default_ui{$this_default_ui}
Truth table0%0/3
A and B
A= defined $this_default_uiB= exists $valid_default_ui{$this_default_ui}
ABresult
0X0
100
111
85 }
86  
87 #
88 # Get a list of supported translation directions.
89 #
90 sub get_langs_list
91 0 ▶{
Subroutine get_langs_list: not called CC=7 SCAR=40.3
Pod documented
92 0 my $self = shift;
93
94 0 my $query = '/getLangs?';
95 0 $self->{_post_} = 'key='.$self->{_key_}.'&ui='.$self->{_ui_};
96 0 my $response = $self->{_http_}->get($self->{_base_} . $query . $self->{_post_});
97  
98 0 ▶ die "Invalid API key\n" if ($response->{status} eq '401');
Branch: 1 branch
BranchTrueFalseTotal
if $response->{"status"} eq 401 0 0 0
99 0 ▶ die "Blocked API key\n" if ($response->{status} eq '402');
Branch: 1 branch
BranchTrueFalseTotal
if $response->{"status"} eq 402 0 0 0
100 0 ▶ die "Failed to get list of supported languages! (response code $response->{status})\n" unless ($response->{success});
Branch: 1 branch
BranchTrueFalseTotal
unless $response->{"success"} 0 0 0
101  
102 0 ▶ if (defined wantarray && length $response->{content}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined wantarray and length $response->{"content"}) 0 0 0
Condition0%0/3
defined wantarray and length $response->{"content"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined wantarray and length $response->{"content"}
defined wantarray length $response->{"content"}
Truth table0%0/3
A and B
A= defined wantarrayB= length $response->{"content"}
ABresult
0X0
100
111
103 0 my $json_respond = JSON->new->utf8->decode($response->{content});
104 0 ▶ return (wantarray) ? @{ $json_respond->{dirs} } : scalar(@{ $json_respond->{dirs} });
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
105 }
106 }
107  
108 sub set_text
109 0 ▶{
Subroutine set_text: not called CC=2 SCAR=17.9
Pod documented
110 0 my ( $self, $text ) = @_;
111 0 ▶ $self->{_text_} = uri_escape_utf8($text) if (defined $text);
Branch: 1 branch
BranchTrueFalseTotal
if defined $text 0 0 0
112 }
113  
114 sub set_hint
115 0 ▶{
Subroutine set_hint: not called CC=6 SCAR=37.4
Pod documented
116 0 my ( $self, $hint ) = @_;
117 0 my @valid_hint_lang;
118 0 ▶ if (defined $hint && ref($hint) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hint and ref $hint eq "ARRAY") 0 0 0
Condition0%0/3
defined $hint and ref $hint eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $hint and ref $hint eq "ARRAY"
defined $hint ref $hint eq "ARRAY"
Truth table0%0/3
A and B
A= defined $hintB= ref $hint eq "ARRAY"
ABresult
0X0
100
111
119 0 for (@{ $hint }) {
120 0 ▶ push @valid_hint_lang, $_ if (exists $valid_lang{$_});
Branch: 1 branch
BranchTrueFalseTotal
if exists $valid_lang{$_} 0 0 0
121 }
122 }
123 0 ▶ $self->{_hint_} = (@valid_hint_lang) ? [ @valid_hint_lang ] : undef;
Branch: 1 branch
BranchTrueFalseTotal
@valid_hint_lang ? : 0 0 0
124 }
125  
126 sub detect_lang
127 0 ▶{
Subroutine detect_lang: not called CC=9 SCAR=45.0
Pod documented
128 0 my $self = shift;
129  
130 0 my $query = '/detect?';
131 0 $self->{_post_} = 'key='.$self->{_key_}.'&text='.$self->{_text_};
132 0 ▶ $self->{_post_} .= '&hint='.join(',', @{ $self->{_hint_} }) if (defined $self->{_hint_});
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"_hint_"} 0 0 0
133 0 my $response = $self->{_http_}->get($self->{_base_} . $query . $self->{_post_});
134  
135 0 ▶ die "Invalid API key\n" if ($response->{status} eq '401');
Branch: 1 branch
BranchTrueFalseTotal
if $response->{"status"} eq 401 0 0 0
136 0 ▶ die "Blocked API key\n" if ($response->{status} eq '402');
Branch: 1 branch
BranchTrueFalseTotal
if $response->{"status"} eq 402 0 0 0
137 0 ▶ die "Exceeded the daily limit on the amount of translated text\n" if ($response->{status} eq '404');
Branch: 1 branch
BranchTrueFalseTotal
if $response->{"status"} eq 404 0 0 0
138 0 ▶ die "Failed to detect the language! (response code $response->{status})\n" unless ($response->{success});
Branch: 1 branch
BranchTrueFalseTotal
unless $response->{"success"} 0 0 0
139  
140 0 ▶ if (defined wantarray && length $response->{content}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined wantarray and length $response->{"content"}) 0 0 0
Condition0%0/3
defined wantarray and length $response->{"content"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined wantarray and length $response->{"content"}
defined wantarray length $response->{"content"}
Truth table0%0/3
A and B
A= defined wantarrayB= length $response->{"content"}
ABresult
0X0
100
111
141 0 my $json_respond = JSON->new->utf8->decode($response->{content});
142 0 ▶ return (wantarray) ? ($json_respond->{lang}) : $json_respond->{lang};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
143 }
144 }
145  
146 sub set_from_lang
147 0 ▶{
Subroutine set_from_lang: not called CC=3 SCAR=24.8
Pod documented
148 0 my ( $self, $from_lang ) = @_;
149 0 ▶ $self->{_from_lang_} = $from_lang if (!defined $from_lang || exists $valid_lang{$from_lang});
Branch: 1 branch
BranchTrueFalseTotal
if not defined $from_lang or exists $valid_lang{$from_lang} 0 0 0
Condition0%0/3
not defined $from_lang or exists $valid_lang{$from_lang}
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $from_lang or exists $valid_lang{$from_lang}
not defined $from_lang exists $valid_lang{$from_lang}
Truth table0%0/3
not(A) or B
A= not defined $from_langB= exists $valid_lang{$from_lang}
ABresult
1X1
011
000
150 }
151  
152 sub set_to_lang
153 0 ▶{
Subroutine set_to_lang: not called CC=3 SCAR=24.8
Pod documented
154 0 my ( $self, $to_lang ) = @_;
155 0 ▶ $self->{_to_lang_} = $to_lang if (defined $to_lang && exists $valid_lang{$to_lang});
Branch: 1 branch
BranchTrueFalseTotal
if defined $to_lang and exists $valid_lang{$to_lang} 0 0 0
Condition0%0/3
defined $to_lang and exists $valid_lang{$to_lang}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $to_lang and exists $valid_lang{$to_lang}
defined $to_lang exists $valid_lang{$to_lang}
Truth table0%0/3
A and B
A= defined $to_langB= exists $valid_lang{$to_lang}
ABresult
0X0
100
111
156 }
157  
158 sub set_format
159 0 ▶{
Subroutine set_format: not called CC=3 SCAR=24.8
Pod documented
160 0 my ( $self, $format ) = @_;
161 0 ▶ $self->{_format_} = $format if (!defined $format || exists $valid_format{$format});
Branch: 1 branch
BranchTrueFalseTotal
if not defined $format or exists $valid_format{$format} 0 0 0
Condition0%0/3
not defined $format or exists $valid_format{$format}
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $format or exists $valid_format{$format}
not defined $format exists $valid_format{$format}
Truth table0%0/3
not(A) or B
A= not defined $formatB= exists $valid_format{$format}
ABresult
1X1
011
000
162 }
163  
164 sub set_options
165 0 ▶{
Subroutine set_options: not called CC=3 SCAR=24.8
Pod documented
166 0 my ( $self, $options ) = @_;
167 0 ▶ $self->{_options_} = $options if (!defined $options || exists $valid_options{$options});
Branch: 1 branch
BranchTrueFalseTotal
if not defined $options or exists $valid_options{$options} 0 0 0
Condition0%0/3
not defined $options or exists $valid_options{$options}
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $options or exists $valid_options{$options}
not defined $options exists $valid_options{$options}
Truth table0%0/3
not(A) or B
A= not defined $optionsB= exists $valid_options{$options}
ABresult
1X1
011
000
168 }
169  
170 sub translate
171 0 ▶{
Subroutine translate: not called CC=15 SCAR=54.8
Pod documented
172 0 my $self = shift;
173  
174 0 my $query = '/translate?';
175 0 ▶ my $lang = (defined $self->{_from_lang_}) ? $self->{_from_lang_}.'-'.$self->{_to_lang_} : $self->{_to_lang_};
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'_from_lang_'} ? : 0 0 0
176 0 $self->{_post_} = 'key='.$self->{_key_}.'&text='.$self->{_text_}.'&lang='.$lang;
177 0 ▶ $self->{_post_} .= '&format='.$self->{_format_} if (defined $self->{_format_});
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"_format_"} 0 0 0
178 0 ▶ $self->{_post_} .= '&options='.$self->{_options_} if (defined $self->{_options_});
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"_options_"} 0 0 0
179 0 my $response = $self->{_http_}->get($self->{_base_} . $query . $self->{_post_});
180  
181 0 ▶ die "Invalid API key\n" if ($response->{status} eq '401');
Branch: 1 branch
BranchTrueFalseTotal
if $response->{"status"} eq 401 0 0 0
182 0 ▶ die "Blocked API key\n" if ($response->{status} eq '402');
Branch: 1 branch
BranchTrueFalseTotal
if $response->{"status"} eq 402 0 0 0
183 0 ▶ die "Exceeded the daily limit on the amount of translated text\n" if ($response->{status} eq '404');
Branch: 1 branch
BranchTrueFalseTotal
if $response->{"status"} eq 404 0 0 0
184 0 ▶ die "Exceeded the maximum text size\n" if ($response->{status} eq '413');
Branch: 1 branch
BranchTrueFalseTotal
if $response->{"status"} eq 413 0 0 0
185 0 ▶ die "The text cannot be translated\n" if ($response->{status} eq '422');
Branch: 1 branch
BranchTrueFalseTotal
if $response->{"status"} eq 422 0 0 0
186 0 ▶ die "The specified translation direction is not supported\n" if ($response->{status} eq '501');
Branch: 1 branch
BranchTrueFalseTotal
if $response->{"status"} eq 501 0 0 0
187 0 ▶ die "Failed to translate text! (response code $response->{status})\n" unless ($response->{success});
Branch: 1 branch
BranchTrueFalseTotal
unless $response->{"success"} 0 0 0
188  
189 0 ▶ if (defined wantarray && length $response->{content}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined wantarray and length $response->{"content"}) 0 0 0
Condition0%0/3
defined wantarray and length $response->{"content"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined wantarray and length $response->{"content"}
defined wantarray length $response->{"content"}
Truth table0%0/3
A and B
A= defined wantarrayB= length $response->{"content"}
ABresult
0X0
100
111
190 0 my $json_respond = JSON->new->utf8->decode($response->{content});
191 0 ▶ if (defined $self->{_options_}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'_options_'}) { } 0 0 0
192 0 return ($json_respond->{detected}->{lang}, $json_respond->{text}[0]);
193 }
194 else {
195 0 ▶ return (wantarray) ? ($json_respond->{text}[0]) : $json_respond->{text}[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
196 }
197 }
198 }
199  
200 #
201 # See §2.7 of Пользовательское соглашение сервиса «API Яндекс.Переводчик»
202 # at https://yandex.ru/legal/translate_api/
203 #
204 # See §2.7 of Terms of Use of API Yandex.Translate Service
205 # at https://yandex.com/legal/translate_api/
206 #
207  
208 sub get_yandex_technology_reference
209 3 ▶{
Subroutine get_yandex_technology_reference: called CC=10 SCAR=30.7
Pod documented
210 3 my ( $self, $attribute ) = @_;
211 3 ▶ if (defined wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined wantarray) 3 0 3
212 3 my %yandex_attribute;
213 3 ▶ if (defined $attribute && ref($attribute) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $attribute and ref $attribute eq "HASH") 0 3 3
Condition33%1/3
defined $attribute and ref $attribute eq "HASH"
!ll&&!rl&&r
300
MC/DC0%0/2
defined $attribute and ref $attribute eq "HASH"
defined $attribute ref $attribute eq "HASH"
Truth table33%1/3
A and B
A= defined $attributeB= ref $attribute eq "HASH"
ABresult
0X0
100
111
214 0 ▶ while (my ( $key, $value ) = each %{ $attribute }) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$attribute;}) 0 0 0
215 0 ▶ $yandex_attribute{$key} = $key.'="'.encode_entities($value).'"' if (lc $key ne 'href');
Branch: 1 branch
BranchTrueFalseTotal
if lc $key ne "href" 0 0 0
216 }
217 }
218  
219 #
220 # Sort %yandex_attribute so that the same $yandex_attributes value
221 # will consistently be produced for a given $attribute hash.
222 #
223 0 ▶ my $yandex_attributes = (%yandex_attribute) ? ' '.join(' ', map { $yandex_attribute{$_} } sort { $a cmp $b } keys %yandex_attribute) : '';
Branch: 1 branch
BranchTrueFalseTotal
%yandex_attribute ? : 0 3 3
224  
225 3 my %yandex_url = (
226 ru => 'http://translate.yandex.ru/',
227 en => 'http://translate.yandex.com/',
228 tr => 'http://translate.yandex.com.tr/',
229 );
230 3 my %yandex_text = (
231 ru => 'Переведено сервисом Яндекс.Переводчик',
232 en => 'Powered by Yandex.Translate',
233 tr => 'Tarafından desteklenmektedir Yandex.Çeviri',
234 );
235 3 ▶ my $yandex_url = (exists $yandex_url{$self->{_ui_}}) ? $yandex_url{$self->{_ui_}} : $yandex_url{$default_ui};
Branch: 1 branch
BranchTrueFalseTotal
exists $yandex_url{$self->{'_ui_'}} ? : 3 0 3
236 3 ▶ my $yandex_text = (exists $yandex_text{$self->{_ui_}}) ? $yandex_text{$self->{_ui_}} : $yandex_text{$default_ui};
Branch: 1 branch
BranchTrueFalseTotal
exists $yandex_text{$self->{'_ui_'}} ? : 3 0 3
237 3 my $yandex_element = '<a'.$yandex_attributes.' href="'.$yandex_url.'">'.$yandex_text.'</a>';
238 3 ▶ return (wantarray) ? ($yandex_element) : $yandex_element;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 3 3
239 }
240 }
241  
242 5 1;
243 __END__
↑ Summary