| 1 | package Weather::OpenWeatherMap::Result; | ||||||||||||
| 2 | 3 | $Weather::OpenWeatherMap::Result::VERSION = '0.003001'; | |||||||||||
| 3 | 5 | ▶ | use Carp; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 5 | ▶ | use strictures 2; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | |||||||||||||
| 6 | 5 | ▶ | use JSON::MaybeXS (); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | |||||||||||||
| 8 | 5 | ▶ | use Module::Runtime 'use_module'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | 5 | ▶ | use List::Objects::Types -all; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | 7 | ▶ | use Types::Standard -all; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | |||||||||||||
| 12 | |||||||||||||
| 13 | 7 | ▶ | use Moo; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 14 | |||||||||||||
| 15 | 3 | ▶ | use Storable 'freeze'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 16 | |||||||||||||
| 17 | sub new_for { | ||||||||||||
| 18 | 6 | ▶ | my ($class, $type) = splice @_, 0, 2; | ||||||||||
|
Subroutine
new_for: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||
| 19 | 6 | ▶ | confess "Expected a subclass type" unless $type; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 20 | 6 | my $subclass = join '::', $class, map ucfirst, split '::', $type; | |||||||||||
| 21 | 6 | use_module($subclass)->new(@_) | |||||||||||
| 22 | } | ||||||||||||
| 23 | |||||||||||||
| 24 | sub decode_json { | ||||||||||||
| 25 | 12 | ▶ | my (undef, $js) = @_; | ||||||||||
|
Subroutine
decode_json: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 26 | 12 | JSON::MaybeXS->new(utf8 => 1)->decode( $js ) | |||||||||||
| 27 | } | ||||||||||||
| 28 | |||||||||||||
| 29 | sub encode_json { | ||||||||||||
| 30 | 2 | ▶ | my (undef, $data) = @_; | ||||||||||
|
Subroutine
encode_json: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 31 | 2 | JSON::MaybeXS->new(utf8 => 1)->encode( $data ) | |||||||||||
| 32 | } | ||||||||||||
| 33 | |||||||||||||
| 34 | |||||||||||||
| 35 | 3 | has request => ( | |||||||||||
| 36 | required => 1, | ||||||||||||
| 37 | is => 'ro', | ||||||||||||
| 38 | isa => InstanceOf['Weather::OpenWeatherMap::Request'], | ||||||||||||
| 39 | ); | ||||||||||||
| 40 | |||||||||||||
| 41 | 3 | has json => ( | |||||||||||
| 42 | required => 1, | ||||||||||||
| 43 | is => 'ro', | ||||||||||||
| 44 | isa => Str, | ||||||||||||
| 45 | ); | ||||||||||||
| 46 | |||||||||||||
| 47 | has data => ( | ||||||||||||
| 48 | lazy => 1, | ||||||||||||
| 49 | is => 'ro', | ||||||||||||
| 50 | isa => HashObj, | ||||||||||||
| 51 | coerce => 1, | ||||||||||||
| 52 | builder => sub { | ||||||||||||
| 53 | 8 | ▶ | my ($self) = @_; | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 54 | 8 | $self->decode_json( $self->json ) | |||||||||||
| 55 | }, | ||||||||||||
| 56 | 3 | ); | |||||||||||
| 57 | |||||||||||||
| 58 | has response_code => ( | ||||||||||||
| 59 | lazy => 1, | ||||||||||||
| 60 | is => 'ro', | ||||||||||||
| 61 | isa => Maybe[Int], | ||||||||||||
| 62 | builder => sub { | ||||||||||||
| 63 | 8 | ▶ | my ($self) = @_; | ||||||||||
|
Subroutine
_build_response_code: called CC=1 SCAR=0.0
| |||||||||||||
| 64 | $self->data->{cod} | ||||||||||||
| 65 | 8 | }, | |||||||||||
| 66 | 3 | ); | |||||||||||
| 67 | |||||||||||||
| 68 | |||||||||||||
| 69 | has is_success => ( | ||||||||||||
| 70 | lazy => 1, | ||||||||||||
| 71 | is => 'ro', | ||||||||||||
| 72 | isa => Bool, | ||||||||||||
| 73 | builder => sub { | ||||||||||||
| 74 | 12 | ▶ | my ($self) = @_; | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
| |||||||||||||
| 75 | 12 | ▶ | ($self->response_code // '') eq '200' | ||||||||||
|
Condition50%1/2
$self->response_code // ""
MC/DC0%0/1
$self->response_code // ""
$self->response_code
Truth table50%1/2
A
A= $self->response_code
| |||||||||||||
| 76 | }, | ||||||||||||
| 77 | 3 | ); | |||||||||||
| 78 | |||||||||||||
| 79 | has error => ( | ||||||||||||
| 80 | lazy => 1, | ||||||||||||
| 81 | is => 'ro', | ||||||||||||
| 82 | isa => Str, | ||||||||||||
| 83 | builder => sub { | ||||||||||||
| 84 | 12 | ▶ | my ($self) = @_; | ||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.0
| |||||||||||||
| 85 | 12 | ▶ | return '' if $self->is_success; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 86 | 6 | my $data = $self->data; | |||||||||||
| 87 | 6 | ▶ | my $msg = $data->{message} || 'Unknown error from backend'; | ||||||||||
|
Condition50%1/2
$data->{'message'} || 'Unknown error from backend'
MC/DC0%0/1
$data->{'message'} || 'Unknown error from backend'
$data->{'message'}
Truth table50%1/2
A
A= $data->{'message'}
| |||||||||||||
| 88 | # there's only so much I can take -> | ||||||||||||
| 89 | 6 | ▶ | $msg = 'Not found' if $msg =~ /Not found city/; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 90 | 6 | $msg | |||||||||||
| 91 | }, | ||||||||||||
| 92 | 3 | ); | |||||||||||
| 93 | |||||||||||||
| 94 | |||||||||||||
| 95 | 3 | 1; | |||||||||||
| 96 | |||||||||||||
| 97 | =pod | ||||||||||||
| 98 | |||||||||||||
| 99 | =head1 NAME | ||||||||||||
| 100 | |||||||||||||
| 101 | Weather::OpenWeatherMap::Result - Weather lookup result superclass | ||||||||||||
| 102 | |||||||||||||
| 103 | =head1 SYNOPSIS | ||||||||||||
| 104 | |||||||||||||
| 105 | # Normally retrieved via Weather::OpenWeatherMap | ||||||||||||
| 106 | |||||||||||||
| 107 | =head1 DESCRIPTION | ||||||||||||
| 108 | |||||||||||||
| 109 | This is the parent class for L<Weather::OpenWeatherMap> weather results. | ||||||||||||
| 110 | |||||||||||||
| 111 | The L</"SEE ALSO"> section links known subclasses. | ||||||||||||
| 112 | |||||||||||||
| 113 | =head2 ATTRIBUTES | ||||||||||||
| 114 | |||||||||||||
| 115 | =head3 data | ||||||||||||
| 116 | |||||||||||||
| 117 | This is the decoded hash from the attached L</json> (as a | ||||||||||||
| 118 | L<List::Objects::WithUtils::Hash>). | ||||||||||||
| 119 | |||||||||||||
| 120 | Subclasses provide more convenient accessors for retrieving desired | ||||||||||||
| 121 | information. | ||||||||||||
| 122 | |||||||||||||
| 123 | =head3 error | ||||||||||||
| 124 | |||||||||||||
| 125 | The error message received from the OpenWeatherMap backend (or the empty | ||||||||||||
| 126 | string if there was no error). | ||||||||||||
| 127 | |||||||||||||
| 128 | See also: L</is_success>, L</response_code> | ||||||||||||
| 129 | |||||||||||||
| 130 | =head3 is_success | ||||||||||||
| 131 | |||||||||||||
| 132 | Returns boolean true if the OpenWeatherMap backend returned a successful | ||||||||||||
| 133 | response. | ||||||||||||
| 134 | |||||||||||||
| 135 | See also: L</error>, L</response_code> | ||||||||||||
| 136 | |||||||||||||
| 137 | =head3 json | ||||||||||||
| 138 | |||||||||||||
| 139 | The raw JSON this Result was created with. | ||||||||||||
| 140 | |||||||||||||
| 141 | =head3 response_code | ||||||||||||
| 142 | |||||||||||||
| 143 | The response code from OpenWeatherMap. | ||||||||||||
| 144 | |||||||||||||
| 145 | See also: L</is_success>, L</error> | ||||||||||||
| 146 | |||||||||||||
| 147 | =head3 request | ||||||||||||
| 148 | |||||||||||||
| 149 | The original request that was attached to this result. | ||||||||||||
| 150 | |||||||||||||
| 151 | =head2 METHODS | ||||||||||||
| 152 | |||||||||||||
| 153 | =head3 new_for | ||||||||||||
| 154 | |||||||||||||
| 155 | Factory method; returns a new object belonging to the appropriate subclass: | ||||||||||||
| 156 | |||||||||||||
| 157 | my $result = Weather::OpenWeatherMap::Result->new_for( | ||||||||||||
| 158 | Forecast => | ||||||||||||
| 159 | request => $orig_request, | ||||||||||||
| 160 | json => $raw_json, | ||||||||||||
| 161 | ); | ||||||||||||
| 162 | |||||||||||||
| 163 | =head3 decode_json | ||||||||||||
| 164 | |||||||||||||
| 165 | Result deserialization wrapper for use by subclasses. | ||||||||||||
| 166 | |||||||||||||
| 167 | =head3 encode_json | ||||||||||||
| 168 | |||||||||||||
| 169 | Serialization wrapper for use by subclasses. | ||||||||||||
| 170 | |||||||||||||
| 171 | =head1 SEE ALSO | ||||||||||||
| 172 | |||||||||||||
| 173 | L<http://www.openweathermap.org/api> | ||||||||||||
| 174 | |||||||||||||
| 175 | L<Weather::OpenWeatherMap::Result::Current> | ||||||||||||
| 176 | |||||||||||||
| 177 | L<Weather::OpenWeatherMap::Result::Forecast> | ||||||||||||
| 178 | |||||||||||||
| 179 | L<Weather::OpenWeatherMap::Result::Forecast::Block> | ||||||||||||
| 180 | |||||||||||||
| 181 | L<Weather::OpenWeatherMap::Result::Forecast::Day> | ||||||||||||
| 182 | |||||||||||||
| 183 | L<Weather::OpenWeatherMap::Result::Forecast::Hour> | ||||||||||||
| 184 | |||||||||||||
| 185 | L<Weather::OpenWeatherMap::Result::Find> | ||||||||||||
| 186 | |||||||||||||
| 187 | L<Weather::OpenWeatherMap::Request> | ||||||||||||
| 188 | |||||||||||||
| 189 | =head1 AUTHOR | ||||||||||||
| 190 | |||||||||||||
| 191 | Jon Portnoy <avenj@cobaltirc.org> | ||||||||||||
| 192 | |||||||||||||
| 193 | Licensed under the same terms as perl. | ||||||||||||
| 194 | |||||||||||||
| 195 | =cut | ||||||||||||