File Coverage

blib/lib/Locale/Utils/Autotranslator/ApiMymemoryTranslatedNet.pm
Criterion Covered Total %
statement 39 39 100.0
branch 2 4 50.0
condition n/a
subroutine 11 11 100.0
pod 1 1 100.0
total 53 55 96.3


line stmt bran cond sub pod time code
1             package Locale::Utils::Autotranslator::ApiMymemoryTranslatedNet; ## no critic (TidyCode)
2            
3 2     2   5139 use strict;
  2         7  
  2         70  
4 2     2   13 use warnings;
  2         5  
  2         75  
5 2     2   1023 use HTML::Entities qw(decode_entities);
  2         13367  
  2         285  
6 2     2   1305 use HTTP::Request::Common qw(GET);
  2         43082  
  2         197  
7 2     2   757 use JSON qw(decode_json);
  2         8852  
  2         17  
8 2     2   1878 use LWP::UserAgent;
  2         51529  
  2         104  
9 2     2   29 use Moo;
  2         5  
  2         21  
10 2     2   3491 use MooX::StrictConstructor;
  2         15254  
  2         21  
11 2     2   30227 use URI;
  2         9  
  2         83  
12 2     2   824 use namespace::autoclean;
  2         12913  
  2         16  
13            
14             our $VERSION = '1.010';
15            
16             extends qw(
17             Locale::Utils::Autotranslator
18             );
19            
20             has user_agent => (
21             is => 'ro',
22             duck_type => 'request',
23             lazy => 1,
24             default => sub { LWP::UserAgent->new },
25             );
26            
27             sub translate_text {
28 11     11 1 23 my ( $self, $msgid ) = @_;
29            
30 11         264 $self->comment('translated by: api.mymemory.translated.net');
31 11         492 my $uri = URI->new('http://api.mymemory.translated.net/get');
32 11         8035 $uri->query_form(
33             q => $msgid,
34             langpair => join q{|}, $self->developer_language, $self->language,
35             );
36 11         1510 my $response = $self->user_agent->request(
37             GET
38             $uri->as_string,
39             'User-Agent' => 'Mozilla/5.0 (Windows NT 5.1; rv:26.0) Gecko/20100101 Firefox/26.0',
40             'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
41             'Accept-Language' => 'de-de,en-us;q=0.7,en;q=0.3',
42             'Accept-Encoding' => 'gzip, deflate',
43             'DNT' => 1,
44             'Connection' => 'keep-alive',
45             'Cache-Control' => 'max-age=0',
46             );
47 11 50       5158 $response->is_success
48             or die $response->status_line, "\n";
49 11         60 my $json = decode_json( $response->decoded_content );
50             $json->{responseStatus} eq '200'
51 11 50       583 or die $json->{responseDetails}, "\n";
52            
53             # Not clear why decode_entities here.
54             # Looks like bad interface.
55 11         92 return decode_entities( $json->{responseData}->{translatedText} );
56             }
57            
58             __PACKAGE__->meta->make_immutable;
59            
60             1;
61            
62             __END__