File Coverage

blib/lib/WebService/Yamli.pm
Criterion Covered Total %
statement 43 43 100.0
branch 3 4 75.0
condition 3 3 100.0
subroutine 9 9 100.0
pod 1 1 100.0
total 59 60 98.3


line stmt bran cond sub pod time code
1 1     1   22258 use strict;
  1         2  
  1         39  
2 1     1   5 use warnings;
  1         2  
  1         99  
3             package WebService::Yamli;
4              
5             # ABSTRACT: Perl wrapper for Yamli's Arabic translation service
6             our $VERSION = '0.002'; # VERSION
7              
8 1     1   7 use Carp;
  1         2  
  1         122  
9 1     1   1155 use LWP::UserAgent;
  1         64039  
  1         58  
10 1     1   16 use URI;
  1         3  
  1         33  
11 1     1   1115 use JSON;
  1         15974  
  1         9  
12 1     1   209 use Carp;
  1         1  
  1         213  
13              
14              
15             =pod
16              
17             =encoding utf8
18              
19             =head1 NAME
20              
21             WebService::Yamli - Perl wrapper for Yamli's Arabic transliteration service
22              
23             =head1 SYNOPSIS
24              
25             use WebService::Yamli;
26              
27             # non-OO:
28             my $tr = WebService::Yamli::tr('perl di 7aga la6eefa aslan');
29             say $tr; # the whole sentence transliterated
30              
31             my @tr = WebService::Yamli::tr('perl');
32             say "@tr"; # a list of candidates
33              
34              
35             =head1 DESCRIPTION
36              
37             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.
38              
39             This Module is an interface to L's API.
40              
41             =head1 IMPLEMENTATION
42              
43             It seems there's no way to feed the Yamli API more than one word, so currently each word results in a HTTP request.
44              
45             =head1 METHODS AND ARGUMENTS
46              
47             =over 4
48              
49             =item tr($arg)
50              
51             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
52              
53             =cut
54              
55             sub tr {
56 3     3 1 1750 my @words = split ' ', shift;
57              
58 3         7 my ($favorite, @candidates);
59 3         67 for my $word (@words) {
60 4         35 my $url = URI->new('http://api.yamli.com/transliterate.ashx');
61 4         10779 $url->query_form(
62             word => $word,
63             account_id => '000000',
64             prot => 'http:',
65             hostname => 'metacpan.org',
66             path => '/pod/WebService::Yamli',
67             build => '5447'
68             );
69              
70 4         1024 my $ua = LWP::UserAgent->new;
71 1     1   7 { no strict 'vars'; $ua->agent(__PACKAGE__ . "/$VERSION"); }
  1         2  
  1         284  
  4         4482  
  4         27  
72              
73 4         193 my $response = $ua->get($url);
74 4 50       877841 croak "Error: ", $response->status_line unless $response->is_success;
75              
76 4         75 my $decoded = decode_json $response->content;
77 4         138 @candidates = split /\|/, $decoded->{r};
78              
79             # strip away the candidate score
80 4         11 @candidates = map { s(/\d$)()r } @candidates;
  60         175  
81 4         137 $favorite .= $candidates[0] . ' ';
82             }
83              
84 3 100 100     29 return @candidates if @words == 1 && wantarray;
85              
86 2         10 local $/ = ' ';
87 2         6 chomp $favorite;
88 2         12 return $favorite;
89             }
90              
91              
92              
93              
94              
95             1;
96             __END__