Created
May 7, 2010 20:56
-
-
Save j1n3l0/393989 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use MooseX::Declare; | |
role Validation { | |
requires qw/ GET HEAD responseCode responseContent /; | |
after GET { confess 'URL does not exist' if $self->responseCode eq 404 }; | |
}; | |
class App with(Validation) { | |
use Moose::Util::TypeConstraints; | |
use REST::Client; | |
my @REST_METHODS = qw/ GET HEAD responseCode responseContent /; | |
has client => ( | |
isa => duck_type( \@REST_METHODS ), | |
handles => \@REST_METHODS, | |
lazy_build => 1, | |
); | |
method _build_client { REST::Client->new } | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Moose is just awesome. Here we are delegating to the GET method from REST::Client (actually any class that can do all of the @REST_METHODS) and validating the response code in a role (so the validation can be as complex as you need it to be). In this case the validation is just inspecting the REST::Client delegatee after the GET is complete.