| 1 | package WebService::ReutersConnect::APIResponse; | ||||||||||||||||||||||
| 2 | 1 | ▶ | use Moose; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 3 | 1 | ▶ | use HTTP::Response; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 4 | 1 | ▶ | use XML::LibXML; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 5 | 1 | ▶ | use Log::Log4perl; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 6 | |||||||||||||||||||||||
| 7 | 1 | my $LOGGER = Log::Log4perl->get_logger(); | |||||||||||||||||||||
| 8 | |||||||||||||||||||||||
| 9 | 1 | has 'http_response' => ( is => 'ro' , isa => 'HTTP::Response' , handles => qr/.*/ ); | |||||||||||||||||||||
| 10 | |||||||||||||||||||||||
| 11 | 1 | has 'xml_document' => ( is => 'ro', isa => 'XML::LibXML::Document' , lazy_build => 1 ); | |||||||||||||||||||||
| 12 | 1 | has 'reuters_status' => ( is => 'ro' , isa => 'Int' , lazy_build => 1 ); | |||||||||||||||||||||
| 13 | 1 | has 'reuters_errors' => ( is => 'ro' , isa => 'ArrayRef[HashRef]' , lazy_build => 1 ); | |||||||||||||||||||||
| 14 | |||||||||||||||||||||||
| 15 | 0 | ▶ | sub _build_xml_document{ | ||||||||||||||||||||
|
Subroutine
_build_xml_document: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||
| 16 | 0 | my ($self) = @_; | |||||||||||||||||||||
| 17 | |||||||||||||||||||||||
| 18 | 0 | my $content = $self->http_response->content(); | |||||||||||||||||||||
| 19 | 0 | return XML::LibXML->load_xml( string => $content ); | |||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||
| 21 | |||||||||||||||||||||||
| 22 | |||||||||||||||||||||||
| 23 | 0 | ▶ | sub _build_reuters_status{ | ||||||||||||||||||||
|
Subroutine
_build_reuters_status: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||
| 24 | 0 | my ($self) = @_; | |||||||||||||||||||||
| 25 | |||||||||||||||||||||||
| 26 | 0 | my $doc = eval{ | |||||||||||||||||||||
| 27 | 0 | $self->xml_document(); | |||||||||||||||||||||
| 28 | }; | ||||||||||||||||||||||
| 29 | 0 | ▶ | if( my $err = $@ ){ | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 30 | 0 | $LOGGER->warn("Cannot build XML document: $err. Parsing: ".$self->content()); | |||||||||||||||||||||
| 31 | ## This is the reuter code for general error. | ||||||||||||||||||||||
| 32 | 0 | return 100; | |||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||
| 34 | |||||||||||||||||||||||
| 35 | ## Grab the status | ||||||||||||||||||||||
| 36 | 0 | my ( $status ) = $doc->documentElement()->findnodes('//status'); | |||||||||||||||||||||
| 37 | |||||||||||||||||||||||
| 38 | 0 | ▶ | unless( $status ){ | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 39 | 0 | $LOGGER->debug("NO status in response. Assuming success (10)"); | |||||||||||||||||||||
| 40 | 0 | return 10; | |||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||
| 42 | 0 | ▶ | return $status->getAttribute('code') // 100; | ||||||||||||||||||||
|
Condition0%0/2
$status->getAttribute("code") // 100
MC/DC0%0/1
$status->getAttribute("code") // 100
$status->getAttribute("code")
Truth table0%0/2
A
A= $status->getAttribute("code")
| |||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||
| 44 | |||||||||||||||||||||||
| 45 | 0 | ▶ | sub _build_reuters_errors{ | ||||||||||||||||||||
|
Subroutine
_build_reuters_errors: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||
| 46 | 0 | my ($self) = @_; | |||||||||||||||||||||
| 47 | 0 | my $doc = eval{ $self->xml_document() }; | |||||||||||||||||||||
| 48 | 0 | ▶ | if( my $err = $@ ){ | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 49 | 0 | return [ { code => 10000, error => 'Cannot parse response: '.$@ } ]; | |||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||
| 51 | 0 | my @r = (); | |||||||||||||||||||||
| 52 | |||||||||||||||||||||||
| 53 | ## Ok find error nodes. | ||||||||||||||||||||||
| 54 | 0 | my @error_nodes = $doc->findnodes('//status/error'); | |||||||||||||||||||||
| 55 | 0 | foreach my $elt ( @error_nodes ){ | |||||||||||||||||||||
| 56 | 0 | ▶ | push @r, { code => $elt->getAttribute('code') // 10000, | ||||||||||||||||||||
|
Condition0%0/4
$elt->getAttribute("code") // 10000
$elt->textContent // "No text content"
MC/DC0%0/1
$elt->getAttribute("code") // 10000
$elt->getAttribute("code")
MC/DC0%0/1
$elt->textContent // "No text content"
$elt->textContent
Truth table0%0/2
A
A= $elt->getAttribute("code")
Truth table0%0/2
A
A= $elt->textContent
| |||||||||||||||||||||||
| 57 | error => $elt->textContent() // 'No text content' }; | ||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||
| 59 | 0 | return \@r; | |||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||
| 61 | |||||||||||||||||||||||
| 62 | =head1 NAME | ||||||||||||||||||||||
| 63 | |||||||||||||||||||||||
| 64 | WebService::ReutersConnect::APIResponse - A ReutersConnect API Response (decorates L<HTTP::Response>). | ||||||||||||||||||||||
| 65 | |||||||||||||||||||||||
| 66 | =cut | ||||||||||||||||||||||
| 67 | |||||||||||||||||||||||
| 68 | =head2 xml_document | ||||||||||||||||||||||
| 69 | |||||||||||||||||||||||
| 70 | Returns the L<XML::LibXML::Document> of this reuter response. | ||||||||||||||||||||||
| 71 | |||||||||||||||||||||||
| 72 | =head2 reuters_status | ||||||||||||||||||||||
| 73 | |||||||||||||||||||||||
| 74 | Returns the reuter status of this query. As per Reuters Connect doc: | ||||||||||||||||||||||
| 75 | |||||||||||||||||||||||
| 76 | 5 - Pending (only for Items) | ||||||||||||||||||||||
| 77 | 10 - Success | ||||||||||||||||||||||
| 78 | 20 - Warnings (Partial success) | ||||||||||||||||||||||
| 79 | 30 - Failure | ||||||||||||||||||||||
| 80 | |||||||||||||||||||||||
| 81 | =head2 reuters_errors | ||||||||||||||||||||||
| 82 | |||||||||||||||||||||||
| 83 | Returns an array ref of reuteur errors: | ||||||||||||||||||||||
| 84 | |||||||||||||||||||||||
| 85 | [ { code => 1234 , error => 'This is error 1234' }, | ||||||||||||||||||||||
| 86 | { code => 5678', error => 'This is error 5678' }, | ||||||||||||||||||||||
| 87 | ... | ||||||||||||||||||||||
| 88 | ] | ||||||||||||||||||||||
| 89 | |||||||||||||||||||||||
| 90 | =cut | ||||||||||||||||||||||
| 91 | |||||||||||||||||||||||
| 92 | =head2 is_reuters_success | ||||||||||||||||||||||
| 93 | |||||||||||||||||||||||
| 94 | Returns true if this Response is successfull in reuter's term. Meaning success or partial success. | ||||||||||||||||||||||
| 95 | |||||||||||||||||||||||
| 96 | Usage: | ||||||||||||||||||||||
| 97 | |||||||||||||||||||||||
| 98 | unless( $this->is_reuters_success() ){ | ||||||||||||||||||||||
| 99 | print "Return STATUS code says: ".$this->reuters_status()."\n"; | ||||||||||||||||||||||
| 100 | print "Errors are: ".$this->reuters_errors_string()."\n"; | ||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||
| 102 | |||||||||||||||||||||||
| 103 | =cut | ||||||||||||||||||||||
| 104 | |||||||||||||||||||||||
| 105 | 0 | ▶ | sub is_reuters_success{ | ||||||||||||||||||||
|
Subroutine
is_reuters_success: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||
| 106 | 0 | my ($self) = @_; | |||||||||||||||||||||
| 107 | 0 | ▶ | return $self->reuters_status() == 10 || $self->reuters_status() == 20; | ||||||||||||||||||||
|
Condition0%0/3
$self->reuters_status == 10 || $self->reuters_status == 20
MC/DC0%0/2
$self->reuters_status == 10 || $self->reuters_status == 20
$self->reuters_status == 10
$self->reuters_status == 20
Truth table0%0/3
A || B
A= $self->reuters_status == 10B= $self->reuters_status == 20
| |||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||
| 109 | |||||||||||||||||||||||
| 110 | =head2 reuters_errors_string | ||||||||||||||||||||||
| 111 | |||||||||||||||||||||||
| 112 | Convenience method. Returns something reasonably nice to display in | ||||||||||||||||||||||
| 113 | case this request is not a success. | ||||||||||||||||||||||
| 114 | |||||||||||||||||||||||
| 115 | Usage: | ||||||||||||||||||||||
| 116 | |||||||||||||||||||||||
| 117 | print $this->reuters_errors_string(); | ||||||||||||||||||||||
| 118 | |||||||||||||||||||||||
| 119 | =cut | ||||||||||||||||||||||
| 120 | |||||||||||||||||||||||
| 121 | 0 | ▶ | sub reuters_errors_string{ | ||||||||||||||||||||
|
Subroutine
reuters_errors_string: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||
| 122 | 0 | my ($self) = @_; | |||||||||||||||||||||
| 123 | 0 | return join(', ', map { $_->{code}.':'.$_->{error} } @{$self->reuters_errors()} ); | |||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||
| 125 | |||||||||||||||||||||||
| 126 | =head2 has_reuters_error | ||||||||||||||||||||||
| 127 | |||||||||||||||||||||||
| 128 | Returns true of this response has the given reuters error code. | ||||||||||||||||||||||
| 129 | |||||||||||||||||||||||
| 130 | Usage: | ||||||||||||||||||||||
| 131 | |||||||||||||||||||||||
| 132 | if( $this->has_reuters_error(3002) ){ | ||||||||||||||||||||||
| 133 | ... | ||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||
| 135 | |||||||||||||||||||||||
| 136 | =cut | ||||||||||||||||||||||
| 137 | |||||||||||||||||||||||
| 138 | 0 | ▶ | sub has_reuters_error{ | ||||||||||||||||||||
|
Subroutine
has_reuters_error: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||
| 139 | 0 | my ($self, $error_code) = @_; | |||||||||||||||||||||
| 140 | 0 | return !!grep { $_->{code} eq $error_code } @{$self->reuters_errors()}; | |||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||
| 142 | |||||||||||||||||||||||
| 143 | 1 | __PACKAGE__->meta->make_immutable(); | |||||||||||||||||||||
| 144 | 1 | 1; | |||||||||||||||||||||