AtD/Results.pm

filter: Statementstmt 96.2% 51 / 53 Branchbran 60.0% 6 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 11 / 11 Podpod 75.0% 3 / 4 totaltotal 91.0% 71 / 78 SCAR 20.9
CC
8
Cov
90%
START
5.2
has_server_exception
2.1
get_errors
2.0
CRAP 8.1
↑ Summary AtD/Scores.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::Results;
10 {
11 5 $Lingua::AtD::Results::VERSION = '1.121570';
12 }
13 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 5 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 5 ▶use Lingua::AtD::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 5 ▶use Lingua::AtD::Exceptions;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 5 ▶use Class::Std;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 # ABSTRACT: Encapsulate conversion of XML from /checkDocument or /checkGrammar call to Error objects.
22  
23 {
24  
25 # Attributes
26 5 my %xml_of : ATTR( :init_arg<xml> :get<xml> );
27 5 my %server_exception_of : ATTR( :get<server_exception> );
28 5 my %error_count_of : ATTR( :get<error_count> :default<0> );
29 5 my %errors_of : ATTR();
30  
31 sub START {
32 9 ▶ my ( $self, $ident, $arg_ref ) = @_;
Subroutine START: called CC=5 SCAR=16.4
Pod undocumented
33 9 my @atd_errors = ();
34  
35 9 my $parser = XML::LibXML->new();
36 9 my $dom = $parser->load_xml( string => $xml_of{$ident} );
37  
38 # Check for server message.
39 9 ▶ if ( $dom->exists('/results/message') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dom->exists("/results/message")) 0 9 9
40 0 $server_exception_of{$ident} = $dom->findvalue('/results/message');
41  
42 # TODO: Implement Exceptions
43 0 croak $server_exception_of{$ident};
44  
45 # Lingua::AtD::ServiceException->throw(
46 # service_message => $server_exception_of{$ident} );
47 }
48  
49 9 foreach my $error_node ( $dom->findnodes('/results/error') ) {
50 2 my @options = ();
51 2 foreach
52 my $option_node ( $error_node->findnodes('./suggestions/option') )
53 {
54 2 push( @options, $option_node->string_value );
55 }
56 2 ▶ my $url =
Branch: 1 branch
BranchTrueFalseTotal
$error_node->exists('url') ? : 2 0 2
57 ( $error_node->exists('url') )
58 ? $error_node->findvalue('url')
59 : undef;
60 2 my $atd_error = Lingua::AtD::Error->new(
61 {
62 string => $error_node->findvalue('string'),
63 description => $error_node->findvalue('description'),
64 precontext => $error_node->findvalue('precontext'),
65 suggestions => [@options],
66 type => $error_node->findvalue('type'),
67 url => $url,
68 }
69 );
70 2 push( @atd_errors, $atd_error );
71 2 $error_count_of{$ident} += 1;
72 }
73 9 $errors_of{$ident} = [@atd_errors];
74  
75 9 return;
76 }
77  
78 sub has_server_exception {
79 1 ▶ my $self = shift;
Subroutine has_server_exception: called CC=2 SCAR=7.2
Pod documented
80 1 ▶ return defined( $server_exception_of{ ident($self) } ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $server_exception_of{builtin::refaddr($self)} ? : 0 1 1
81 }
82  
83 sub has_errors {
84 6 ▶ my $self = shift;
Subroutine has_errors: called CC=2 SCAR=6.9
Pod documented
85  
86 # return defined( $errors_of{ ident($self) } ) ? 1 : 0;
87 6 ▶ return ( $error_count_of{ ident($self) } > 0 ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$error_count_of{builtin::refaddr($self)} > 0 ? : 2 4 6
88 }
89  
90 sub get_errors {
91 1 ▶ my $self = shift;
Subroutine get_errors: called CC=2 SCAR=7.1
Pod documented
92 return $self->has_errors()
93 1 ▶ ? @{ $errors_of{ ident($self) } }
Branch: 1 branch
BranchTrueFalseTotal
$self->has_errors ? : 1 0 1
94 : undef;
95 }
96  
97 }
98  
99 5 1; # Magic true value required at end of module
100  
101  
102 =pod
103  
104 =head1 NAME
105  
106 Lingua::AtD::Results - Encapsulate conversion of XML from /checkDocument or /checkGrammar call to Error objects.
107  
108 =head1 VERSION
109  
110 version 1.121570
111  
112 =head1 SYNOPSIS
113  
114 use Lingua::AtD;
115  
116 # Create a new service proxy
117 my $atd = Lingua::AtD->new( {
118 host => 'service.afterthedeadline.com',
119 port => 80
120 throttle => 2,
121 });
122  
123 # Run spelling and grammar checks. Returns a Lingua::AtD::Response object.
124 my $doc_check = $atd->check_document('Text to check.');
125 # Loop through reported document errors.
126 foreach my $atd_error ($doc_check->get_errors()) {
127 # Do something with...
128 print "Error string: ", $atd_error->get_string(), "\n";
129 }
130  
131 # Run only grammar checks. Essentially the same as
132 # check_document(), sans spell-check.
133 my $grmr_check = $atd->check_grammar('Text to check.');
134 # Loop through reported document errors.
135 foreach my $atd_error ($grmr_check->get_errors()) {
136 # Do something with...
137 print "Error string: ", $atd_error->get_string(), "\n";
138 }
139  
140 # Get statistics on a document. Returns a Lingua::AtD::Scores object.
141 my $atd_scores = $atd->stats('Text to check.');
142 # Loop through reported document errors.
143 foreach my $atd_metric ($atd_scores->get_metrics()) {
144 # Do something with...
145 print $atd_metric->get_type(), "/", $atd_metric->get_key(),
146 " = ", $atd_metric->get_value(), "\n";
147 }
148  
149 =head1 DESCRIPTION
150  
151 Encapsulates conversion of the XML response from the AtD server into a list of spelling/grammar/style error objects (L<Lingua::AtD::Error>).
152  
153 =head1 METHODS
154  
155 =head2 new
156  
157 # Possible, but not likely
158 my $atd_results = Lingua::AtD::Results->new($xml_response);
159 foreach my $atd_error ($atd_results->get_errors()) {
160 # Do something really fun...
161 }
162  
163 Lingua::AtD::Results objects should only ever be created from method calls to L<Lingua::AtD>. However, if you have saved XML responses from prior calls to AtD, you can use this object to convert those responses into PERL objects. I won't stop you.
164  
165 See the L<SYNOPSIS> for typical usage.
166  
167 =head2 has_server_exception
168  
169 Convenience method to see if the AtD server returned an error message.
170  
171 =head2 get_server_exception
172  
173 Exception message from the server.
174  
175 =head2 has_errors
176  
177 Convenience method to see if the XML response from AtD actually contained any spelling/grammar/style errors. These are not exceptions (see L<get_server_exception>). These are expected, and in fact are what you've asked for.
178  
179 =head2 get_error_count
180  
181 Returns the number of spelling/grammar/style errors detected.
182  
183 =head2 get_errors
184  
185 Returns a list of spelling/grammar/style errors as L<Lingua::AtD::Error> objects.
186  
187 =head2 get_xml
188  
189 Returns a string containing the raw XML response from the AtD service call.
190  
191 =head1 SEE ALSO
192  
193 See the L<API Documentation|http://www.afterthedeadline.com/api.slp> at After the Deadline's website.
194  
195 =head1 AUTHOR
196  
197 David L. Day <dday376@gmail.com>
198  
199 =head1 COPYRIGHT AND LICENSE
200  
201 This software is copyright (c) 2011 by David L. Day.
202  
203 This is free software; you can redistribute it and/or modify it under
204 the same terms as the Perl 5 programming language system itself.
205  
206 =cut
207  
208  
209 __END__
↑ Summary AtD/Scores.pm »