File Coverage

blib/lib/WebService/Yamli.pm
Criterion Covered Total %
statement 45 45 100.0
branch 3 4 75.0
condition 3 3 100.0
subroutine 10 10 100.0
pod 1 1 100.0
total 62 63 98.4


line stmt bran cond sub pod time code
1 1     1   15932 use strict;
  1         2  
  1         26  
2 1     1   3 use warnings;
  1         2  
  1         44  
3             package WebService::Yamli;
4              
5             # ABSTRACT: Perl wrapper for Yamli's Arabic translation service
6             our $VERSION = '0.003'; # VERSION
7              
8 1     1   15 use v5.14;
  1         3  
9              
10 1     1   4 use Carp;
  1         1  
  1         63  
11 1     1   620 use LWP::UserAgent;
  1         39613  
  1         51  
12 1     1   8 use URI;
  1         2  
  1         29  
13 1     1   1133 use JSON;
  1         15547  
  1         5  
14 1     1   219 use Carp;
  1         2  
  1         213  
15              
16              
17             =pod
18              
19             =encoding utf8
20              
21             =head1 NAME
22              
23             WebService::Yamli - Perl wrapper for Yamli's Arabic transliteration service
24              
25             =head1 SYNOPSIS
26              
27             use WebService::Yamli;
28              
29             # non-OO:
30             my $tr = WebService::Yamli::tr('perl di 7aga la6eefa aslan');
31             say $tr; # the whole sentence transliterated
32              
33             my @tr = WebService::Yamli::tr('perl');
34             say "@tr"; # a list of candidates
35              
36              
37             =head1 DESCRIPTION
38              
39             Franco-Arabic, aka Chat Arabic, Arabizy, is a transliteration of Arabic, commonly used on the internet. It restricts itself to the ASCII charset and substitutes numbers for the Arabic characters which have no equivalent in Latin.
40              
41             This Module is an interface to L's API.
42              
43             =head1 IMPLEMENTATION
44              
45             It seems there's no way to feed the Yamli API more than one word, so currently each word results in a HTTP request.
46              
47             =head1 METHODS AND ARGUMENTS
48              
49             =over 4
50              
51             =item tr($arg)
52              
53             Transliterates argument. Returns transliterated string, except if input is a single word and subroutine is in list context, in that case it returns a candidate list
54              
55             =cut
56              
57             sub tr {
58 3     3 1 2044 my @words = split ' ', shift;
59              
60 3         8 my ($favorite, @candidates);
61 3         7 for my $word (@words) {
62 4         27 my $url = URI->new('http://api.yamli.com/transliterate.ashx');
63 4         6949 $url->query_form(
64             word => $word,
65             account_id => '000000',
66             prot => 'http:',
67             hostname => 'metacpan.org',
68             path => '/pod/WebService::Yamli',
69             build => '5447'
70             );
71              
72 4         745 my $ua = LWP::UserAgent->new;
73 1     1   8 { no strict 'vars'; $ua->agent(__PACKAGE__ . "/$VERSION"); }
  1         1  
  1         282  
  4         3051  
  4         22  
74              
75 4         192 my $response = $ua->get($url);
76 4 50       1755137 croak "Error: ", $response->status_line unless $response->is_success;
77              
78 4         135 my $decoded = decode_json $response->content;
79 4         191 @candidates = split /\|/, $decoded->{r};
80              
81             # strip away the candidate score
82 4         13 @candidates = map { s(/\d$)()r } @candidates;
  60         182  
83 4         168 $favorite .= $candidates[0] . ' ';
84             }
85              
86 3 100 100     38 return @candidates if @words == 1 && wantarray;
87              
88 2         19 local $/ = ' ';
89 2         9 chomp $favorite;
90 2         22 return $favorite;
91             }
92              
93              
94              
95              
96              
97             1;
98             __END__