AtD.pm

filter: Statementstmt 97.1% 67 / 69 Branchbran 75.0% 3 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 14 / 14 Podpod 75.0% 3 / 4 totaltotal 95.6% 87 / 91 SCAR 11.0
CC
3
Cov
96%
_atd
3.0
BEGIN
1.0
BEGIN
1.0
CRAP 3.0
« AtD/Error.pm ↑ Summary AtD/Exceptions.pm »
1 #
2 # This file is part of Lingua-AtD
3 #
4 # This software is copyright (c) 2011 by David L. Day.
5 #
6 # This is free software; you can redistribute it and/or modify it under
7 # the same terms as the Perl 5 programming language system itself.
8 #
9 package Lingua::AtD;
10 {
11 3 $Lingua::AtD::VERSION = '1.121570';
12 }
13 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use Class::Std;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use Lingua::AtD::Results;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶use Lingua::AtD::Scores;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 #use Lingua::AtD::Exceptions;
22 3 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 # ABSTRACT: Provides an OO wrapper for After the Deadline grammar and spelling service.
25  
26 {
27  
28 # Attributes
29 3 my %api_key_of :
30 ATTR( :init_arg<api_key> :get<api_key> :default<'Lingua-AtD'> );
31 3 my %throttle_of :
32 ATTR( :init_arg<throttle> :get<throttle> :set<throttle> :default<2> );
33 3 my %last_call_of : ATTR( :get<last_call> :default<0> );
34 3 my %service_host_of :
35 ATTR( :init_arg<host> :get<service_host> :default<'service.afterthedeadline.com'> );
36 3 my %service_port_of :
37 ATTR( :init_arg<port> :get<service_port> :default<80> );
38 3 my %service_url_of : ATTR( :get<service_url> );
39  
40 sub START {
41 3 ▶ my ( $self, $ident, $arg_ref ) = @_;
Subroutine START: called CC=1 SCAR=0.0
Pod undocumented
42  
43 # Construct the URL
44 $service_url_of{$ident} =
45 'http://'
46 . $service_host_of{$ident} . ':'
47 3 . $service_port_of{$ident} . '/';
48  
49 # Generate API Key
50 3 my $rand_hex = join "", map { unpack "H*", chr( rand(256) ) } 1 .. 16;
51 3 $api_key_of{$ident} = "Lingua-AtD-$rand_hex";
52  
53 3 return;
54 }
55  
56 sub _atd : PRIVATE {
57 9 ▶ my ( $self, $verb, $arg_ref ) = @_;
Subroutine _atd: called CC=3 SCAR=11.1
58 9 my $ident = ident($self);
59 9 my $url = $service_url_of{$ident} . $verb;
60 9 my $ua = LWP::UserAgent->new();
61 9 $ua->agent( 'Lingua::AtD/' . $Lingua::AtD::VERSION );
62  
63 # Throttle Calls. AtD throws a 503 if called too quickly.
64 9 my $remaining = $throttle_of{$ident} - ( time - $last_call_of{$ident} );
65 9 ▶ sleep($remaining) if ( $remaining > 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $remaining > 0 7 2 9
66  
67 9 my $response = $ua->post( $url, Content => [ %{$arg_ref} ] );
68  
69 9 $last_call_of{$ident} = time;
70  
71 9 ▶ if ( $response->is_error() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_error) 0 9 9
72  
73 # TODO: Implement Exceptions
74 0 my $msg = "'$url' responded with '" . $response->status_line . "'.";
75 0 croak $msg;
76  
77 # Lingua::AtD::HTTPException->throw(
78 # http_status => $response->status_line,
79 # service_url => $url,
80 # );
81 }
82  
83 9 return $response->content;
84 3 ▶ }
Subroutine BEGIN: called CC=1 SCAR=0.0
85  
86 sub check_document {
87 6 ▶ my ( $self, $text ) = @_;
Subroutine check_document: called CC=1 SCAR=0.0
Pod documented
88 6 my $ident = ident($self);
89 my $raw_response =
90 $self->_atd( 'checkDocument',
91 6 { key => $api_key_of{$ident}, data => $text } );
92 6 return Lingua::AtD::Results->new( { xml => $raw_response } );
93 }
94  
95 sub check_grammar {
96 2 ▶ my ( $self, $text ) = @_;
Subroutine check_grammar: called CC=1 SCAR=0.0
Pod documented
97 2 my $ident = ident($self);
98 my $raw_response =
99 $self->_atd( 'checkGrammar',
100 2 { key => $api_key_of{$ident}, data => $text } );
101 2 return Lingua::AtD::Results->new( { xml => $raw_response } );
102 }
103  
104 sub stats {
105 1 ▶ my ( $self, $text ) = @_;
Subroutine stats: called CC=1 SCAR=0.0
Pod documented
106 1 my $ident = ident($self);
107 my $raw_response =
108 1 $self->_atd( 'stats', { key => $api_key_of{$ident}, data => $text } );
109 1 return Lingua::AtD::Scores->new( { xml => $raw_response } );
110 }
111 }
112  
113 3 1; # Magic true value required at end of module
114  
115  
116 =pod
117  
118 =head1 NAME
119  
120 Lingua::AtD - Provides an OO wrapper for After the Deadline grammar and spelling service.
121  
122 =head1 VERSION
123  
124 version 1.121570
125  
126 =head1 SYNOPSIS
127  
128 use Lingua::AtD;
129  
130 # Create a new service proxy
131 my $atd = Lingua::AtD->new( {
132 host => 'service.afterthedeadline.com',
133 port => 80,
134 throttle => 2,
135 });
136  
137 # Run spelling and grammar checks. Returns a Lingua::AtD::Response object.
138 my $doc_check = $atd->check_document('Text to check.');
139 # Loop through reported document errors.
140 foreach my $atd_error ($doc_check->get_errors()) {
141 # Do something with...
142 print "Error string: ", $atd_error->get_string(), "\n";
143 }
144  
145 # Run only grammar checks. Essentially the same as
146 # check_document(), sans spell-check.
147 my $grmr_check = $atd->check_grammar('Text to check.');
148 # Loop through reported document errors.
149 foreach my $atd_error ($grmr_check->get_errors()) {
150 # Do something with...
151 print "Error string: ", $atd_error->get_string(), "\n";
152 }
153  
154 # Get statistics on a document. Returns a Lingua::AtD::Scores object.
155 my $atd_scores = $atd->stats('Text to check.');
156 # Loop through reported document errors.
157 foreach my $atd_metric ($atd_scores->get_metrics()) {
158 # Do something with...
159 print $atd_metric->get_type(), "/", $atd_metric->get_key(),
160 " = ", $atd_metric->get_value(), "\n";
161 }
162  
163 =head1 DESCRIPTION
164  
165 Lingua::AtD provides an OO-style interface for After the Deadline's grammar and spell checking services.
166  
167 =head1 METHODS
168  
169 =head2 new
170  
171 This constructor takes four arguments, all optional. The sample below shows the defaults.
172  
173 $atd = Lingua::AtD->new({
174 api_key => 'Lingua-AtD',
175 host => 'service.afterthedeadline.com',
176 port => 80,
177 throttle => 2,
178 });
179  
180 =over 4
181  
182 =item api_key
183  
184 API key used to access the service. Defaults to this package's name plus 32 hex digits (i.e. I<Lingua-AtD>-7b8391f59fd9fa4246b2c69cd8793b88). See the L<API Documentation|http://www.afterthedeadline.com/api.slp> for requirements.
185  
186 =item host
187  
188 Host for the AtD service. Defaults to the public host: I<service.afterthedeadline.com>. AtD's software is open source, and it's entirely possible to download and set up your own private AtD service. See the L<AtD Project website|http://open.afterthedeadline.com/> for details.
189  
190 =item port
191  
192 Port for the AtD service. Defaults to the standard http port: I<80>. AtD's software is open source, and it's entirely possible to download and set up your own private AtD service. See the L<AtD Project website|http://open.afterthedeadline.com/> for details.
193  
194 =item throttle
195  
196 There's no API documentation stating such, but testing has shown that AtD service throws a 503 error if called too quickly. This specifies the number of seconds to wait between calls. The default is 2 and seems to work fine. If you see 503 errors, consider bumping this up more.
197  
198 =back
199  
200 =head2 get_api_key
201  
202 $atd->get_api_key();
203  
204 Returns the API Key used to access the AtD service.
205  
206 =head2 get_host
207  
208 $atd->get_host();
209  
210 Returns the host of the AtD service.
211  
212 =head2 get_port
213  
214 $atd->get_port();
215  
216 Returns the port of the AtD service.
217  
218 =head2 get_service_url
219  
220 $atd->get_service();
221  
222 Returns a formatted URL for the AtD service.
223  
224 =head2 get_throttle
225  
226 $atd->get_throttle();
227  
228 Returns the number of seconds that must pass between calls to the AtD service.
229  
230 =head2 set_throttle
231  
232 $atd->set_throttle(3);
233  
234 Sets the number of seconds that must pass between calls to the AtD service.
235  
236 =head2 check_document
237  
238 $atd_results = $atd->check_document('Some text stringg in badd nneed of prufreding.');
239  
240 Invokes the document check service for some string of text and return a L<Lingua::AtD::Results> object.
241  
242 From the L<API Documentation|http://www.afterthedeadline.com/api.slp>: I<Checks a document and returns errors and suggestions>
243  
244 =head2 check_grammar
245  
246 $atd_results = $atd->check_grammar('Some text stringg in badd nneed of prufreding.');
247  
248 Invokes the grammar check service for some string of text and return a L<Lingua::AtD::Results> object. This differs from I<check_document> in that it only checks grammar and style, not spelling.
249  
250 From the L<API Documentation|http://www.afterthedeadline.com/api.slp>: I<Checks a document (sans spelling) returns errors and suggestions>
251  
252 =head2 stats
253  
254 $atd_scores = $atd->stats('Some text stringg in badd nneed of prufreding.');
255  
256 Invokes the stats service for some string of text and return a L<Lingua::AtD::Scores> object. This differs from I<check_document> in that it only checks grammar and style, not spelling.
257  
258 From the L<API Documentation|http://www.afterthedeadline.com/api.slp>: I<Returns statistics about the writing quality of a document>
259  
260 =head1 BUGS
261  
262 No known bugs.
263  
264 =head1 IRONY
265  
266 Wouldn't it be kind of funny if I had a ton of spelling/grammar/style errors in my documentation? Yeah, it would. And I bet there are. Shame on me for not running my documentation through my own module.
267  
268 =head1 SEE ALSO
269  
270 =for :list * L<Your::Module>
271 * L<Your::Package>
272  
273 See the L<API Documentation|http://www.afterthedeadline.com/api.slp> at After the Deadline's website.
274  
275 B<NB:> In the L<API Documentation|http://www.afterthedeadline.com/api.slp>, there is a fourth service called B</info.slp>. I do not plan to implement this. Each Lingua::AtD::Error supplies an informative URL when one is available. I see no reason to call this independently, but feel free to submit a patch if you find a reason.
276  
277 =head1 AUTHOR
278  
279 David L. Day <dday376@gmail.com>
280  
281 =head1 COPYRIGHT AND LICENSE
282  
283 This software is copyright (c) 2011 by David L. Day.
284  
285 This is free software; you can redistribute it and/or modify it under
286 the same terms as the Perl 5 programming language system itself.
287  
288 =cut
289  
290  
291 __END__
« AtD/Error.pm ↑ Summary AtD/Exceptions.pm »