| 1 | package Weather::YR::Base; | ||||||||||||||||||||
| 2 | 3 | ▶ | use Moose; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | 3 | ▶ | use namespace::autoclean; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | 3 | ▶ | use DateTime::Format::ISO8601; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 3 | ▶ | use DateTime::TimeZone; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 3 | ▶ | use DateTime; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 3 | ▶ | use LWP::UserAgent; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 3 | ▶ | use Mojo::URL; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 3 | ▶ | use XML::LibXML; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 3 | ▶ | use XML::Simple; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | has 'service_url' => ( | ||||||||||||||||||||
| 14 | isa => 'Mojo::URL', | ||||||||||||||||||||
| 15 | is => 'ro', | ||||||||||||||||||||
| 16 | lazy => 1, | ||||||||||||||||||||
| 17 | 3 | ▶ | default => sub { Mojo::URL->new('https://api.met.no') }, | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 18 | 3 | ); | |||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | 3 | has [ 'lat', 'lon', 'msl' ] => ( | |||||||||||||||||||
| 21 | isa => 'Num', | ||||||||||||||||||||
| 22 | is => 'rw', | ||||||||||||||||||||
| 23 | required => 0, | ||||||||||||||||||||
| 24 | default => 0, | ||||||||||||||||||||
| 25 | ); | ||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | 3 | has 'xml' => ( | |||||||||||||||||||
| 28 | isa => 'Maybe[Str]', | ||||||||||||||||||||
| 29 | is => 'rw', | ||||||||||||||||||||
| 30 | required => 0, | ||||||||||||||||||||
| 31 | ); | ||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | 3 | has 'lang' => ( | |||||||||||||||||||
| 34 | isa => 'Str', | ||||||||||||||||||||
| 35 | is => 'rw', | ||||||||||||||||||||
| 36 | required => 0, | ||||||||||||||||||||
| 37 | default => 'en', | ||||||||||||||||||||
| 38 | ); | ||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | has 'tz' => ( | ||||||||||||||||||||
| 41 | isa => 'DateTime::TimeZone', | ||||||||||||||||||||
| 42 | is => 'rw', | ||||||||||||||||||||
| 43 | required => 0, | ||||||||||||||||||||
| 44 | 1 | ▶ | default => sub { DateTime::TimeZone->new( name => 'UTC' ); }, | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 45 | 3 | ); | |||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | has 'ua' => ( | ||||||||||||||||||||
| 48 | isa => 'Object', | ||||||||||||||||||||
| 49 | is => 'rw', | ||||||||||||||||||||
| 50 | required => 0, | ||||||||||||||||||||
| 51 | 2 | ▶ | default => sub { LWP::UserAgent->new; }, | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 52 | 3 | ); | |||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | 3 | has 'xml_ref' => ( | |||||||||||||||||||
| 55 | isa => 'Maybe[HashRef]', | ||||||||||||||||||||
| 56 | is => 'ro', | ||||||||||||||||||||
| 57 | lazy_build => 1, | ||||||||||||||||||||
| 58 | ); | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | # Make sure that 'lat' and 'lon' is restricted to four decimals, ref. | ||||||||||||||||||||
| 61 | # https://api.met.no/doc/TermsOfService#traffic | ||||||||||||||||||||
| 62 | 10 | ▶ | around [ 'lat', 'lon' ] => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 63 | 10 | my $orig = shift; | |||||||||||||||||||
| 64 | 10 | my $self = shift; | |||||||||||||||||||
| 65 | 10 | ▶ | my $value = shift || $self->$orig; | ||||||||||||||||||
|
Condition100%3/3
shift() || $self->$orig
MC/DC100%2/2
shift() || $self->$orig
shift()
$self->$orig
Truth table100%3/3
A || B
A= shift()B= $self->$orig
| |||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | 10 | $value = sprintf( '%0.4f', $value ); | |||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | 10 | return $self->$orig( $value ); | |||||||||||||||||||
| 70 | 3 | }; | |||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | 1 | ▶ | sub _build_xml_ref { | ||||||||||||||||||
|
Subroutine
_build_xml_ref: called CC=8 SCAR=26.9
| |||||||||||||||||||||
| 73 | 1 | my $self = shift; | |||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | 1 | ▶ | unless ( length $self->xml ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 76 | 0 | my $response = $self->ua->get( $self->url->to_string ); | |||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | 0 | ▶ | if ( $self->can('status_code') ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 79 | 0 | $self->status_code( $response->code ); | |||||||||||||||||||
| 80 | } | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | 0 | ▶ | if ( $response->is_success ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 83 | 0 | $self->xml( $response->decoded_content ); | |||||||||||||||||||
| 84 | } | ||||||||||||||||||||
| 85 | else { | ||||||||||||||||||||
| 86 | 0 | warn "Failed to GET data from " . $self->url->to_string; | |||||||||||||||||||
| 87 | } | ||||||||||||||||||||
| 88 | } | ||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | 1 | ▶ | if ( length $self->xml ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 91 | 1 | ▶ | if ( $self->can('schema_url') ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 92 | 1 | eval { | |||||||||||||||||||
| 93 | 1 | my $xml_doc = XML::LibXML->new->load_xml( string => $self->xml ); | |||||||||||||||||||
| 94 | 1 | my $response = $self->ua->get( $self->schema_url->to_string ); | |||||||||||||||||||
| 95 | 1 | my $schema = XML::LibXML::Schema->new( string => $response->decoded_content ); | |||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | 1 | $schema->validate( $xml_doc ); | |||||||||||||||||||
| 98 | }; | ||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | 1 | ▶ | if ( $@ ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 101 | 0 | warn "Failed to validate the XML returned from YR.no using schema URL '" . $self->schema_url . "'; $@"; | |||||||||||||||||||
| 102 | } | ||||||||||||||||||||
| 103 | else { | ||||||||||||||||||||
| 104 | 1 | my $result = undef; | |||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | 1 | eval { | |||||||||||||||||||
| 107 | 1 | $result = XML::Simple::XMLin( $self->xml, ForceArray => 0 ); | |||||||||||||||||||
| 108 | }; | ||||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | 1 | ▶ | unless ( $@ ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 111 | 1 | return $result; | |||||||||||||||||||
| 112 | } | ||||||||||||||||||||
| 113 | } | ||||||||||||||||||||
| 114 | } | ||||||||||||||||||||
| 115 | } | ||||||||||||||||||||
| 116 | else { | ||||||||||||||||||||
| 117 | 0 | warn "No XML to parse!"; | |||||||||||||||||||
| 118 | } | ||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | # Something failed! | ||||||||||||||||||||
| 121 | 0 | return undef; | |||||||||||||||||||
| 122 | } | ||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | 684 | ▶ | sub date_to_datetime { | ||||||||||||||||||
|
Subroutine
date_to_datetime: called CC=3 SCAR=11.6
Pod
undocumented
| |||||||||||||||||||||
| 125 | 684 | my $self = shift; | |||||||||||||||||||
| 126 | 684 | ▶ | my $date = shift // ''; | ||||||||||||||||||
|
Condition50%1/2
shift() // ""
MC/DC0%0/1
shift() // ""
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | 684 | ▶ | if ( length $date ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 129 | 684 | $date = DateTime::Format::ISO8601->parse_datetime( $date ); | |||||||||||||||||||
| 130 | } | ||||||||||||||||||||
| 131 | else { | ||||||||||||||||||||
| 132 | 0 | $date = DateTime->now; | |||||||||||||||||||
| 133 | } | ||||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | 684 | $date->set_time_zone( $self->tz ); | |||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | 684 | return $date; | |||||||||||||||||||
| 138 | } | ||||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | 3 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | 3 | 1; | |||||||||||||||||||