OpenWeatherMap/Request.pm

filter: Statementstmt 97.4% 77 / 79 Branchbran 70.0% 7 / 10 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 91.3% 21 / 23 Podpod 100% 1 / 1 totaltotal 87.8% 108 / 123 CC 8 SCAR 6.1
CC
8
Cov
91%
__ANON__
3.7
_build_http_request
3.7
_parse_location_str
3.0
CRAP 1.8
« OpenWeatherMap/Result.pm ↑ Summary OpenWeatherMap/Result/Forecast/Block.pm »
1 package Weather::OpenWeatherMap::Request;
2 10 $Weather::OpenWeatherMap::Request::VERSION = '0.003001';
3 10 ▶use feature 'state';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 10 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 10 ▶use Types::Standard -all;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use Types::DateTime -all;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 10 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 10 ▶use URI::Escape 'uri_escape_utf8';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 10 ▶use Module::Runtime 'use_module';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15  
16 10 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 10 ▶use Storable 'freeze';
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 sub new_for {
21 19 ▶ my ($class, $type) = splice @_, 0, 2;
Subroutine new_for: called CC=2 SCAR=7.0
Pod documented
22 19 ▶ confess "Expected a subclass type" unless $type;
Branch: 1 branch
BranchTrueFalseTotal
unless $type 0 19 19
23 19 my $subclass = join '::', $class, map ucfirst, split '::', $type;
24 19 use_module($subclass)->new(@_)
25 }
26  
27 =pod
28  
29 =for Pod::Coverage has_\w+
30  
31 =cut
32  
33 has api_key => (
34 lazy => 1,
35 is => 'ro',
36 isa => Str,
37 predicate => 1,
38 0 ▶ builder => sub { '' },
Subroutine __ANON__: not called CC=1 SCAR=6.9
39 10 );
40  
41  
42 has tag => (
43 lazy => 1,
44 is => 'ro',
45 predicate => 1,
46 0 ▶ builder => sub { '' },
Subroutine __ANON__: not called CC=1 SCAR=6.9
47 10 );
48  
49 10 has location => (
50 required => 1,
51 is => 'ro',
52 isa => Str,
53 );
54  
55  
56 has ts => (
57 is => 'ro',
58 isa => StrictNum,
59 11 ▶ builder => sub { time },
Subroutine _build_ts: called CC=1 SCAR=0.0
60 10 );
61  
62 has url => (
63 init_arg => undef,
64 lazy => 1,
65 is => 'ro',
66 isa => Str,
67 8 ▶ builder => sub { shift->_parse_location_str },
Subroutine _build_url: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
68 10 );
69  
70 has http_request => (
71 lazy => 1,
72 is => 'ro',
73 isa => InstanceOf['HTTP::Request'],
74 builder => sub {
75 6 ▶ my ($self) = @_;
Subroutine __ANON__: called CC=3 SCAR=13.1
76 6 my $req = HTTP::Request->new( GET => $self->url );
77 6 ▶ $req->header( 'x-api-key' => $self->api_key )
Branch: 2 branches
BranchTrueFalseTotal
if $self->has_api_key and length $self->api_key 6 0 6
if $self->has_api_key and length $self->api_key 2 0 2
Condition33%2/6
$self->has_api_key and length $self->api_key
!ll&&!rl&&r
006
$self->has_api_key and length $self->api_key
!ll&&!rl&&r
002
MC/DC0%0/2
$self->has_api_key and length $self->api_key
$self->has_api_key length $self->api_key
MC/DC0%0/2
$self->has_api_key and length $self->api_key
$self->has_api_key length $self->api_key
Truth table33%1/3
A and B
A= $self->has_api_keyB= length $self->api_key
ABresult
0X0
100
111
Truth table33%1/3
A and B
A= $self->has_api_keyB= length $self->api_key
ABresult
0X0
100
111
78 if $self->has_api_key and length $self->api_key;
79 6 $req
80 },
81 10 );
82  
83  
84 has _units => (
85 required => 1,
86 is => 'ro',
87 isa => Str,
88 19 ▶ builder => sub { 'imperial' },
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine _build__units: called CC=1 SCAR=0.0
89 10 );
90  
91  
92  
93 sub _url_bycode {
94 1 ▶ my ($self, $code) = @_;
Subroutine _url_bycode: called CC=1 SCAR=0.0
95 1 'http://api.openweathermap.org/data/2.5/weather?id='
96 . uri_escape_utf8($code)
97 . '&units=' . $self->_units
98 }
99  
100 sub _url_bycoord {
101 1 ▶ my $self = shift;
Subroutine _url_bycoord: called CC=1 SCAR=0.0
102 1 my ($lat, $long) = map {; uri_escape_utf8($_) } @_;
103 1 "http://api.openweathermap.org/data/2.5/weather?lat=$lat&lon=$long"
104 . '&units=' . $self->_units
105 }
106  
107 sub _url_byname {
108 3 ▶ my ($self, @parts) = @_;
Subroutine _url_byname: called CC=1 SCAR=0.0
109 'http://api.openweathermap.org/data/2.5/weather?q='
110 3 . join(',', map {; uri_escape_utf8($_) } @parts)
111 . '&units=' . $self->_units
112 }
113  
114  
115 sub _parse_location_str {
116 15 ▶ my ($self) = @_;
Subroutine _parse_location_str: called CC=3 SCAR=11.0
117  
118 15 state $latlong =
119 qr{\Alat(?:itude)?\s+?(-?[0-9.]+),?\s+?long?(?:itude)?\s+?(-?[0-9.]+)};
120  
121 15 my $str = $self->location;
122 15 my $url;
123 URL: {
124 15 ▶ if (is_StrictNum($str)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&is_StrictNum($str)) 3 12 15
125 3 $url = $self->_url_bycode($str);
126 last URL
127 3 }
128  
129 12 ▶ if (my ($lat, $lon) = $str =~ $latlong) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $str =~ /$latlong/) 4 8 12
130 4 $url = $self->_url_bycoord($lat, $lon);
131 last URL
132 4 }
133  
134 8 my @parts = split /,\s+?/, $str;
135 8 $url = $self->_url_byname(@parts);
136 }
137  
138 $url
139 15 }
140  
141 10 1;
142  
143 =pod
144  
145 =head1 NAME
146  
147 Weather::OpenWeatherMap::Request - Weather lookup request superclass
148  
149 =head1 SYNOPSIS
150  
151 # Normally generated via Weather::OpenWeatherMap
152 # (when a request is made)
153  
154 =head1 DESCRIPTION
155  
156 This is the parent class for request objects generated by
157 L<Weather::OpenWeatherMap>.
158  
159 The L</"SEE ALSO"> section links known subclasses.
160  
161 =head2 ATTRIBUTES
162  
163 =head3 api_key
164  
165 The L<OpenWeatherMap API key|http://www.weathermap.org/api> attached to this
166 request.
167  
168 =head3 location
169  
170 B<Required:>
171  
172 The location string, used to (lazily) generate the L</http_request> and L</url>
173 attributes.
174  
175 Locations can be specified in various ways.
176  
177 Strings such as 'City, State' or 'City, Country' will be parsed appropriately.
178  
179 A numeric location is taken to be an
180 L<OpenWeatherMap|http://www.openweathermap.org> city code.
181  
182 A string in the form of 'lat X, long Y' or 'latitude X, longitude Y' is also
183 accepted.
184  
185 =head3 tag
186  
187 An (optional) arbitrary scalar attached to the request object at construction
188 time.
189  
190 =head3 http_request
191  
192 The (generated) L<HTTP::Request> object used to request the weather data.
193  
194 =head3 ts
195  
196 The C<time()> the request object was created.
197  
198 =head3 url
199  
200 The (generated) L<OpenWeatherMap|http://www.openweathermap.org/> URL.
201  
202 =head2 METHODS
203  
204 =head3 new_for
205  
206 Factory method; returns a new object belonging to the appropriate subclass:
207  
208 my $request = Weather::OpenWeatherMap::Request->new_for(
209 Current =>
210 api_key => $api_key,
211 location => $location,
212 tag => $tag,
213 );
214  
215 =head1 SEE ALSO
216  
217 L<Weather::OpenWeatherMap::Request::Current>
218  
219 L<Weather::OpenWeatherMap::Request::Forecast>
220  
221 L<Weather::OpenWeatherMap::Request::Find>
222  
223 L<Weather::OpenWeatherMap::Result>
224  
225 =head1 AUTHOR
226  
227 Jon Portnoy <avenj@cobaltirc.org>
228  
229 =cut
230  
« OpenWeatherMap/Result.pm ↑ Summary OpenWeatherMap/Result/Forecast/Block.pm »