OpenWeatherMap/Result.pm

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