From 9970a68dbb108d36b16595d26b827a7d63223155 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Wed, 12 Jan 2011 11:44:09 -0600 Subject: add Plack::Client::Backend as a base class --- lib/Plack/Client.pm | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) (limited to 'lib/Plack/Client.pm') diff --git a/lib/Plack/Client.pm b/lib/Plack/Client.pm index 813351c..1ed096a 100644 --- a/lib/Plack/Client.pm +++ b/lib/Plack/Client.pm @@ -69,10 +69,13 @@ appropriate apps based on the request. ) Constructor. Takes a hash of arguments, where keys are URL schemas, and values -are backends which handle those schemas. Hashref and arrayref values are also -valid, and will be dereferenced and passed to the constructor of the default -backend for that scheme (the class C, where -C<$scheme> has dashes replaced by underscores). +are backends which handle those schemas. Backends are really just coderefs +which receive a L and return a PSGI application coderef, but +see L for a more structured way to define these. +Hashref and arrayref values are also valid, and will be dereferenced and passed +to the constructor of the default backend for that scheme (the class +C, where C<$scheme> has dashes replaced by +underscores). =cut @@ -83,27 +86,26 @@ sub new { my %backends; for my $scheme (keys %params) { my $backend = $params{$scheme}; - if (blessed($backend)) { - croak "Backends must support the app_from_request method" - unless $backend->can('app_from_request'); + if ((blessed($backend) || '') eq 'Plack::Client::Backend') { + $backends{$scheme} = $backend->as_code; + } + elsif (reftype($backend) eq 'CODE') { $backends{$scheme} = $backend; } elsif (ref($backend)) { (my $normal_scheme = $scheme) =~ s/-/_/g; my $backend_class = "Plack::Client::Backend::$normal_scheme"; Class::Load::load_class($backend_class); - croak "Backends must support the app_from_request method" - unless $backend_class->can('app_from_request'); - croak "Backend classes must have a constructor" - unless $backend_class->can('new'); + croak "Backend classes must inherit from Plack::Client::Backend" + unless $backend_class->isa('Plack::Client::Backend'); $backends{$scheme} = $backend_class->new( reftype($backend) eq 'HASH' ? %$backend : reftype($backend) eq 'ARRAY' ? @$backend : $$backend - ); + )->as_code; } else { - croak 'XXX'; + croak "Backends must be a coderef or a Plack::Client::Backend instance"; } } @@ -265,7 +267,7 @@ sub _app_from_request { my $uri = $req->env->{'plack.client.original_uri'} || $req->uri; my $backend = $self->backend($uri); - my $app = $backend->app_from_request($req); + my $app = $backend->($req); croak "Couldn't find app" unless $app; -- cgit v1.2.3-54-g00ecf