Permalink
Browse files

better Mojo::UserAgent example for tutorial

  • Loading branch information...
kraih committed Apr 11, 2013
1 parent 9263835 commit 7b806e9f98e1bdbbe466b7599e56664e53d2c4bd
Showing with 8 additions and 6 deletions.
  1. +2 −1 Changes
  2. +2 −3 lib/Mojo/Transaction/WebSocket.pm
  3. +4 −2 lib/Mojolicious/Lite.pm
View
@@ -3,7 +3,8 @@
- Added finished_ok method to Test::Mojo.
- Removed deprecated slurp_rel_file method from Mojo::Home.
- Removed deprecated html_escape function from Mojo::Util.
- - Improved Mojo::Transaction::WebSocket with support for status codes.
+ - Improved Mojo::Transaction::WebSocket with support for status codes and
+ reasons.
- Improved documentation. (jberger, sri)
- Improved tests.
- Fixed a few small timing bugs in Mojo::Server::Daemon.
@@ -11,7 +11,7 @@ use constant DEBUG => $ENV{MOJO_WEBSOCKET_DEBUG} || 0;
use constant MODERN =>
(($Config{use64bitint} // '') eq 'define' || $Config{longsize} >= 8);
-# Unique value from the spec
+# Unique value from RFC 6455
use constant GUID => '258EAFA5-E914-47DA-95CA-C5AB0DC85B11';
# Opcodes
@@ -227,7 +227,6 @@ sub server_close {
sub server_handshake {
my $self = shift;
- # WebSocket handshake
my $res_headers = $self->res->code(101)->headers;
$res_headers->upgrade('websocket')->connection('Upgrade');
my $req_headers = $self->req->headers;
@@ -289,7 +288,7 @@ sub _message {
# No FIN bit (Continuation)
return unless $frame->[0];
- # Message
+ # Whole message
my $msg = delete $self->{message};
if (delete $self->{op} == TEXT) {
$self->emit(text => $msg);
View
@@ -845,9 +845,11 @@ L<Mojo::JSON> and L<Mojo::DOM> this can be a very powerful tool.
use Mojolicious::Lite;
- get '/test' => sub {
+ get '/headers' => sub {
my $self = shift;
- $self->render(data => $self->ua->get('http://mojolicio.us')->res->body);
+ my $url = $self->param('url') || 'http://mojolicio.us';
+ my $dom = $self->ua->get($url)->res->dom;
+ $self->render(json => [$dom->find('h1, h2, h3')->pluck('text')->each]);
};
app->start;

0 comments on commit 7b806e9

Please sign in to comment.