Skip to content

Instantly share code, notes, and snippets.

@j1n3l0
Created May 7, 2010 20:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save j1n3l0/393989 to your computer and use it in GitHub Desktop.
Save j1n3l0/393989 to your computer and use it in GitHub Desktop.
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 }
};
@j1n3l0
Copy link
Author

j1n3l0 commented May 7, 2010

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment