Commits

Toby Inkster committed 98623c9

Minor cleanups; document async stuff.

  • Participants
  • Parent commits 6aa7d88

Comments (0)

Files changed (4)

File examples/stuff.pl

 use 5.010;
 use lib "lib";
-use Web::Magic -sub => 'W';
+use Web::Magic::Async -sub => 'W';
 
 say Web::Magic->can('random_jackbauer_fact');
 
 my $u = W q{ http://json-schema.org/card };
+say ref $u;
 say $u->uri->authority;
 #say $u->{description}; ## annoying - the JSON response is syntactically broken
 

File lib/Web/Magic.pm

 	
 	if ($args{-quotelike})
 	{
-		my $code   = sub ($) { __PACKAGE__->new(@_); };
+		my $code   = sub ($) { $class->new(@_); };
 		
 		$args{-quotelike} = [ $args{-quotelike} ]
 			unless ref $args{-quotelike};
 	
 	if ($args{-sub})
 	{
-		my $code   = sub ($;$%) { __PACKAGE__->new(@_); };
+		my $code   = sub ($;$%) { $class->new(@_); };
 		
 		$args{-sub} = [ $args{-sub} ]
 			unless ref $args{-sub};
 =item C<< do_request >>
 
 Actually performs the HTTP request. You rarely need to call this
-method implicitly, as calling any Post-Request method will automatically
+method explicitly, as calling any Post-Request method will automatically
 call C<do_request>.
 
 C<do_request> will be called automatically (via C<DESTROY>) on any

File lib/Web/Magic/Async.pm

 
 use base 'Web::Magic';
 
+sub import { goto &Web::Magic::import }
+
 sub do_request
 {
 	my ($self, %extra_headers) = @_;
 	croak $self->_async->{failed_assertion}
 		if $self->_async->{failed_assertion};
 
-	$self->_async->{headers};
+	$self->_async->{headers} // $self->_stash->{response}->headers;
 }
 
 sub is_requested
 
 1;
 
+__END__
+
+=head1 NAME
+
+Web::Magic::Async - asynchronous HTTP dwimmery
+
+=head1 SYNOPSIS
+
+ use Web::Magic::Async -sub => 'W'; 
+ say W('http://json-schema.org/card')->{description};
+
+=head1 DESCRIPTION
+
+An asynchronous drop-in replacement for L<Web::Magic>. Differences are
+noted below.
+
+=over
+
+=item C<< do_request >>
+
+Starts the HTTP request in the background, and returns immediately.
+While in Web::Magic, you'd rarely call this method explicitly, in
+Web::Magic::Async, call it as soon as you can (once you've finished
+specifying request headers, etc) so that your code can get on with
+other stuff while the HTTP stuff happens in the background.
+
+=item C<< response >>
+
+Blocks until the HTTP request has completed.
+
+=item C<< headers >>
+
+Blocks until at least the HTTP headers have been received.
+
+=item C<< is_requested >>
+
+If a request is in progress, blocks until it has finished before
+returning true.
+
+=item C<< is_in_progress >>
+
+Returns true if and only if the HTTP request is currently in progress.
+This method does not exist in Web::Magic itself.
+
+=back
+
+=head1 BUGS
+
+Uncountable, almost certainly.
+
+Have a go at counting them here:
+L<http://rt.cpan.org/Dist/Display.html?Queue=Web-Magic>.
+
+=head1 SEE ALSO
+
+L<Web::Magic>, L<AnyEvent>, L<AnyEvent::HTTP>.
+
+=head1 AUTHOR
+
+Toby Inkster E<lt>tobyink@cpan.orgE<gt>.
+
+=head1 COPYRIGHT AND LICENCE
+
+This software is copyright (c) 2011-2012 by Toby Inkster.
+
+This is free software; you can redistribute it and/or modify it under
+the same terms as the Perl 5 programming language system itself.
+
+=head1 DISCLAIMER OF WARRANTIES
+
+THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
+WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
+MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+

File meta/makefile.ttl

 		"common::sense",
 		"namespace::sweep",
 		"Acme::24 0.03",
+		"AnyEvent::HTTP 2.13",
 		"Carp 0",
 		"HTML::HTML5::Parser 0.100",
 		"HTML::HTML5::Writer 0.100",