| 1 | # -*- cperl; cperl-indent-level: 4 -*- | ||||||||||||||||||||||||||||||||||||||
| 2 | # Copyright (C) 2011-2021, Roland van Ipenburg | ||||||||||||||||||||||||||||||||||||||
| 3 | package WWW::NOS::Open v1.0.5; | ||||||||||||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use utf8; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use 5.014000; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||
| 10 | 4 | ▶ | use Date::Calc qw(Add_Delta_Days Date_to_Days Delta_Days Today); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 11 | 4 | ▶ | use Date::Format; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 12 | 4 | ▶ | use HTTP::Headers; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 13 | 4 | ▶ | use HTTP::Request; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 14 | use HTTP::Status | ||||||||||||||||||||||||||||||||||||||
| 15 | 4 | ▶ | qw(HTTP_OK HTTP_BAD_REQUEST HTTP_UNAUTHORIZED HTTP_FORBIDDEN HTTP_INTERNAL_SERVER_ERROR); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 16 | 4 | ▶ | use JSON; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 17 | 4 | ▶ | use LWP::UserAgent; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 18 | 4 | ▶ | use Log::Log4perl qw(:easy get_logger); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 19 | 4 | ▶ | use Moose qw/around has with/; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 20 | 4 | ▶ | use Moose::Util::TypeConstraints qw/enum/; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 21 | 4 | ▶ | use URI::Escape qw(uri_escape); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 22 | 4 | ▶ | use URI; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 23 | 4 | ▶ | use XML::Simple; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||
| 25 | 4 | ▶ | use namespace::autoclean '-also' => qr/^__/sxm; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||
| 27 | 4 | ▶ | use WWW::NOS::Open::Article; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 28 | 4 | ▶ | use WWW::NOS::Open::AudioFragment; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 29 | 4 | ▶ | use WWW::NOS::Open::Broadcast; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 30 | 4 | ▶ | use WWW::NOS::Open::DayGuide; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 31 | 4 | ▶ | use WWW::NOS::Open::Document; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 32 | 4 | ▶ | use WWW::NOS::Open::Exceptions; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 33 | 4 | ▶ | use WWW::NOS::Open::Result; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 34 | 4 | ▶ | use WWW::NOS::Open::TypeDef; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 35 | 4 | ▶ | use WWW::NOS::Open::Version; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 36 | 4 | ▶ | use WWW::NOS::Open::Video; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||
| 38 | 4 | ▶ | use Readonly; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 39 | 4 | ▶ | Readonly::Scalar my $SERVER => $ENV{'NOSOPEN_SERVER'} || q{http://open.nos.nl}; | ||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$ENV{'NOSOPEN_SERVER'} || 'http://open.nos.nl'
MC/DC0%0/1
$ENV{'NOSOPEN_SERVER'} || 'http://open.nos.nl'
$ENV{'NOSOPEN_SERVER'}
Truth table50%1/2
A
A= $ENV{'NOSOPEN_SERVER'}
| |||||||||||||||||||||||||||||||||||||||
| 40 | 4 | Readonly::Scalar my $TIMEOUT => 15; | |||||||||||||||||||||||||||||||||||||
| 41 | 4 | Readonly::Scalar my $AGENT => q{WWW::NOS::Open/} . $WWW::NOS::Open::VERSION; | |||||||||||||||||||||||||||||||||||||
| 42 | 4 | Readonly::Scalar my $DATE_FORMAT => q{%04u-%02u-%02u}; | |||||||||||||||||||||||||||||||||||||
| 43 | 4 | Readonly::Scalar my $DEFAULT_START => -1; # Yesterday | |||||||||||||||||||||||||||||||||||||
| 44 | 4 | Readonly::Scalar my $DEFAULT_END => 1; # Tomorrow | |||||||||||||||||||||||||||||||||||||
| 45 | 4 | Readonly::Scalar my $MAX_RANGE => 14; # Two weeks | |||||||||||||||||||||||||||||||||||||
| 46 | 4 | Readonly::Scalar my $GET => q{GET}; | |||||||||||||||||||||||||||||||||||||
| 47 | 4 | Readonly::Scalar my $DEFAULT_API_KEY => q{TEST}; | |||||||||||||||||||||||||||||||||||||
| 48 | 4 | Readonly::Scalar my $DEFAULT_OUTPUT => q{xml}; | |||||||||||||||||||||||||||||||||||||
| 49 | 4 | Readonly::Scalar my $DEFAULT_CATEGORY => q{nieuws}; | |||||||||||||||||||||||||||||||||||||
| 50 | 4 | Readonly::Scalar my $DASH => q{-}; | |||||||||||||||||||||||||||||||||||||
| 51 | 4 | Readonly::Scalar my $DOUBLE_COLON => q{::}; | |||||||||||||||||||||||||||||||||||||
| 52 | 4 | Readonly::Scalar my $FRAGMENT => q{Fragment}; | |||||||||||||||||||||||||||||||||||||
| 53 | 4 | Readonly::Scalar my $VERSION_PATH => q{%s/v1/index/version/key/%s/output/%s/}; | |||||||||||||||||||||||||||||||||||||
| 54 | 4 | Readonly::Scalar my $LATEST_PATH => | |||||||||||||||||||||||||||||||||||||
| 55 | q{%s/v1/latest/%s/key/%s/output/%s/category/%s/}; | ||||||||||||||||||||||||||||||||||||||
| 56 | 4 | Readonly::Scalar my $SEARCH_PATH => q{%s/v1/search/query/key/%s/output/%s/q/%s}; | |||||||||||||||||||||||||||||||||||||
| 57 | 4 | Readonly::Scalar my $GUIDE_PATH => | |||||||||||||||||||||||||||||||||||||
| 58 | q{%s/v1/guide/%s/key/%s/output/%s/start/%s/end/%s/}; | ||||||||||||||||||||||||||||||||||||||
| 59 | 4 | Readonly::Scalar my $STRIP_PRIVATE => qr{^_}smx; | |||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||
| 61 | 4 | Readonly::Hash my %ERR => ( | |||||||||||||||||||||||||||||||||||||
| 62 | 'INTERNAL_SERVER' => q{Internal server error or no response recieved}, | ||||||||||||||||||||||||||||||||||||||
| 63 | 'EXCEEDED_RANGE' => qq{Date range exceeds maximum of $MAX_RANGE days}, | ||||||||||||||||||||||||||||||||||||||
| 64 | ); | ||||||||||||||||||||||||||||||||||||||
| 65 | 4 | Readonly::Hash my %LOG => ( | |||||||||||||||||||||||||||||||||||||
| 66 | 'REQUESTING' => q{Requesting %s}, | ||||||||||||||||||||||||||||||||||||||
| 67 | 'RESPONSE_CODE' => q{Response code %d}, | ||||||||||||||||||||||||||||||||||||||
| 68 | ); | ||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||
| 70 | 4 | Log::Log4perl::easy_init($ERROR); | |||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||
| 72 | 4 | my $log = Log::Log4perl->get_logger(__PACKAGE__); | |||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||
| 74 | has '_ua' => ( | ||||||||||||||||||||||||||||||||||||||
| 75 | 'is' => 'ro', | ||||||||||||||||||||||||||||||||||||||
| 76 | 'isa' => 'LWP::UserAgent', | ||||||||||||||||||||||||||||||||||||||
| 77 | 4 | ▶ | 'default' => sub { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 78 | 4 | LWP::UserAgent->new( | |||||||||||||||||||||||||||||||||||||
| 79 | 'timeout' => $TIMEOUT, | ||||||||||||||||||||||||||||||||||||||
| 80 | 'agent' => $AGENT, | ||||||||||||||||||||||||||||||||||||||
| 81 | ); | ||||||||||||||||||||||||||||||||||||||
| 82 | }, | ||||||||||||||||||||||||||||||||||||||
| 83 | 4 | ); | |||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||
| 85 | 4 | has '_version' => ( | |||||||||||||||||||||||||||||||||||||
| 86 | 'is' => 'ro', | ||||||||||||||||||||||||||||||||||||||
| 87 | 'isa' => 'WWW::NOS::Open::Version', | ||||||||||||||||||||||||||||||||||||||
| 88 | ); | ||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||
| 90 | 1 | ▶ | sub get_version { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
get_version: called CC=1 SCAR=0.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 91 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||||||||
| 92 | 1 | my $url = sprintf $VERSION_PATH, $SERVER, | |||||||||||||||||||||||||||||||||||||
| 93 | URI::Escape::uri_escape( $self->get_api_key ), | ||||||||||||||||||||||||||||||||||||||
| 94 | URI::Escape::uri_escape( $self->_get_default_output ); | ||||||||||||||||||||||||||||||||||||||
| 95 | 1 | my $response = $self->_do_request($url); | |||||||||||||||||||||||||||||||||||||
| 96 | 0 | my $version = $self->_parse_version( $response->decoded_content ); | |||||||||||||||||||||||||||||||||||||
| 97 | 0 | return $version; | |||||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||
| 100 | 0 | ▶ | sub _parse_version { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_parse_version: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||
| 101 | 0 | my ( $self, $body ) = @_; | |||||||||||||||||||||||||||||||||||||
| 102 | 0 | my ( $version, $build ); | |||||||||||||||||||||||||||||||||||||
| 103 | 0 | my $xml = XML::Simple->new( 'ForceArray' => 1 )->XMLin($body); | |||||||||||||||||||||||||||||||||||||
| 104 | 0 | $version = $xml->{'item'}[0]->{'version'}[0]; | |||||||||||||||||||||||||||||||||||||
| 105 | 0 | $build = $xml->{'item'}[0]->{'build'}[0]; | |||||||||||||||||||||||||||||||||||||
| 106 | 0 | return WWW::NOS::Open::Version->new( $version, $build ); | |||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||
| 109 | 4 | has '_default_output' => ( | |||||||||||||||||||||||||||||||||||||
| 110 | 'is' => 'ro', | ||||||||||||||||||||||||||||||||||||||
| 111 | 'isa' => 'Str', | ||||||||||||||||||||||||||||||||||||||
| 112 | 'default' => $DEFAULT_OUTPUT, | ||||||||||||||||||||||||||||||||||||||
| 113 | 'reader' => '_get_default_output', | ||||||||||||||||||||||||||||||||||||||
| 114 | 'init_arg' => 'default_output', | ||||||||||||||||||||||||||||||||||||||
| 115 | ); | ||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||
| 117 | 4 | has '_api_key' => ( | |||||||||||||||||||||||||||||||||||||
| 118 | 'is' => 'rw', | ||||||||||||||||||||||||||||||||||||||
| 119 | 'isa' => 'Str', | ||||||||||||||||||||||||||||||||||||||
| 120 | 'default' => $DEFAULT_API_KEY, | ||||||||||||||||||||||||||||||||||||||
| 121 | 'reader' => 'get_api_key', | ||||||||||||||||||||||||||||||||||||||
| 122 | 'writer' => 'set_api_key', | ||||||||||||||||||||||||||||||||||||||
| 123 | 'init_arg' => 'api_key', | ||||||||||||||||||||||||||||||||||||||
| 124 | ); | ||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||
| 126 | 0 | ▶ | sub _get_latest_resources { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_get_latest_resources: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||
| 127 | 0 | my ( $self, $type, $category ) = @_; | |||||||||||||||||||||||||||||||||||||
| 128 | 0 | ▶ | ( defined $category ) || ( $category = $DEFAULT_CATEGORY ); | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 129 | 0 | my $url = sprintf $LATEST_PATH, | |||||||||||||||||||||||||||||||||||||
| 130 | $SERVER, | ||||||||||||||||||||||||||||||||||||||
| 131 | URI::Escape::uri_escape($type), | ||||||||||||||||||||||||||||||||||||||
| 132 | URI::Escape::uri_escape( $self->get_api_key ), | ||||||||||||||||||||||||||||||||||||||
| 133 | URI::Escape::uri_escape( $self->_get_default_output ), | ||||||||||||||||||||||||||||||||||||||
| 134 | URI::Escape::uri_escape($category); | ||||||||||||||||||||||||||||||||||||||
| 135 | 0 | my $response = $self->_do_request($url); | |||||||||||||||||||||||||||||||||||||
| 136 | 0 | my @resources = | |||||||||||||||||||||||||||||||||||||
| 137 | $self->_parse_resources( $type, $response->decoded_content ); | ||||||||||||||||||||||||||||||||||||||
| 138 | 0 | return @resources; | |||||||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||
| 141 | 0 | ▶ | sub get_latest_articles { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
get_latest_articles: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 142 | 0 | my ( $self, @param ) = @_; | |||||||||||||||||||||||||||||||||||||
| 143 | 0 | return $self->_get_latest_resources( q{article}, @param ); | |||||||||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||
| 146 | sub __get_props { | ||||||||||||||||||||||||||||||||||||||
| 147 | my $meta = shift; | ||||||||||||||||||||||||||||||||||||||
| 148 | my @props = map { $_->name } $meta->get_all_attributes; | ||||||||||||||||||||||||||||||||||||||
| 149 | for (@props) { | ||||||||||||||||||||||||||||||||||||||
| 150 | s/$STRIP_PRIVATE//smx; | ||||||||||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||||||||||
| 152 | return @props; | ||||||||||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||
| 155 | 0 | ▶ | sub _parse_resource { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_parse_resource: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||||||||
| 156 | 0 | my ( $self, $type, $hr_resource ) = @_; | |||||||||||||||||||||||||||||||||||||
| 157 | 0 | my %mapping = ( | |||||||||||||||||||||||||||||||||||||
| 158 | 'article' => __PACKAGE__ . $DOUBLE_COLON . ucfirst $type, | ||||||||||||||||||||||||||||||||||||||
| 159 | 'video' => __PACKAGE__ . $DOUBLE_COLON . ucfirst $type, | ||||||||||||||||||||||||||||||||||||||
| 160 | 'audio' => __PACKAGE__ . $DOUBLE_COLON . ucfirst $type . $FRAGMENT, | ||||||||||||||||||||||||||||||||||||||
| 161 | 'document' => __PACKAGE__ . $DOUBLE_COLON . ucfirst $type, | ||||||||||||||||||||||||||||||||||||||
| 162 | 'broadcast' => __PACKAGE__ . $DOUBLE_COLON . ucfirst $type, | ||||||||||||||||||||||||||||||||||||||
| 163 | ); | ||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||
| 165 | 0 | my @props = __get_props( ( $mapping{$type} )->meta ); | |||||||||||||||||||||||||||||||||||||
| 166 | 0 | my %param; | |||||||||||||||||||||||||||||||||||||
| 167 | 0 | ▶ | while ( my $prop = shift @props ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 168 | $param{$prop} = | ||||||||||||||||||||||||||||||||||||||
| 169 | ( q{HASH} eq ref $hr_resource->{$prop}[0] ) | ||||||||||||||||||||||||||||||||||||||
| 170 | 0 | ? %{ $hr_resource->{$prop}[0] } | |||||||||||||||||||||||||||||||||||||
| 171 | 0 | ▶ | : $hr_resource->{$prop}[0]; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||||||||||||
| 173 | 0 | ▶ | $param{'keywords'} = $hr_resource->{'keywords'}->[0]->{'keyword'} || []; | ||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$hr_resource->{'keywords'}[0]{'keyword'} || []
MC/DC0%0/1
$hr_resource->{'keywords'}[0]{'keyword'} || []
$hr_resource->{'keywords'}[0]{'keyword'}
Truth table0%0/2
A
A= $hr_resource->{'keywords'}[0]{'keyword'}
| |||||||||||||||||||||||||||||||||||||||
| 174 | 0 | return ( $mapping{$type} )->new(%param); | |||||||||||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||
| 177 | 0 | ▶ | sub _parse_resources { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_parse_resources: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||
| 178 | 0 | my ( $self, $type, $body ) = @_; | |||||||||||||||||||||||||||||||||||||
| 179 | 0 | my @resources; | |||||||||||||||||||||||||||||||||||||
| 180 | 0 | my $xml = XML::Simple->new( 'ForceArray' => 1 )->XMLin($body); | |||||||||||||||||||||||||||||||||||||
| 181 | 0 | my @xml_resources = @{ $xml->{$type} }; | |||||||||||||||||||||||||||||||||||||
| 182 | 0 | ▶ | while ( my $resource = shift @xml_resources ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 183 | 0 | push @resources, $self->_parse_resource( $type, $resource ); | |||||||||||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||||||||||
| 185 | 0 | return @resources; | |||||||||||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||
| 188 | 0 | ▶ | sub get_latest_videos { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
get_latest_videos: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 189 | 0 | my ( $self, @param ) = @_; | |||||||||||||||||||||||||||||||||||||
| 190 | 0 | return $self->_get_latest_resources( q{video}, @param ); | |||||||||||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||
| 193 | 0 | ▶ | sub get_latest_audio_fragments { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
get_latest_audio_fragments: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 194 | 0 | my ( $self, @param ) = @_; | |||||||||||||||||||||||||||||||||||||
| 195 | 0 | return $self->_get_latest_resources( q{audio}, @param ); | |||||||||||||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||
| 198 | 0 | ▶ | sub _parse_result { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_parse_result: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||
| 199 | 0 | my ( $self, $body ) = @_; | |||||||||||||||||||||||||||||||||||||
| 200 | 0 | my @documents; | |||||||||||||||||||||||||||||||||||||
| 201 | 0 | my $xml = XML::Simple->new( 'ForceArray' => 1 )->XMLin($body); | |||||||||||||||||||||||||||||||||||||
| 202 | 0 | my @xml_documents = @{ $xml->{'documents'}->[0]->{'document'} }; | |||||||||||||||||||||||||||||||||||||
| 203 | 0 | ▶ | while ( my $hr_document = shift @xml_documents ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 204 | 0 | push @documents, $self->_parse_resource( q{document}, $hr_document ); | |||||||||||||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||||||||||||
| 206 | my $result = WWW::NOS::Open::Result->new( | ||||||||||||||||||||||||||||||||||||||
| 207 | 'documents' => [@documents], | ||||||||||||||||||||||||||||||||||||||
| 208 | 0 | 'related' => $xml->{'related'}->[0]->{'related'}, | |||||||||||||||||||||||||||||||||||||
| 209 | ); | ||||||||||||||||||||||||||||||||||||||
| 210 | 0 | return $result; | |||||||||||||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||||||
| 213 | 0 | ▶ | sub search { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
search: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 214 | 0 | my ( $self, $query ) = @_; | |||||||||||||||||||||||||||||||||||||
| 215 | 0 | my $url = sprintf $SEARCH_PATH, | |||||||||||||||||||||||||||||||||||||
| 216 | $SERVER, | ||||||||||||||||||||||||||||||||||||||
| 217 | URI::Escape::uri_escape( $self->get_api_key ), | ||||||||||||||||||||||||||||||||||||||
| 218 | URI::Escape::uri_escape( $self->_get_default_output ), | ||||||||||||||||||||||||||||||||||||||
| 219 | URI::Escape::uri_escape($query); | ||||||||||||||||||||||||||||||||||||||
| 220 | 0 | my $response = $self->_do_request($url); | |||||||||||||||||||||||||||||||||||||
| 221 | 0 | my $result = $self->_parse_result( $response->decoded_content ); | |||||||||||||||||||||||||||||||||||||
| 222 | 0 | return $result; | |||||||||||||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||||||||||||
| 225 | 4 | ▶ | sub __get_date { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
__get_date: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 226 | 4 | my ( $start_day, $end_day ) = @_; | |||||||||||||||||||||||||||||||||||||
| 227 | 4 | my $today = Date_to_Days(Today); | |||||||||||||||||||||||||||||||||||||
| 228 | return ( | ||||||||||||||||||||||||||||||||||||||
| 229 | ( | ||||||||||||||||||||||||||||||||||||||
| 230 | 4 | sprintf $DATE_FORMAT, | |||||||||||||||||||||||||||||||||||||
| 231 | Add_Delta_Days( 1, 1, 1, $today + $start_day - 1 ), | ||||||||||||||||||||||||||||||||||||||
| 232 | ), | ||||||||||||||||||||||||||||||||||||||
| 233 | ( | ||||||||||||||||||||||||||||||||||||||
| 234 | sprintf $DATE_FORMAT, | ||||||||||||||||||||||||||||||||||||||
| 235 | Add_Delta_Days( 1, 1, 1, $today + $end_day - 1 ), | ||||||||||||||||||||||||||||||||||||||
| 236 | ), | ||||||||||||||||||||||||||||||||||||||
| 237 | ); | ||||||||||||||||||||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||||||
| 240 | 0 | ▶ | sub _parse_dayguide { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_parse_dayguide: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||
| 241 | 0 | my ( $self, $hr_dayguide ) = @_; | |||||||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||||||||
| 243 | 0 | my @props = __get_props( WWW::NOS::Open::DayGuide->meta ); | |||||||||||||||||||||||||||||||||||||
| 244 | 0 | my %param; | |||||||||||||||||||||||||||||||||||||
| 245 | 0 | ▶ | while ( my $prop = shift @props ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 246 | 0 | $param{$prop} = $hr_dayguide->{$prop}; | |||||||||||||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||||||||||||
| 248 | 0 | $param{'broadcasts'} = []; | |||||||||||||||||||||||||||||||||||||
| 249 | 0 | my @broadcasts = $hr_dayguide->{'item'}; | |||||||||||||||||||||||||||||||||||||
| 250 | 0 | ▶ | while ( my $ar_broadcast = shift @broadcasts ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 251 | 0 | push @{ $param{'broadcasts'} }, | |||||||||||||||||||||||||||||||||||||
| 252 | $self->_parse_resource( q{broadcast}, $ar_broadcast->[0] ); | ||||||||||||||||||||||||||||||||||||||
| 253 | } | ||||||||||||||||||||||||||||||||||||||
| 254 | 0 | return WWW::NOS::Open::DayGuide->new(%param); | |||||||||||||||||||||||||||||||||||||
| 255 | } | ||||||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||||
| 257 | 0 | ▶ | sub _parse_guide { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_parse_guide: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||
| 258 | 0 | my ( $self, $body ) = @_; | |||||||||||||||||||||||||||||||||||||
| 259 | 0 | my @dayguides; | |||||||||||||||||||||||||||||||||||||
| 260 | 0 | my $xml = XML::Simple->new( 'ForceArray' => 1 )->XMLin($body); | |||||||||||||||||||||||||||||||||||||
| 261 | 0 | my @xml_dayguides = @{ $xml->{'dayguide'} }; | |||||||||||||||||||||||||||||||||||||
| 262 | 0 | ▶ | while ( my $hr_dayguide = shift @xml_dayguides ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 263 | 0 | push @dayguides, $self->_parse_dayguide($hr_dayguide); | |||||||||||||||||||||||||||||||||||||
| 264 | } | ||||||||||||||||||||||||||||||||||||||
| 265 | 0 | return @dayguides; | |||||||||||||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||||||||
| 268 | 4 | ▶ | sub _get_broadcasts { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_get_broadcasts: called CC=6 SCAR=18.4
| |||||||||||||||||||||||||||||||||||||||
| 269 | 4 | my ( $self, $type, $start, $end, $channel ) = @_; | |||||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||||||
| 271 | 4 | my ( $default_start, $default_end ) = | |||||||||||||||||||||||||||||||||||||
| 272 | __get_date( $DEFAULT_START, $DEFAULT_END ); | ||||||||||||||||||||||||||||||||||||||
| 273 | 4 | ▶ | ( defined $start ) || ( $start = $default_start ); | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 274 | 4 | ▶ | ( defined $end ) || ( $end = $default_end ); | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||||||||
| 276 | 4 | foreach ( $start, $end ) { | |||||||||||||||||||||||||||||||||||||
| 277 | 8 | ▶ | ( q{DateTime} eq ref ) && ( $_ = $_->ymd ); | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 278 | } | ||||||||||||||||||||||||||||||||||||||
| 279 | 4 | ▶ | if ( Delta_Days( split /$DASH/smx, qq{$start$DASH$end} ) > $MAX_RANGE ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 280 | ## no critic qw(RequireExplicitInclusion) | ||||||||||||||||||||||||||||||||||||||
| 281 | NOSOpenExceededRangeException->throw( | ||||||||||||||||||||||||||||||||||||||
| 282 | ## use critic | ||||||||||||||||||||||||||||||||||||||
| 283 | 2 | 'error' => $ERR{'EXCEEDED_RANGE'}, | |||||||||||||||||||||||||||||||||||||
| 284 | ); | ||||||||||||||||||||||||||||||||||||||
| 285 | } | ||||||||||||||||||||||||||||||||||||||
| 286 | 2 | my $url = sprintf $GUIDE_PATH, | |||||||||||||||||||||||||||||||||||||
| 287 | $SERVER, | ||||||||||||||||||||||||||||||||||||||
| 288 | URI::Escape::uri_escape($type), | ||||||||||||||||||||||||||||||||||||||
| 289 | URI::Escape::uri_escape( $self->get_api_key ), | ||||||||||||||||||||||||||||||||||||||
| 290 | URI::Escape::uri_escape( $self->_get_default_output ), | ||||||||||||||||||||||||||||||||||||||
| 291 | URI::Escape::uri_escape($start), | ||||||||||||||||||||||||||||||||||||||
| 292 | URI::Escape::uri_escape($end); | ||||||||||||||||||||||||||||||||||||||
| 293 | 2 | my $response = $self->_do_request($url); | |||||||||||||||||||||||||||||||||||||
| 294 | 0 | my @guide_days = $self->_parse_guide( $response->decoded_content ); | |||||||||||||||||||||||||||||||||||||
| 295 | 0 | return @guide_days; | |||||||||||||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||||||
| 298 | 0 | ▶ | sub get_tv_broadcasts { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
get_tv_broadcasts: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 299 | 0 | my ( $self, @param ) = @_; | |||||||||||||||||||||||||||||||||||||
| 300 | 0 | return $self->_get_broadcasts( q{tv}, @param ); | |||||||||||||||||||||||||||||||||||||
| 301 | } | ||||||||||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||||||||
| 303 | 4 | ▶ | sub get_radio_broadcasts { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
get_radio_broadcasts: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 304 | 4 | my ( $self, @param ) = @_; | |||||||||||||||||||||||||||||||||||||
| 305 | 4 | return $self->_get_broadcasts( q{radio}, @param ); | |||||||||||||||||||||||||||||||||||||
| 306 | } | ||||||||||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||||||||
| 308 | sub __throw { | ||||||||||||||||||||||||||||||||||||||
| 309 | my $json = JSON->new; | ||||||||||||||||||||||||||||||||||||||
| 310 | my %map = ( | ||||||||||||||||||||||||||||||||||||||
| 311 | HTTP::Status::HTTP_BAD_REQUEST => q{NOSOpenBadRequestException}, | ||||||||||||||||||||||||||||||||||||||
| 312 | HTTP::Status::HTTP_UNAUTHORIZED => q{NOSOpenUnauthorizedException}, | ||||||||||||||||||||||||||||||||||||||
| 313 | HTTP::Status::HTTP_FORBIDDEN => q{NOSOpenForbiddenException}, | ||||||||||||||||||||||||||||||||||||||
| 314 | ); | ||||||||||||||||||||||||||||||||||||||
| 315 | my $res = shift; | ||||||||||||||||||||||||||||||||||||||
| 316 | $map{ $res->code } | ||||||||||||||||||||||||||||||||||||||
| 317 | ->throw( 'error' => $json->decode( $res->decoded_content ), ); | ||||||||||||||||||||||||||||||||||||||
| 318 | } | ||||||||||||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||||||||||||
| 320 | 3 | ▶ | sub _do_request { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_do_request: called CC=3 SCAR=12.6
| |||||||||||||||||||||||||||||||||||||||
| 321 | 3 | my ( $self, $url ) = @_; | |||||||||||||||||||||||||||||||||||||
| 322 | 3 | my $request = HTTP::Request->new( | |||||||||||||||||||||||||||||||||||||
| 323 | $GET => $url, | ||||||||||||||||||||||||||||||||||||||
| 324 | HTTP::Headers->new(), | ||||||||||||||||||||||||||||||||||||||
| 325 | ); | ||||||||||||||||||||||||||||||||||||||
| 326 | 3 | $log->debug( sprintf $LOG{'REQUESTING'}, $url ); | |||||||||||||||||||||||||||||||||||||
| 327 | 3 | my $response = $self->_ua->request($request); | |||||||||||||||||||||||||||||||||||||
| 328 | 3 | $log->debug( sprintf $LOG{'RESPONSE_CODE'}, $response->code ); | |||||||||||||||||||||||||||||||||||||
| 329 | 3 | ▶ | if ( $response->code == HTTP_INTERNAL_SERVER_ERROR ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||
| 330 | ## no critic qw(RequireExplicitInclusion) | ||||||||||||||||||||||||||||||||||||||
| 331 | NOSOpenInternalServerErrorException->throw( | ||||||||||||||||||||||||||||||||||||||
| 332 | ## use critic | ||||||||||||||||||||||||||||||||||||||
| 333 | 3 | 'error' => $ERR{'INTERNAL_SERVER'}, | |||||||||||||||||||||||||||||||||||||
| 334 | ); | ||||||||||||||||||||||||||||||||||||||
| 335 | } | ||||||||||||||||||||||||||||||||||||||
| 336 | elsif ( $response->code > HTTP_OK ) { | ||||||||||||||||||||||||||||||||||||||
| 337 | 0 | __throw($response); | |||||||||||||||||||||||||||||||||||||
| 338 | } | ||||||||||||||||||||||||||||||||||||||
| 339 | 0 | return $response; | |||||||||||||||||||||||||||||||||||||
| 340 | } | ||||||||||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||||||||||
| 342 | 4 | ▶ | around 'BUILDARGS' => sub { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.8
| |||||||||||||||||||||||||||||||||||||||
| 343 | 4 | my $orig = shift; | |||||||||||||||||||||||||||||||||||||
| 344 | 4 | my $class = shift; | |||||||||||||||||||||||||||||||||||||
| 345 | 4 | my ( $api_key, $default_output ) = @_; | |||||||||||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||||||||||||
| 347 | 4 | ▶ | return $class->$orig( | ||||||||||||||||||||||||||||||||||||
|
Condition50%3/6
$api_key || $DEFAULT_API_KEY
$default_output || $DEFAULT_OUTPUT
MC/DC0%0/2
$api_key || $DEFAULT_API_KEY
$api_key
$DEFAULT_API_KEY
MC/DC0%0/2
$default_output || $DEFAULT_OUTPUT
$default_output
$DEFAULT_OUTPUT
Truth table66%2/3
A || B
A= $api_keyB= $DEFAULT_API_KEY
Truth table33%1/3
A || B
A= $default_outputB= $DEFAULT_OUTPUT
| |||||||||||||||||||||||||||||||||||||||
| 348 | 'api_key' => $api_key || $DEFAULT_API_KEY, | ||||||||||||||||||||||||||||||||||||||
| 349 | 'default_output' => $default_output || $DEFAULT_OUTPUT, | ||||||||||||||||||||||||||||||||||||||
| 350 | ); | ||||||||||||||||||||||||||||||||||||||
| 351 | 4 | }; | |||||||||||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||||||||||||
| 353 | 4 | with 'WWW::NOS::Open::Interface'; | |||||||||||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||||||||||||
| 355 | 4 | ▶ | no Moose; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||||||||||||
| 357 | 4 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||||||||||||
| 359 | 4 | 1; | |||||||||||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||||||||||||
| 361 | __END__ | ||||||||||||||||||||||||||||||||||||||