AtD/Scores.pm

filter: Statementstmt 95.9% 47 / 49 Branchbran 50.0% 4 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 11 / 11 Podpod 75.0% 3 / 4 totaltotal 90.2% 65 / 72 SCAR 18.0
CC
6
Cov
89%
START
3.1
has_server_exception
2.1
has_metrics
2.1
CRAP 6.0
« AtD/Results.pm ↑ Summary AtD/Error.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::Scores;
10 {
11 5 $Lingua::AtD::Scores::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::Metric;
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 /stats call to Metric 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 %metric_count_of : ATTR( :get<metric_count> :default<0> );
29 5 my %metrics_of : ATTR();
30  
31 sub START {
32 2 ▶ my ( $self, $ident, $arg_ref ) = @_;
Subroutine START: called CC=3 SCAR=11.2
Pod undocumented
33 2 my @atd_metrics = ();
34  
35 2 my $parser = XML::LibXML->new();
36 2 my $dom = $parser->load_xml( string => $xml_of{$ident} );
37  
38 # Check for server message. Not sure if stats will do this.
39 2 ▶ if ( $dom->exists('/results/message') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dom->exists("/results/message")) 0 2 2
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 2 foreach my $metric_node ( $dom->findnodes('/scores/metric') ) {
50 9 my $atd_metric = Lingua::AtD::Metric->new(
51 {
52 key => $metric_node->findvalue('key'),
53 type => $metric_node->findvalue('type'),
54 value => $metric_node->findvalue('value'),
55 }
56 );
57 9 $metric_count_of{$ident} += 1;
58 9 push( @atd_metrics, $atd_metric );
59 }
60 2 $metrics_of{$ident} = [@atd_metrics];
61  
62 2 return;
63 }
64  
65 sub has_server_exception {
66 1 ▶ my $self = shift;
Subroutine has_server_exception: called CC=2 SCAR=7.2
Pod documented
67 1 ▶ return defined( $server_exception_of{ ident($self) } ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $server_exception_of{builtin::refaddr($self)} ? : 0 1 1
68 }
69  
70 sub has_metrics {
71 3 ▶ my $self = shift;
Subroutine has_metrics: called CC=2 SCAR=7.2
Pod documented
72 3 ▶ return defined( $metrics_of{ ident($self) } ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $metrics_of{builtin::refaddr($self)} ? : 3 0 3
73 }
74  
75 sub get_metrics {
76 1 ▶ my $self = shift;
Subroutine get_metrics: called CC=2 SCAR=7.1
Pod documented
77 return $self->has_metrics()
78 1 ▶ ? @{ $metrics_of{ ident($self) } }
Branch: 1 branch
BranchTrueFalseTotal
$self->has_metrics ? : 1 0 1
79 : undef;
80 }
81 }
82  
83 5 1; # Magic true value required at end of module
84  
85  
86 =pod
87  
88 =head1 NAME
89  
90 Lingua::AtD::Scores - Encapsulate conversion of XML from /stats call to Metric objects.
91  
92 =head1 VERSION
93  
94 version 1.121570
95  
96 =head1 SYNOPSIS
97  
98 use Lingua::AtD;
99  
100 # Create a new service proxy
101 my $atd = Lingua::AtD->new( {
102 host => 'service.afterthedeadline.com',
103 port => 80
104 throttle => 2,
105 });
106  
107 # Run spelling and grammar checks. Returns a Lingua::AtD::Response object.
108 my $doc_check = $atd->check_document('Text to check.');
109 # Loop through reported document errors.
110 foreach my $atd_error ($doc_check->get_errors()) {
111 # Do something with...
112 print "Error string: ", $atd_error->get_string(), "\n";
113 }
114  
115 # Run only grammar checks. Essentially the same as
116 # check_document(), sans spell-check.
117 my $grmr_check = $atd->check_grammar('Text to check.');
118 # Loop through reported document errors.
119 foreach my $atd_error ($grmr_check->get_errors()) {
120 # Do something with...
121 print "Error string: ", $atd_error->get_string(), "\n";
122 }
123  
124 # Get statistics on a document. Returns a Lingua::AtD::Scores object.
125 my $atd_scores = $atd->stats('Text to check.');
126 # Loop through reported document errors.
127 foreach my $atd_metric ($atd_scores->get_metrics()) {
128 # Do something with...
129 print $atd_metric->get_type(), "/", $atd_metric->get_key(),
130 " = ", $atd_metric->get_value(), "\n";
131 }
132  
133 =head1 DESCRIPTION
134  
135 Encapsulates conversion of the XML response from the AtD server into a list of spelling/grammar/style metric objects (L<Lingua::AtD::Metric>).
136  
137 =head1 METHODS
138  
139 =head2 new
140  
141 # Possible, but not likely
142 my $atd_scores = Lingua::AtD::Scores->new($xml_response);
143 foreach my $atd_metric ($atd_scores->get_metrics()) {
144 # Do something really fun...
145 }
146  
147 Lingua::AtD::Scores objects should only ever be created from a 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.
148  
149 See the L<SYNOPSIS> for typical usage.
150  
151 =head2 has_server_exception
152  
153 Convenience method to see if the AtD server returned an error message.
154  
155 =head2 get_server_exception
156  
157 Exception message from the server.
158  
159 =head2 has_metrics
160  
161 Convenience method to see if the XML response from AtD actually contained any metrics.
162  
163 =head2 get_metric_count
164  
165 Returns the number of linguistic metrics generated by AtD.
166  
167 =head2 get_metrics
168  
169 Returns a list of linguistic metrics as L<Lingua::AtD::Metric> objects. For details on what metrics are supplied, see the L<API Documentation|http://www.afterthedeadline.com/api.slp>.
170  
171 =head2 get_xml
172  
173 Returns a string containing the raw XML response from the AtD service call.
174  
175 =head1 SEE ALSO
176  
177 See the L<API Documentation|http://www.afterthedeadline.com/api.slp> at After the Deadline's website.
178  
179 =head1 AUTHOR
180  
181 David L. Day <dday376@gmail.com>
182  
183 =head1 COPYRIGHT AND LICENSE
184  
185 This software is copyright (c) 2011 by David L. Day.
186  
187 This is free software; you can redistribute it and/or modify it under
188 the same terms as the Perl 5 programming language system itself.
189  
190 =cut
191  
192  
193 __END__
« AtD/Results.pm ↑ Summary AtD/Error.pm »