| 1 | package WebService::ReutersConnect::Item; | ||||||||||||
| 2 | 1 | ▶ | use Moose; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 3 | 1 | ▶ | use DateTime; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 1 | ▶ | use DateTime::Format::ISO8601; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 1 | ▶ | use Log::Log4perl; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 1 | my $LOGGER = Log::Log4perl->get_logger(); | |||||||||||
| 7 | |||||||||||||
| 8 | 1 | ▶ | use URI::Escape; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | |||||||||||||
| 10 | ## Source reuters | ||||||||||||
| 11 | 1 | has 'reuters' => ( is => 'ro' , isa => 'WebService::ReutersConnect' , weak_ref => 1 , required => 1); | |||||||||||
| 12 | |||||||||||||
| 13 | ## Source channel. | ||||||||||||
| 14 | 1 | has 'channel' => ( is => 'ro', isa => 'WebService::ReutersConnect::Channel', required => 1 , lazy_build => 1 ); | |||||||||||
| 15 | 1 | has 'channel_alias' => ( is => 'ro', isa => 'Str' , required => 1 ); | |||||||||||
| 16 | |||||||||||||
| 17 | ## Raw mandatory attributes. | ||||||||||||
| 18 | 1 | has 'id' => ( is => 'ro' , isa => 'Str' , required => 1 ); | |||||||||||
| 19 | 1 | has 'guid' => ( is => 'ro' , isa => 'Str', required => 1); | |||||||||||
| 20 | 1 | has 'version' => ( is => 'ro', isa => 'Str', required => 1); | |||||||||||
| 21 | 1 | has 'date_created' => ( is => 'ro', isa => 'DateTime', required => 1); | |||||||||||
| 22 | 1 | has 'media_type' => ( is => 'ro', isa => 'Str', required => 1 ); | |||||||||||
| 23 | |||||||||||||
| 24 | ## Raw optional attributes. | ||||||||||||
| 25 | 1 | has 'headline' => ( is => 'ro', isa => 'Maybe[Str]' ); | |||||||||||
| 26 | 1 | has 'slug' => ( is => 'ro', isa => 'Maybe[Str]'); | |||||||||||
| 27 | 1 | has 'raw_preview_url' => ( is => 'ro', isa => 'Maybe[Str]'); | |||||||||||
| 28 | 1 | has 'priority' => ( is => 'ro', isa => 'Maybe[Str]' ); | |||||||||||
| 29 | 1 | has 'fragment' => ( is => 'ro', isa => 'Maybe[Str]' ); | |||||||||||
| 30 | |||||||||||||
| 31 | ## Duration in second for video content. | ||||||||||||
| 32 | 1 | has 'duration' => ( is => 'ro' , isa => 'Maybe[Int]' ); | |||||||||||
| 33 | |||||||||||||
| 34 | ## The main links if this Item is a NEP (News Event Package). | ||||||||||||
| 35 | 0 | ▶ | has 'main_links' => ( is => 'ro', isa => 'ArrayRef[WebService::ReutersConnect::Item]' , default => sub{ []; }, required => 1 ); | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||
| 36 | 0 | ▶ | has 'supplemental_sets' => ( is => 'ro', isa => 'ArrayRef[WebService::ReutersConnect::ResultSet]' , default => sub{ []; }, required => 1); | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||
| 37 | |||||||||||||
| 38 | ## Extended calculated attributes. | ||||||||||||
| 39 | 1 | has 'preview_url' => ( is => 'ro', isa => 'Maybe[Str]' , lazy_build => 1 ); | |||||||||||
| 40 | |||||||||||||
| 41 | |||||||||||||
| 42 | |||||||||||||
| 43 | =head1 NAME | ||||||||||||
| 44 | |||||||||||||
| 45 | WebService::ReutersConnect::Item - A ReutersConnect SNI (Simple News Item). | ||||||||||||
| 46 | |||||||||||||
| 47 | =head1 PROPERTIES | ||||||||||||
| 48 | |||||||||||||
| 49 | =head2 channel | ||||||||||||
| 50 | |||||||||||||
| 51 | Origin L<WebService::ReutersConnect::Channel> of this Item. | ||||||||||||
| 52 | |||||||||||||
| 53 | =head2 id | ||||||||||||
| 54 | |||||||||||||
| 55 | Absolute ID of this item and version. | ||||||||||||
| 56 | |||||||||||||
| 57 | =head2 guid | ||||||||||||
| 58 | |||||||||||||
| 59 | ID of this item regardless of its version. | ||||||||||||
| 60 | |||||||||||||
| 61 | =head2 date_created | ||||||||||||
| 62 | |||||||||||||
| 63 | A L<DateTime> | ||||||||||||
| 64 | |||||||||||||
| 65 | =head2 slug | ||||||||||||
| 66 | |||||||||||||
| 67 | =head2 headline | ||||||||||||
| 68 | |||||||||||||
| 69 | =head2 media_type | ||||||||||||
| 70 | |||||||||||||
| 71 | =head2 preview_url | ||||||||||||
| 72 | |||||||||||||
| 73 | In case of an image. | ||||||||||||
| 74 | |||||||||||||
| 75 | =head2 priority | ||||||||||||
| 76 | |||||||||||||
| 77 | =head2 duration | ||||||||||||
| 78 | |||||||||||||
| 79 | In case of a Video. Duration in seconds. | ||||||||||||
| 80 | |||||||||||||
| 81 | =cut | ||||||||||||
| 82 | |||||||||||||
| 83 | 0 | ▶ | sub _build_preview_url{ | ||||||||||
|
Subroutine
_build_preview_url: not called CC=2 SCAR=17.9
| |||||||||||||
| 84 | 0 | my ($self) = @_; | |||||||||||
| 85 | 0 | my $raw = $self->raw_preview_url(); | |||||||||||
| 86 | 0 | ▶ | unless( $raw ){ return; } | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 87 | 0 | return $raw.'?token='.uri_escape_utf8($self->reuters->authToken()); | |||||||||||
| 88 | } | ||||||||||||
| 89 | |||||||||||||
| 90 | 0 | ▶ | sub _build_channel{ | ||||||||||
|
Subroutine
_build_channel: not called CC=1 SCAR=6.9
| |||||||||||||
| 91 | 0 | my ($self) = @_; | |||||||||||
| 92 | 0 | $LOGGER->debug("Building channel from ".$self->channel_alias()); | |||||||||||
| 93 | 0 | my ($channel) = $self->reuters->channels( { channel => [ $self->channel_alias() ] }); | |||||||||||
| 94 | 0 | $LOGGER->debug("Got channel '".$channel->description()."'"); | |||||||||||
| 95 | 0 | return $channel; | |||||||||||
| 96 | } | ||||||||||||
| 97 | |||||||||||||
| 98 | |||||||||||||
| 99 | =head2 fetch_richer_me | ||||||||||||
| 100 | |||||||||||||
| 101 | Convenience method that uses WebService::ReutersConnect::fetch_package tp fetch a richer version | ||||||||||||
| 102 | of this object. Dies if $this is not composite. | ||||||||||||
| 103 | |||||||||||||
| 104 | Usage: | ||||||||||||
| 105 | |||||||||||||
| 106 | if( $this->is_composite() ){ | ||||||||||||
| 107 | $this->fetch_richer_me(); | ||||||||||||
| 108 | ## This now contains richer content (according to ReutersConnect's package method. | ||||||||||||
| 109 | } | ||||||||||||
| 110 | |||||||||||||
| 111 | =cut | ||||||||||||
| 112 | |||||||||||||
| 113 | 0 | ▶ | sub fetch_richer_me{ | ||||||||||
|
Subroutine
fetch_richer_me: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||
| 114 | 0 | my ($self) = @_; | |||||||||||
| 115 | 0 | ▶ | unless( $self->is_composite() ){ | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 116 | 0 | confess("Only a composite Item can be made richer"); | |||||||||||
| 117 | } | ||||||||||||
| 118 | 0 | my ($richer) = $self->reuters->fetch_package( $self->channel_alias() , [ $self->id() ]); | |||||||||||
| 119 | 0 | return $richer; | |||||||||||
| 120 | } | ||||||||||||
| 121 | |||||||||||||
| 122 | =head2 is_composite | ||||||||||||
| 123 | |||||||||||||
| 124 | Returns true if this item is composite. A composite item is | ||||||||||||
| 125 | typically a NEP (News Event Package), or a SNEP (Super News Event Package pointing to other NEPs). | ||||||||||||
| 126 | |||||||||||||
| 127 | usage: | ||||||||||||
| 128 | |||||||||||||
| 129 | if( $this->is_composite() ){ | ||||||||||||
| 130 | my ( $rich_version ) = $reuters->fetch_package( $this->channel() , [ $this ] ); | ||||||||||||
| 131 | } | ||||||||||||
| 132 | |||||||||||||
| 133 | =cut | ||||||||||||
| 134 | |||||||||||||
| 135 | 0 | ▶ | sub is_composite{ | ||||||||||
|
Subroutine
is_composite: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 136 | 0 | my ($self) = @_; | |||||||||||
| 137 | 0 | return $self->media_type() eq 'C'; | |||||||||||
| 138 | } | ||||||||||||
| 139 | |||||||||||||
| 140 | =head2 from_node | ||||||||||||
| 141 | |||||||||||||
| 142 | Build a new instance from a XML::LibXML::Node. Internal Use. | ||||||||||||
| 143 | |||||||||||||
| 144 | =cut | ||||||||||||
| 145 | |||||||||||||
| 146 | 0 | ▶ | sub from_node{ | ||||||||||
|
Subroutine
from_node: not called CC=11 SCAR=48.8
Pod
documented
| |||||||||||||
| 147 | 0 | my ($class, $node, $reuters, $channel) = @_; | |||||||||||
| 148 | |||||||||||||
| 149 | |||||||||||||
| 150 | 0 | my $build = { reuters => $reuters }; | |||||||||||
| 151 | 0 | ▶ | unless( $channel ){ | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 152 | ## Just extract the channel ID and let the _build_channel do its job later. | ||||||||||||
| 153 | 0 | ▶ | $build->{channel_alias} = $node->findvalue('./channel') || confess("Cannot find channel in node ".$node->toString()); | ||||||||||
|
Condition0%0/2
$node->findvalue('./channel') || confess('Cannot find channel in node ' . $node->toString)
MC/DC0%0/1
$node->findvalue('./channel') || confess('Cannot find channel in node ' . $node->toString)
$node->findvalue('./channel')
Truth table0%0/2
A
A= $node->findvalue('./channel')
| |||||||||||||
| 154 | }else{ | ||||||||||||
| 155 | 0 | $build->{channel} = $channel; | |||||||||||
| 156 | 0 | $build->{channel_alias} = $channel->alias(); | |||||||||||
| 157 | } | ||||||||||||
| 158 | |||||||||||||
| 159 | ## $LOGGER->info("Building Item from ".$node->toString()); | ||||||||||||
| 160 | |||||||||||||
| 161 | 0 | $build->{id} = $node->findvalue('./id'); | |||||||||||
| 162 | 0 | $build->{guid} = $node->findvalue('./guid'); | |||||||||||
| 163 | 0 | $build->{version} = $node->findvalue('./version'); | |||||||||||
| 164 | 0 | $build->{date_created} = DateTime::Format::ISO8601->parse_datetime($node->findvalue('./dateCreated')); | |||||||||||
| 165 | 0 | ▶ | $build->{slug} = $node->findvalue('./slug') || undef; | ||||||||||
|
Condition0%0/2
$node->findvalue('./slug') || undef
MC/DC0%0/1
$node->findvalue('./slug') || undef
$node->findvalue('./slug')
Truth table0%0/2
A
A= $node->findvalue('./slug')
| |||||||||||||
| 166 | 0 | ▶ | $build->{headline} = $node->findvalue('./headline') || undef; | ||||||||||
|
Condition0%0/2
$node->findvalue('./headline') || undef
MC/DC0%0/1
$node->findvalue('./headline') || undef
$node->findvalue('./headline')
Truth table0%0/2
A
A= $node->findvalue('./headline')
| |||||||||||||
| 167 | 0 | ▶ | $build->{fragment} = $node->findvalue('./fragment') || undef; | ||||||||||
|
Condition0%0/2
$node->findvalue('./fragment') || undef
MC/DC0%0/1
$node->findvalue('./fragment') || undef
$node->findvalue('./fragment')
Truth table0%0/2
A
A= $node->findvalue('./fragment')
| |||||||||||||
| 168 | 0 | $build->{media_type} = $node->findvalue('./mediaType'); | |||||||||||
| 169 | 0 | $build->{priority} = $node->findvalue('./priority'); | |||||||||||
| 170 | 0 | ▶ | $build->{raw_preview_url} = $node->findvalue('./previewUrl') || undef; | ||||||||||
|
Condition0%0/2
$node->findvalue('./previewUrl') || undef
MC/DC0%0/1
$node->findvalue('./previewUrl') || undef
$node->findvalue('./previewUrl')
Truth table0%0/2
A
A= $node->findvalue('./previewUrl')
| |||||||||||||
| 171 | 0 | ▶ | $build->{duration} = $node->findvalue('./duration') || undef; | ||||||||||
|
Condition0%0/2
$node->findvalue('./duration') || undef
MC/DC0%0/1
$node->findvalue('./duration') || undef
$node->findvalue('./duration')
Truth table0%0/2
A
A= $node->findvalue('./duration')
| |||||||||||||
| 172 | |||||||||||||
| 173 | 0 | my @links = (); | |||||||||||
| 174 | 0 | my @link_nodes = $node->findnodes('./mainLinks/link'); | |||||||||||
| 175 | 0 | foreach my $link_node ( @link_nodes ){ | |||||||||||
| 176 | 0 | push @links, $class->from_node($link_node, $reuters, $channel); | |||||||||||
| 177 | } | ||||||||||||
| 178 | 0 | $build->{'main_links'} = \@links; | |||||||||||
| 179 | |||||||||||||
| 180 | 0 | my @supp_sets = (); | |||||||||||
| 181 | 0 | my @supp_set_nodes = $node->findnodes('./supplementalLinks'); | |||||||||||
| 182 | 0 | foreach my $supp_set_node ( @supp_set_nodes ){ | |||||||||||
| 183 | 0 | my $id = $supp_set_node->getAttribute('id'); | |||||||||||
| 184 | 0 | my @link_nodes = $supp_set_node->findnodes('./link'); | |||||||||||
| 185 | 0 | my @links = (); | |||||||||||
| 186 | 0 | foreach my $link_node ( @link_nodes ){ | |||||||||||
| 187 | 0 | push @links , $class->from_node($link_node, $reuters, $channel); | |||||||||||
| 188 | } | ||||||||||||
| 189 | 0 | my $n_found = scalar(@links); | |||||||||||
| 190 | 0 | my $set = WebService::ReutersConnect::ResultSet->new({ id => $id, | |||||||||||
| 191 | items => \@links, | ||||||||||||
| 192 | num_found => $n_found, | ||||||||||||
| 193 | size => $n_found, | ||||||||||||
| 194 | start => 0 }); | ||||||||||||
| 195 | 0 | push @supp_sets , $set; | |||||||||||
| 196 | } | ||||||||||||
| 197 | 0 | $build->{'supplemental_sets'} = \@supp_sets; | |||||||||||
| 198 | |||||||||||||
| 199 | 0 | return $class->new( $build ); | |||||||||||
| 200 | } | ||||||||||||
| 201 | |||||||||||||
| 202 | 1 | __PACKAGE__->meta->make_immutable(); | |||||||||||
| 203 | 1 | 1; | |||||||||||