| 1 | package Weather::OpenWeatherMap; | ||
| 2 | $Weather::OpenWeatherMap::VERSION = '0.005001'; | ||
| 3 | 1 | ▶ | use strictures 2; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | 1 | ▶ | use LWP::UserAgent; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | 1 | ▶ | use Types::Standard -all; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | |||
| 11 | 1 | ▶ | use Weather::OpenWeatherMap::Cache; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 12 | use Weather::OpenWeatherMap::Error; | ||
| 13 | |||
| 14 | |||
| 15 | # Full require list to make Storable retrievals comfortable: | ||
| 16 | use Weather::OpenWeatherMap::Request; | ||
| 17 | require Weather::OpenWeatherMap::Request::Current; | ||
| 18 | require Weather::OpenWeatherMap::Request::Forecast; | ||
| 19 | |||
| 20 | use Weather::OpenWeatherMap::Result; | ||
| 21 | require Weather::OpenWeatherMap::Result::Current; | ||
| 22 | require Weather::OpenWeatherMap::Result::Forecast; | ||
| 23 | |||
| 24 | use Moo; | ||
| 25 | |||
| 26 | =pod | ||
| 27 | |||
| 28 | =for Pod::Coverage has_api_key | ||
| 29 | |||
| 30 | =cut | ||
| 31 | |||
| 32 | has api_key => ( | ||
| 33 | lazy => 1, | ||
| 34 | is => 'ro', | ||
| 35 | isa => Str, | ||
| 36 | predicate => 1, | ||
| 37 | builder => sub { '' }, | ||
| 38 | ); | ||
| 39 | |||
| 40 | has cache => ( | ||
| 41 | lazy => 1, | ||
| 42 | is => 'ro', | ||
| 43 | isa => Bool, | ||
| 44 | builder => sub { 0 }, | ||
| 45 | ); | ||
| 46 | |||
| 47 | has cache_dir => ( | ||
| 48 | lazy => 1, | ||
| 49 | is => 'ro', | ||
| 50 | isa => Maybe[Str], | ||
| 51 | builder => sub { undef }, | ||
| 52 | ); | ||
| 53 | |||
| 54 | has cache_expiry => ( | ||
| 55 | lazy => 1, | ||
| 56 | is => 'ro', | ||
| 57 | isa => Maybe[StrictNum], | ||
| 58 | builder => sub { undef }, | ||
| 59 | ); | ||
| 60 | |||
| 61 | has _cache => ( | ||
| 62 | lazy => 1, | ||
| 63 | is => 'ro', | ||
| 64 | isa => InstanceOf['Weather::OpenWeatherMap::Cache'], | ||
| 65 | builder => sub { | ||
| 66 | my ($self) = @_; | ||
| 67 | Weather::OpenWeatherMap::Cache->new( | ||
| 68 | ( $self->cache_dir ? (dir => $self->cache_dir) : () ), | ||
| 69 | ( $self->cache_expiry ? (expiry => $self->cache_expiry) : () ), | ||
| 70 | ) | ||
| 71 | }, | ||
| 72 | ); | ||
| 73 | |||
| 74 | |||
| 75 | has ua => ( | ||
| 76 | is => 'ro', | ||
| 77 | isa => InstanceOf['LWP::UserAgent'], | ||
| 78 | builder => sub { LWP::UserAgent->new(timeout => 60) }, | ||
| 79 | ); | ||
| 80 | |||
| 81 | |||
| 82 | sub get_weather { | ||
| 83 | my ($self, %args) = @_; | ||
| 84 | |||
| 85 | croak "Missing 'location =>' in query" unless $args{location}; | ||
| 86 | |||
| 87 | my $type = | ||
| 88 | delete $args{forecast} ? 'Forecast' | ||
| 89 | : delete $args{find} ? 'Find' | ||
| 90 | : 'Current'; | ||
| 91 | |||
| 92 | my $my_request = Weather::OpenWeatherMap::Request->new_for( | ||
| 93 | $type => | ||
| 94 | ( | ||
| 95 | $self->has_api_key && length $self->api_key ? | ||
| 96 | ( api_key => $self->api_key ) : () | ||
| 97 | ), | ||
| 98 | %args | ||
| 99 | ); | ||
| 100 | |||
| 101 | my $result; | ||
| 102 | |||
| 103 | if ( $self->cache && (my $cached = $self->_cache->retrieve($my_request)) ) { | ||
| 104 | $result = $cached->object | ||
| 105 | } else { | ||
| 106 | my $http_response = $self->ua->request( $my_request->http_request ); | ||
| 107 | |||
| 108 | unless ($http_response->is_success) { | ||
| 109 | die Weather::OpenWeatherMap::Error->new( | ||
| 110 | request => $my_request, | ||
| 111 | source => 'http', | ||
| 112 | status => $http_response->status_line, | ||
| 113 | ); | ||
| 114 | } | ||
| 115 | |||
| 116 | $result = Weather::OpenWeatherMap::Result->new_for( | ||
| 117 | $type => | ||
| 118 | request => $my_request, | ||
| 119 | json => $http_response->content, | ||
| 120 | ( | ||
| 121 | $my_request->can('hourly') && $my_request->hourly ? | ||
| 122 | (hourly => 1) : () | ||
| 123 | ), | ||
| 124 | ); | ||
| 125 | } | ||
| 126 | |||
| 127 | unless ($result->is_success) { | ||
| 128 | die Weather::OpenWeatherMap::Error->new( | ||
| 129 | request => $my_request, | ||
| 130 | source => 'api', | ||
| 131 | status => $result->error, | ||
| 132 | ) | ||
| 133 | } | ||
| 134 | |||
| 135 | $self->_cache->cache($result) if $self->cache; | ||
| 136 | |||
| 137 | $result | ||
| 138 | } | ||
| 139 | |||
| 140 | |||
| 141 | 1; | ||
| 142 | |||
| 143 | =pod | ||
| 144 | |||
| 145 | =head1 NAME | ||
| 146 | |||
| 147 | Weather::OpenWeatherMap - Interface to the OpenWeatherMap API | ||
| 148 | |||
| 149 | =head1 SYNOPSIS | ||
| 150 | |||
| 151 | use Weather::OpenWeatherMap; | ||
| 152 | |||
| 153 | my $api_key = 'foo'; | ||
| 154 | |||
| 155 | my $wx = Weather::OpenWeatherMap->new( | ||
| 156 | api_key => $api_key, | ||
| 157 | ); | ||
| 158 | |||
| 159 | # Current conditions: | ||
| 160 | # (see Weather::OpenWeatherMap::Result::Current) | ||
| 161 | my $current = $wx->get_weather( | ||
| 162 | location => 'Manchester, NH', | ||
| 163 | ); | ||
| 164 | my $tempf = $current->temp_f; | ||
| 165 | my $wind = $current->wind_speed_mph; | ||
| 166 | |||
| 167 | # Daily forecast conditions: | ||
| 168 | # (see Weather::OpenWeatherMap::Result::Forecast) | ||
| 169 | my $forecast = $wx->get_weather( | ||
| 170 | location => 'Manchester, NH', | ||
| 171 | forecast => 1, | ||
| 172 | days => 3, | ||
| 173 | ); | ||
| 174 | for my $day ($forecast->list) { | ||
| 175 | my $date = $day->dt->mdy; | ||
| 176 | my $temp_lo = $day->temp_min_f, | ||
| 177 | my $temp_hi = $day->temp_max_f, | ||
| 178 | # (see Weather::OpenWeatherMap::Result::Forecast::Day) | ||
| 179 | } | ||
| 180 | |||
| 181 | # Hourly (3-hr blocks) forecast conditions: | ||
| 182 | my $forecast = $wx->get_weather( | ||
| 183 | location => 'Manchester, NH', | ||
| 184 | forecast => 1, | ||
| 185 | hourly => 1, | ||
| 186 | days => 3, | ||
| 187 | ); | ||
| 188 | for my $block ($forecast->list) { | ||
| 189 | my $time = $block->dt_txt; | ||
| 190 | my $temp = $block->temp; | ||
| 191 | # (see Weather::OpenWeatherMap::Result::Forecast::Hour) | ||
| 192 | } | ||
| 193 | |||
| 194 | # Find a city: | ||
| 195 | # (see Weather::OpenWeatherMap::Result::Find) | ||
| 196 | my $search = $wx->get_weather( | ||
| 197 | location => 'Manchester', | ||
| 198 | find => 1, | ||
| 199 | max => 5, | ||
| 200 | ); | ||
| 201 | for my $place ($search->list) { | ||
| 202 | my $region = $place->country; | ||
| 203 | # ... | ||
| 204 | } | ||
| 205 | |||
| 206 | =head1 DESCRIPTION | ||
| 207 | |||
| 208 | An object-oriented interface to retrieving weather conditions & forecasts from | ||
| 209 | B<OpenWeatherMap> (L<http://www.openweathermap.org/>) for a given city, | ||
| 210 | latitude/longitude, or OpenWeatherMap city code. | ||
| 211 | |||
| 212 | This module provides a simple blocking (L<LWP::UserAgent>) interface to | ||
| 213 | weather retrieval; if you have an event loop handy, the included | ||
| 214 | L<Weather::OpenWeatherMap::Request> & L<Weather::OpenWeatherMap::Result> | ||
| 215 | classes can be used to create appropriate L<HTTP::Request> instances and parse | ||
| 216 | responses from non-blocking HTTP clients. | ||
| 217 | |||
| 218 | See L<POEx::Weather::OpenWeatherMap> for a non-blocking implementation using | ||
| 219 | the L<POE> ecosystem. | ||
| 220 | |||
| 221 | =head2 ATTRIBUTES | ||
| 222 | |||
| 223 | =head3 api_key | ||
| 224 | |||
| 225 | Your L<OpenWeatherMap|http://www.openweathermap.org/> API key. | ||
| 226 | |||
| 227 | (See L<http://www.openweathermap.org/api> to register for free.) | ||
| 228 | |||
| 229 | =head3 cache | ||
| 230 | |||
| 231 | A boolean value indicating whether successful results should be cached to | ||
| 232 | disk via L<Weather::OpenWeatherMap::Cache>. | ||
| 233 | |||
| 234 | Defaults to false. This may change in a future release. | ||
| 235 | |||
| 236 | =head3 cache_dir | ||
| 237 | |||
| 238 | The directory in which cache files are saved. The default may be fine; see | ||
| 239 | L<Weather::OpenWeatherMap::Cache>. | ||
| 240 | |||
| 241 | =head3 cache_expiry | ||
| 242 | |||
| 243 | The duration (in seconds) for which cache files are considered valid; see | ||
| 244 | L<Weather::OpenWeatherMap::Cache>. | ||
| 245 | |||
| 246 | =head3 ua | ||
| 247 | |||
| 248 | The L<LWP::UserAgent> instance used to issue HTTP requests; can be used to | ||
| 249 | control LWP options: | ||
| 250 | |||
| 251 | my $wx = Weather::OpenWeatherMap->new( | ||
| 252 | api_key => $my_api_key, | ||
| 253 | ua => LWP::UserAgent->new(%lwp_opts), | ||
| 254 | ); | ||
| 255 | |||
| 256 | =head2 METHODS | ||
| 257 | |||
| 258 | =head3 get_weather | ||
| 259 | |||
| 260 | $wx->get_weather( | ||
| 261 | # 'location =>' is mandatory. | ||
| 262 | # These are all valid location strings: | ||
| 263 | # By name: | ||
| 264 | # 'Manchester, NH' | ||
| 265 | # 'London, UK' | ||
| 266 | # By OpenWeatherMap city code: | ||
| 267 | # 5089178 | ||
| 268 | # By latitude/longitude: | ||
| 269 | # 'lat 42, long -71' | ||
| 270 | location => 'Manchester, NH', | ||
| 271 | |||
| 272 | # Set 'forecast => 1' to get the forecast, | ||
| 273 | # omit or set to false for current weather: | ||
| 274 | forecast => 1, | ||
| 275 | |||
| 276 | # If 'forecast' is true, you can ask for an hourly (rather than daily) | ||
| 277 | # forecast report: | ||
| 278 | hourly => 1, | ||
| 279 | |||
| 280 | # If 'forecast' is true, you can specify the number of days to fetch | ||
| 281 | # (up to 16 for daily reports, 5 for hourly reports): | ||
| 282 | days => 3, | ||
| 283 | |||
| 284 | # Optional tag for identifying the response to this request: | ||
| 285 | tag => 'foo', | ||
| 286 | ); | ||
| 287 | |||
| 288 | Request a weather report (in the form of a L<Weather::OpenWeatherMap::Result> | ||
| 289 | object) for the given C<< location => >>. | ||
| 290 | |||
| 291 | The location can be a 'City, State' or 'City, Country' string, an | ||
| 292 | L<OpenWeatherMap|http://www.openweathermap.org/> city code, or a 'lat X, long | ||
| 293 | Y' string. | ||
| 294 | |||
| 295 | Requests the current weather by default (see | ||
| 296 | L<Weather::OpenWeatherMap::Request::Current> and | ||
| 297 | L<Weather::OpenWeatherMap::Result::Current>). | ||
| 298 | |||
| 299 | If passed C<< forecast => 1 >>, requests a weather forecast (see | ||
| 300 | L<Weather::OpenWeatherMap::Request::Forecast> and | ||
| 301 | L<Weather::OpenWeatherMap::Result::Forecast>), in which case C<< days => | ||
| 302 | $count >> and/or C<< hourly => $bool >> can be specified. | ||
| 303 | |||
| 304 | If passed C<< find => 1 >>, requests search results for a given location name | ||
| 305 | or latitude & longitude; see L<Weather::OpenWeatherMap::Request::Find> and | ||
| 306 | L<Weather::OpenWeatherMap::Result::Find>. | ||
| 307 | |||
| 308 | Any extra arguments are passed to the constructor for the appropriate Request | ||
| 309 | subclass; see L<Weather::OpenWeatherMap::Request>. | ||
| 310 | |||
| 311 | =head1 SEE ALSO | ||
| 312 | |||
| 313 | L<Weather::OpenWeatherMap::Result> | ||
| 314 | |||
| 315 | L<POEx::Weather::OpenWeatherMap> | ||
| 316 | |||
| 317 | =head1 AUTHOR | ||
| 318 | |||
| 319 | Jon Portnoy <avenj@cobaltirc.org> | ||
| 320 | |||
| 321 | =cut | ||