AtD.pm

filter: Statementstmt 61.7% 42 / 68 Branchbran 0.0% 0 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 71.4% 10 / 14 Podpod 75.0% 3 / 4 totaltotal 61.1% 55 / 90 CC 3 SCAR 11.8
CC
3
Cov
58%
_atd
12.0
check_document
2.0
check_grammar
2.0
CRAP 3.2
↑ 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;
10 2 $Lingua::AtD::VERSION = '1.160790';
11 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Class::Std;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use Lingua::AtD::Results;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use Lingua::AtD::Scores;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 #use Lingua::AtD::Exceptions;
20 2 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 # ABSTRACT: Provides an OO wrapper for After the Deadline grammar and spelling service.
23  
24 {
25  
26 # Attributes
27 2 my %api_key_of :
28 ATTR( :init_arg<api_key> :get<api_key> :default<'Lingua-AtD'> );
29 2 my %throttle_of :
30 ATTR( :init_arg<throttle> :get<throttle> :set<throttle> :default<2> );
31 2 my %last_call_of : ATTR( :get<last_call> :default<0> );
32 2 my %service_host_of :
33 ATTR( :init_arg<host> :get<service_host> :default<'service.afterthedeadline.com'> );
34 2 my %service_port_of :
35 ATTR( :init_arg<port> :get<service_port> :default<80> );
36 2 my %service_url_of : ATTR( :get<service_url> );
37  
38 2 ▶ sub START {
Subroutine START: called CC=1 SCAR=0.0
Pod undocumented
39 2 my ( $self, $ident, $arg_ref ) = @_;
40  
41 # Construct the URL
42 $service_url_of{$ident} =
43 'http://'
44 . $service_host_of{$ident} . ':'
45 2 . $service_port_of{$ident} . '/';
46  
47 # Generate API Key
48 2 my $rand_hex = join "", map { unpack "H*", chr( rand(256) ) } 1 .. 16;
49 2 $api_key_of{$ident} = "Lingua-AtD-$rand_hex";
50  
51 2 return;
52 }
53  
54 0 ▶ sub _atd : PRIVATE {
Subroutine _atd: not called CC=3 SCAR=24.8
55 0 my ( $self, $verb, $arg_ref ) = @_;
56 0 my $ident = ident($self);
57 0 my $url = $service_url_of{$ident} . $verb;
58 0 my $ua = LWP::UserAgent->new();
59 0 $ua->agent( 'Lingua::AtD/' . $Lingua::AtD::VERSION );
60  
61 # Throttle Calls. AtD throws a 503 if called too quickly.
62 0 my $remaining = $throttle_of{$ident} - ( time - $last_call_of{$ident} );
63 0 ▶ sleep($remaining) if ( $remaining > 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $remaining > 0 0 0 0
64  
65 0 my $response = $ua->post( $url, Content => [ %{$arg_ref} ] );
66  
67 0 $last_call_of{$ident} = time;
68  
69 0 ▶ if ( $response->is_error() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_error) 0 0 0
70  
71 # TODO: Implement Exceptions
72 0 my $msg = "'$url' responded with '" . $response->status_line . "'.";
73 0 croak $msg;
74  
75 # Lingua::AtD::HTTPException->throw(
76 # http_status => $response->status_line,
77 # service_url => $url,
78 # );
79 }
80  
81 0 return $response->content;
82 2 ▶ }
Subroutine BEGIN: called CC=1 SCAR=0.0
83  
84 0 ▶ sub check_document {
Subroutine check_document: not called CC=1 SCAR=6.9
Pod documented
85 0 my ( $self, $text ) = @_;
86 0 my $ident = ident($self);
87 my $raw_response =
88 $self->_atd( 'checkDocument',
89 0 { key => $api_key_of{$ident}, data => $text } );
90 0 return Lingua::AtD::Results->new( { xml => $raw_response } );
91 }
92  
93 0 ▶ sub check_grammar {
Subroutine check_grammar: not called CC=1 SCAR=6.9
Pod documented
94 0 my ( $self, $text ) = @_;
95 0 my $ident = ident($self);
96 my $raw_response =
97 $self->_atd( 'checkGrammar',
98 0 { key => $api_key_of{$ident}, data => $text } );
99 0 return Lingua::AtD::Results->new( { xml => $raw_response } );
100 }
101  
102 0 ▶ sub stats {
Subroutine stats: not called CC=1 SCAR=6.9
Pod documented
103 0 my ( $self, $text ) = @_;
104 0 my $ident = ident($self);
105 my $raw_response =
106 0 $self->_atd( 'stats', { key => $api_key_of{$ident}, data => $text } );
107 0 return Lingua::AtD::Scores->new( { xml => $raw_response } );
108 }
109 }
110  
111 2 1; # Magic true value required at end of module
112  
113 __END__
↑ Summary AtD/Error.pm »