From 9b724c2c7edc3c2805516fea7f584cdf0031d8ca Mon Sep 17 00:00:00 2001 From: dakkar Date: Thu, 16 May 2013 22:06:09 +0100 Subject: modules for all response blocks --- lib/WebService/ForecastIo.pm | 7 +++-- lib/WebService/ForecastIo/Alert.pm | 36 ++++++++++++++++++++++ lib/WebService/ForecastIo/DataBlock.pm | 22 ++++++++++++++ lib/WebService/ForecastIo/DataPoint.pm | 44 +++++++++++++++++++++++++++ lib/WebService/ForecastIo/Response.pm | 55 ++++++++++++++++++++++++++++++++++ 5 files changed, 162 insertions(+), 2 deletions(-) create mode 100644 lib/WebService/ForecastIo/Alert.pm create mode 100644 lib/WebService/ForecastIo/DataBlock.pm create mode 100644 lib/WebService/ForecastIo/DataPoint.pm create mode 100644 lib/WebService/ForecastIo/Response.pm (limited to 'lib') diff --git a/lib/WebService/ForecastIo.pm b/lib/WebService/ForecastIo.pm index 45e7802..a2ef8ec 100644 --- a/lib/WebService/ForecastIo.pm +++ b/lib/WebService/ForecastIo.pm @@ -5,6 +5,7 @@ use MooseX::Types::DateTime; use MooseX::Params::Validate; use Moose::Util::TypeConstraints; use DateTime::Format::ISO8601; +use WebService::ForecastIo::Response; has base_uri => ( is => 'ro', @@ -55,7 +56,7 @@ sub _make_request_uri { } $req_uri->path_segments( - grep { length($_)>0 } + grep { length($_) } $req_uri->path_segments, $self->api_key, "$lat,$lon", @@ -96,7 +97,9 @@ sub request { my $response = $self->user_agent->get($uri); if ($response->is_success) { - return $response->decoded_content; + return WebService::ForecastIo::Response->new( + $response->decoded_content, + ); } else { die $response->status_line diff --git a/lib/WebService/ForecastIo/Alert.pm b/lib/WebService/ForecastIo/Alert.pm new file mode 100644 index 0000000..37ff4ac --- /dev/null +++ b/lib/WebService/ForecastIo/Alert.pm @@ -0,0 +1,36 @@ +package WebService::ForecastIo::Alert; +use Moose; +use Moose::Util::TypeConstraints; +use MooseX::Types::URI 'Uri'; + +class_type 'WebService::ForecastIo::Alert'; + +subtype 'WebService::ForecastIo::AlertArray', + as 'ArrayRef[WebService::ForecastIo::Alert]'; + +coerce 'WebService::ForecastIo::Alert', from 'HashRef', + via { WebService::ForecastIo::Alert->new($_) }; + +coerce 'WebService::ForecastIo::AlertArray', from 'ArrayRef[HashRef]', + via { + my $array = $_; + [ map { WebService::ForecastIo::Alert->new($_) } + @$array ] + }; + +has title => ( + is => 'ro', + isa => 'Str', +); + +has expires => ( + is => 'ro', + isa => 'Int', # time! +); + +has uri => ( + is => 'ro', + isa => Uri, +); + +1; diff --git a/lib/WebService/ForecastIo/DataBlock.pm b/lib/WebService/ForecastIo/DataBlock.pm new file mode 100644 index 0000000..9d87b3a --- /dev/null +++ b/lib/WebService/ForecastIo/DataBlock.pm @@ -0,0 +1,22 @@ +package WebService::ForecastIo::DataBlock; +use Moose; +use Moose::Util::TypeConstraints; +use WebService::ForecastIo::DataPoint; + +class_type 'WebService::ForecastIo::DataBlock'; + +coerce 'WebService::ForecastIo::DataBlock', from 'HashRef', + via { WebService::ForecastIo::DataBlock->new($_) }; + +has [qw(summary icon)] => ( + is => 'ro', + isa => 'Str', +); + +has data => ( + is => 'ro', + isa => 'WebService::ForecastIo::DataPointArray', + coerce => 1, +); + +1; diff --git a/lib/WebService/ForecastIo/DataPoint.pm b/lib/WebService/ForecastIo/DataPoint.pm new file mode 100644 index 0000000..c05e981 --- /dev/null +++ b/lib/WebService/ForecastIo/DataPoint.pm @@ -0,0 +1,44 @@ +package WebService::ForecastIo::DataPoint; +use Moose; +use Moose::Util::TypeConstraints; + +class_type 'WebService::ForecastIo::DataPoint'; + +subtype 'WebService::ForecastIo::DataPointArray', + as 'ArrayRef[WebService::ForecastIo::DataPoint]'; + +coerce 'WebService::ForecastIo::DataPoint', from 'HashRef', + via { WebService::ForecastIo::DataPoint->new($_) }; + +coerce 'WebService::ForecastIo::DataPointArray', from 'ArrayRef[HashRef]', + via { + my $array = $_; + [ map { WebService::ForecastIo::DataPoint->new($_) } + @$array ] + }; + +has [qw( time + sunriseTime sunsetTime + precipIntensityMaxTime + temperatureMinTime temperatureMaxTime )] => ( + is => 'ro', + isa => 'Int', # time! +); + +has [qw(summary icon precipType)] => ( + is => 'ro', + isa => 'Str', +); + +has [qw( precipIntensity precipIntensityMax + precipProbability precipAccumulation + temperature temperatureMin temperatureMax + dewPoint humidity + windSpeed windBearing + cloudCover + pressure visibility ozone )] => ( + is => 'ro', + isa => 'Num', +); + +1; diff --git a/lib/WebService/ForecastIo/Response.pm b/lib/WebService/ForecastIo/Response.pm new file mode 100644 index 0000000..e217c70 --- /dev/null +++ b/lib/WebService/ForecastIo/Response.pm @@ -0,0 +1,55 @@ +package WebService::ForecastIo::Response; +use Moose; +use WebService::ForecastIo::DataPoint; +use WebService::ForecastIo::DataBlock; +use WebService::ForecastIo::Alert; +use JSON; + +has 'currently' => ( + is => 'ro', + isa => 'WebService::ForecastIo::DataPoint', + coerce => 1, +); + +has [qw(daily hourly minutely)] => ( + is => 'ro', + isa => 'WebService::ForecastIo::DataBlock', + coerce => 1, +); + +has [qw(latitude longitude)] => ( + is => 'ro', + isa => 'Num', +); + +has timezone => ( + is => 'ro', + isa => 'Str', +); + +has offset => ( + is => 'ro', + isa => 'Num', +); + +has alerts => ( + is => 'ro', + isa => 'WebService::ForecastIo::AlertArray', + coerce => 1, +); + +has flags => ( + is => 'ro', + isa => 'HashRef', +); + +around BUILDARGS => sub { + my ($orig,$class,@args) = @_; + + if (@args==1 and !ref($args[0])) { + @args = decode_json($args[0]); + } + return $class->$orig(@args); +}; + +1; -- cgit v1.2.3