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   3477 use strict;
  2         3  
  2         47  
4 2     2   8 use warnings;
  2         3  
  2         43  
5 2     2   778 use HTML::Entities qw(decode_entities);
  2         8941  
  2         155  
6 2     2   798 use HTTP::Request::Common qw(GET);
  2         36144  
  2         98  
7 2     2   503 use JSON qw(decode_json);
  2         6347  
  2         10  
8 2     2   1163 use LWP::UserAgent;
  2         37073  
  2         54  
9 2     2   11 use Moo;
  2         5  
  2         12  
10 2     2   981 use MooX::StrictConstructor;
  2         9962  
  2         14  
11 2     2   17867 use URI;
  2         4  
  2         40  
12 2     2   693 use namespace::autoclean;
  2         8641  
  2         11  
13            
14             our $VERSION = '1.002';
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 20 my ( $self, $msgid ) = @_;
29            
30 11         200 $self->comment('translated by: api.mymemory.translated.net');
31 11         358 my $uri = URI->new('http://api.mymemory.translated.net/get');
32 11         5776 $uri->query_form(
33             q => $msgid,
34             langpair => join q{|}, $self->developer_language, $self->language,
35             );
36 11         1119 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       3877 $response->is_success
48             or die $response->status_line, "\n";
49 11         46 my $json = decode_json( $response->decoded_content );
50             $json->{responseStatus} eq '200'
51 11 50       479 or die $json->{responseDetails}, "\n";
52            
53             # Not clear why decode_entities here.
54             # Looks like bad interface.
55 11         72 return decode_entities( $json->{responseData}->{translatedText} );
56             }
57            
58             __PACKAGE__->meta->make_immutable;
59            
60             1;
61            
62             __END__