Re: [Mojolicious] render utf8

2019-01-28 Thread Dan Book
The 'data' parameter expects bytes. Bytes cannot include wide characters,
they must either be binary data or already encoded text. If you want to
pass unencoded characters, use the 'text' parameter. See
https://metacpan.org/pod/Mojolicious::Guides::Rendering#Rendering-text

-Dan

On Mon, Jan 28, 2019 at 4:45 AM Boyd Duffee  wrote:

> Tracked down a bug in my code and found the reason for it wasn't
> explicitly covered in the docs.  I don't know if this is obvious or
> expected behaviour, so I'm posting for comment and for the next unfortunate
> soul STFW for this answer.
>
> I created a Data::ICal object as $calendar and added a bunch of events
> which I sent to the browser using
>
> $self->render( data => $calendar->as_string(),
> format => 'ics', status => 200 );
>
> It worked fine until I started including a unicode character in the events
> (  \x{1f4f9} ) and then I started getting this error on STDERR
>
> Mojo::Reactor::Poll: I/O watcher failed: Wide character in subroutine
> entry at /usr/local/share/perl5/Mojo/IOLoop/Stream.pm line 86.
>
> and it refused to send the response.  The failure was hidden by some
> caching code and my lack of understanding of the render lifecycle.  Of
> course, the solution was to properly encode the datastream
>
> use Encode qw/encode_utf8/;
> ...
> $self->render( data => encode_utf8( $calendar->as_string() ),
> format => 'ics', status => 200 );
>
> I guess I was expecting render to simply push out whatever garbage that I
> was passing off as data or maybe to emit a a more informative warning on
> development.log to realize the true impact it had on my app rather than
> puzzling over a HTTP 502 error.
>
> thoughts?
> --
> Boyd Duffee
>   They're good dogs, Brent.  Oh, h*ck. - We Rate Dogs
>
> --
> You received this message because you are subscribed to the Google Groups
> "Mojolicious" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to mojolicious+unsubscr...@googlegroups.com.
> To post to this group, send email to mojolicious@googlegroups.com.
> Visit this group at https://groups.google.com/group/mojolicious.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to mojolicious+unsubscr...@googlegroups.com.
To post to this group, send email to mojolicious@googlegroups.com.
Visit this group at https://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.


Re: [Mojolicious] Mojolicious::Lite routes organization

2019-01-28 Thread Stefan Adams
Hi!  See this recent thread
 in
this Mailing List.

On Mon, Jan 28, 2019 at 3:08 AM mikem  wrote:

> Hi Guys,
>
> I'm using Mojolicious::Lite to build a simple API for a project. This is
> my code:
>
>
> -
> app.pl
>
> -
> use Mojolicious::Lite;
>
> push @{ app->routes->namespaces }, 'MyAPI';
>
> (get '/products/list')->to('Products#list');
> (get '/products/add')->to('Products#add');
>
> -
>
>
> -
> MyAPI::Products.pm
>
> -
> package MyAPI::Products;
>
> use Mojo::Base 'Mojolicious::Controller';
>
> sub list {
>   my $self = shift;
>   $self->render(json => { hello => 'list' });
> }
>
> sub add {
>   my $self = shift;
>   $self->render(json => { hello => 'add' });
> }
>
> 1;
>
> -
>
> This works great and let me split my routers into several different files
> (keeping the simplicity of Mojolicious::Lite. My question is:
>
> Instead of having to register all my routers in app.pl and map them into
> the .pm files, can't I use a wild card for that? Something like:
>
>
> -
> (any '/products/*url')->to(cb => sub {
>   my $c= shift;
>   my $url = $c->param('url');
>   print STDERR "### $url\n";
>
>   ??
> });
>
> -
>
> With this, I have on the $url vbl what I need for the routing, I just
> can't figure out how to translate that into the correct method on the
> controller (MyAPI::Products::list for example).
>
> Thanks for the help.
>
>
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Mojolicious" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to mojolicious+unsubscr...@googlegroups.com.
> To post to this group, send email to mojolicious@googlegroups.com.
> Visit this group at https://groups.google.com/group/mojolicious.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to mojolicious+unsubscr...@googlegroups.com.
To post to this group, send email to mojolicious@googlegroups.com.
Visit this group at https://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.


[Mojolicious] render utf8

2019-01-28 Thread Boyd Duffee
Tracked down a bug in my code and found the reason for it wasn't explicitly
covered in the docs.  I don't know if this is obvious or expected
behaviour, so I'm posting for comment and for the next unfortunate soul
STFW for this answer.

I created a Data::ICal object as $calendar and added a bunch of events
which I sent to the browser using

$self->render( data => $calendar->as_string(),
format => 'ics', status => 200 );

It worked fine until I started including a unicode character in the events
(  \x{1f4f9} ) and then I started getting this error on STDERR

Mojo::Reactor::Poll: I/O watcher failed: Wide character in subroutine entry
at /usr/local/share/perl5/Mojo/IOLoop/Stream.pm line 86.

and it refused to send the response.  The failure was hidden by some
caching code and my lack of understanding of the render lifecycle.  Of
course, the solution was to properly encode the datastream

use Encode qw/encode_utf8/;
...
$self->render( data => encode_utf8( $calendar->as_string() ),
format => 'ics', status => 200 );

I guess I was expecting render to simply push out whatever garbage that I
was passing off as data or maybe to emit a a more informative warning on
development.log to realize the true impact it had on my app rather than
puzzling over a HTTP 502 error.

thoughts?
-- 
Boyd Duffee
  They're good dogs, Brent.  Oh, h*ck. - We Rate Dogs

-- 
You received this message because you are subscribed to the Google Groups 
"Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to mojolicious+unsubscr...@googlegroups.com.
To post to this group, send email to mojolicious@googlegroups.com.
Visit this group at https://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.


[Mojolicious] Mojolicious::Lite routes organization

2019-01-28 Thread mikem
Hi Guys,

I'm using Mojolicious::Lite to build a simple API for a project. This is my 
code:

-
app.pl
-
use Mojolicious::Lite;

push @{ app->routes->namespaces }, 'MyAPI';

(get '/products/list')->to('Products#list');
(get '/products/add')->to('Products#add');
-

-
MyAPI::Products.pm
-
package MyAPI::Products;

use Mojo::Base 'Mojolicious::Controller';

sub list {
  my $self = shift;
  $self->render(json => { hello => 'list' });
}

sub add {
  my $self = shift;
  $self->render(json => { hello => 'add' });
}

1;
-

This works great and let me split my routers into several different files 
(keeping the simplicity of Mojolicious::Lite. My question is:

Instead of having to register all my routers in app.pl and map them into 
the .pm files, can't I use a wild card for that? Something like:

-
(any '/products/*url')->to(cb => sub {
  my $c= shift;
  my $url = $c->param('url');  
  print STDERR "### $url\n";

  ??
});
-

With this, I have on the $url vbl what I need for the routing, I just can't 
figure out how to translate that into the correct method on the controller 
(MyAPI::Products::list for example).

Thanks for the help.




-- 
You received this message because you are subscribed to the Google Groups 
"Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to mojolicious+unsubscr...@googlegroups.com.
To post to this group, send email to mojolicious@googlegroups.com.
Visit this group at https://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.