File Coverage

blib/lib/WebService/Yamli.pm
Criterion Covered Total %
statement 40 40 100.0
branch 4 6 66.6
condition 3 3 100.0
subroutine 8 8 100.0
pod 1 1 100.0
total 56 58 96.5


line stmt bran cond sub pod time code
1 2     2   190249 use strict;
  2         27  
  2         63  
2 2     2   11 use warnings;
  2         4  
  2         127  
3             package WebService::Yamli;
4              
5             # ABSTRACT: Perl wrapper for Yamli's Arabic translation service
6             our $VERSION = '0.005'; # VERSION
7              
8 2     2   13 use Carp;
  2         4  
  2         129  
9 2     2   1456 use LWP::UserAgent;
  2         111891  
  2         88  
10 2     2   26 use URI;
  2         5  
  2         59  
11 2     2   1556 use JSON;
  2         22050  
  2         20  
12 2     2   389 use Carp;
  2         6  
  2         944  
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. Define $WebService::Yamli::HTTPS if HTTPS should be used instead.
44              
45             =cut
46              
47             our $HTTPS = 0;
48              
49             =head1 METHODS AND ARGUMENTS
50              
51             =over 4
52              
53             =item tr($arg)
54              
55             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
56              
57             =cut
58              
59             sub tr {
60 3     3 1 4139 my @words = split ' ', shift;
61 3 50       21 my $s = $HTTPS ? 's' : '';
62              
63 3         11 my ($favorite, @candidates);
64 3         11 for my $word (@words) {
65 4         49 my $url = URI->new("http$s://api.yamli.com/transliterate.ashx");
66 4         7155 $url->query_form(
67             word => $word,
68             account_id => '000006',
69             tool => 'api',
70             prot => "http$s:",
71             hostname => 'metacpan.org',
72             path => '/pod/WebService::Yamli',
73             build => '5515'
74             );
75              
76 4         1083 my $ua = LWP::UserAgent->new;
77 4         4111 $ua->agent(__PACKAGE__ . "/" . $WebService::Yamli::VERSION);
78              
79 4         266 my $response = $ua->get($url);
80 4 50       1239739 croak "Error: ", $response->status_line unless $response->is_success;
81              
82 4         83 my $decoded = decode_json $response->content;
83 4         185 @candidates = split /\|/, $decoded->{r};
84              
85             # strip away the candidate score
86 4         16 @candidates = map { s(/\d$)()r } @candidates;
  60         322  
87 4         191 $favorite .= $candidates[0] . ' ';
88             }
89              
90 3 100 100     46 return @candidates if @words == 1 && wantarray;
91              
92 2         13 local $/ = ' ';
93 2         8 chomp $favorite;
94 2         16 return $favorite;
95             }
96              
97              
98              
99              
100              
101             1;
102             __END__