[Mojolicious] Re: Need help with stashing data and retrieving it in the template

2019-02-07 Thread Ron Bergin
Thank you Dan.  I will work on adjusting the error handling as suggested.

This is the first time I've done any async and for me it's a bit confusing 
and has a stepper learning curve than the Dancer apps that I've done.

-- 
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] Re: Need help with stashing data and retrieving it in the template

2019-02-07 Thread Dan Book
For error handling in this case, keep in mind that there are multiple kinds
of errors. Transport errors (connection failure, etc) will result in the
promise being rejected, which for `all` will cause the whole promise to be
rejected and the `catch` callback to be called, rendering an exception page
(in my example). If you don't want that to happen, add a `catch` handler to
each `get_p` promise that doesn't die to turn it into a resolved promise
(this example one will just pass the rejection reason along):

my @promises = map { $ua->get_p($_)->catch(sub { @_ }) } @urls;

Then when iterating over the results, you will get the string error instead
of the $tx if the get_p originally failed.

HTTP errors on the other hand you have to check for yourself, they don't
affect the resolution of the promise.

-Dan

On Thu, Feb 7, 2019 at 2:34 PM Ron Bergin  wrote:

> Stefan, Thanks for the link.  I've previously read that post but will need
> to read it again.
>
> Dan, I figured out what was causing that last error.  I forgot to load
> use Mojo::Util 'trim';
>
> The script is now working correctly.  Now I need to work on adding error
> checking/handling for cases where the url is unreachable.
>
> --
> 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] Re: Need help with stashing data and retrieving it in the template

2019-02-07 Thread Ron Bergin
Stefan, Thanks for the link.  I've previously read that post but will need 
to read it again.

Dan, I figured out what was causing that last error.  I forgot to load 
use Mojo::Util 'trim';

The script is now working correctly.  Now I need to work on adding error 
checking/handling for cases where the url is unreachable.

-- 
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] Re: Need help with stashing data and retrieving it in the template

2019-02-07 Thread Dan Book
Regarding delays, promises actually came later and are now considered the
preferred approach as they are much more composable, error handling is
easier, and they are the same concept used in many other languages.

-Dan

On Thu, Feb 7, 2019 at 2:24 PM Ron Bergin  wrote:

> Dan, thank you for your input.  I haven't looked at promises much but I
> was under the impression (based on some of Joel Berger's examples as well
> as one or two other key mojolicious developers) that delays were supposed
> to be cleaner and more desirable.
>
> I knew about the issue with the for loop initialization.  Using the
> correct loop gave me a 500 error so I temporarily "broke" that while
> troubleshooting the passing of the data via the dumper helper.  I corrected
> that loop and swapped in your code but it is giving a "Premature connection
> close" error.
> $ morbo ./titles_promise.pl
> Server available at http://127.0.0.1:3000
>
> [Thu Feb  7 11:02:35 2019] [debug] GET "/titles" (604f6be1)
> [Thu Feb  7 11:02:35 2019] [debug] Routing to a callback
> [Thu Feb  7 11:02:35 2019] [error] Premature connection close
> [Thu Feb  7 11:02:35 2019] [debug] Template
> "exception.development.html.ep" not found
> [Thu Feb  7 11:02:35 2019] [debug] Template "exception.html.ep" not found
> [Thu Feb  7 11:02:35 2019] [debug] Rendering template "mojo/debug.html.ep"
> [Thu Feb  7 11:02:35 2019] [debug] Rendering template
> "mojo/menubar.html.ep"
> [Thu Feb  7 11:02:35 2019] [debug] Your secret passphrase needs to be
> changed
> [Thu Feb  7 11:02:35 2019] [debug] 500 Internal Server Error (0.028391s,
> 35.222/s)
>
>
> Thinking that it might be due to a scoping issue with $ua, I moved that
> out to have file scope, but then it produced this error:
> Use of inherited AUTOLOAD for non-method Mojo::Server::Sandbox::
> b205866ab9c46b3f8105929c8a269a25::trim() is deprecated. This will be
> fatal in Perl 5.28 at /home/rkb/test/titles_promise.pl line 23.
> [Thu Feb  7 11:19:23 2019] [error] Undefined subroutine ::Server::
> Sandbox::b205866ab9c46b3f8105929c8a269a25::trim called at /usr/local/share
> /perl/5.26.1/Mojo/Promise.pm line 123.
>
>
>
> On Thursday, February 7, 2019 at 9:48:20 AM UTC-8, Ron Bergin wrote:
>>
>> I'm brand new to Mojolicious and working on a lite script that goes to 75
>> urls and retrieves text in an h3 tag. I've worked up a working test script
>> that outputs to console and now I'm working on adjusting it to be a web app
>> but I can't seem to get the correct syntax to store/retrieve the data.
>> What am I doing wrong?
>>
>> #!/usr/bin/perl
>>
>> use Mojolicious::Lite;
>>
>> my @urls  = (
>> 'https://mojolicious.org',
>> 'https://metacpan.org',
>> 'https://perlmonks.org',
>> 'http://www.google.com',
>> );
>>
>> get '/titles' => sub {
>> my $ua= Mojo::UserAgent->new;
>> my $async = Mojo::IOLoop::Delay->new;
>>
>> $async->steps(
>> sub {
>> my $self = shift;
>> $ua->get($_, $self->begin) for @urls;
>> },
>> sub {
>> my ($self, @tx) = @_;
>> my $titles  = [];
>>
>> foreach (@tx) {
>> my $title  = '';
>> my $status = $_->res->is_success ? 'Connected' : $_->
>> error->{message};
>>
>> if ( $_->res->is_success ) {
>> $title = $_->res->dom->at('title')->text;
>> $title =~ s/^\s+|\s+$//g; # couldn't get Mojo::Util
>> trim function to work
>> }
>>
>> push @$titles, {host => $_->req->url->host, status =>
>> $status, title => $title};
>> }
>> $self->stash(titles => $titles);
>> }
>> );
>> } => 'titles';
>>
>> app->start;
>>
>> __DATA__
>>
>> @@ titles.html.ep
>> % layout 'titles';
>>
>> %= dumper stash('titles')
>>
>> 
>> 
>>   Host
>>   Status
>>   Title
>> 
>>
>> 
>> % for my $server ( stash('titles') ) {
>>   
>> <%= $server->{host} %>
>> <%= $server->{status} %>
>> <%= $server->{title} =%>
>>   
>> % }
>> 
>>
>> @@ layouts/titles.html.ep
>> 
>>
>> 
>>
>> 
>>   Titles
>> 
>>
>> 
>> Titles
>> <%= content =%>
>> 
>>
>> 
>>
>>
>> Here's the output:
>>
>>> $ ./titles_web.pl get /titles
>>> [Thu Feb  7 09:45:54 2019] [debug] GET "/titles" (fcf21ce8)
>>> [Thu Feb  7 09:45:54 2019] [debug] Routing to a callback
>>> [Thu Feb  7 09:45:54 2019] [debug] Rendering template "titles.html.ep"
>>> from DATA section
>>> [Thu Feb  7 09:45:54 2019] [debug] Rendering template
>>> "layouts/titles.html.ep" from DATA section
>>> [Thu Feb  7 09:45:54 2019] [debug] 200 OK (0.00267s, 374.532/s)
>>> 
>>>
>>> 
>>>
>>> 
>>>   Titles
>>> 
>>>
>>> 
>>> Titles
>>>
>>> undef
>>>
>>>
>>> 
>>> 
>>>   Host
>>>   Status
>>>   Title
>>> 
>>>
>>> 
>>>   
>>> 
>>> 
>>> 
>>> 
>>> 
>>> 
>>>
>>> 
>>>
>>
>> --
> 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

Re: [Mojolicious] Re: Need help with stashing data and retrieving it in the template

2019-02-07 Thread Dan Book
I used trim(), this needs to be imported from Mojo::Util.

-Dan

On Thu, Feb 7, 2019 at 2:24 PM Ron Bergin  wrote:

> Dan, thank you for your input.  I haven't looked at promises much but I
> was under the impression (based on some of Joel Berger's examples as well
> as one or two other key mojolicious developers) that delays were supposed
> to be cleaner and more desirable.
>
> I knew about the issue with the for loop initialization.  Using the
> correct loop gave me a 500 error so I temporarily "broke" that while
> troubleshooting the passing of the data via the dumper helper.  I corrected
> that loop and swapped in your code but it is giving a "Premature connection
> close" error.
> $ morbo ./titles_promise.pl
> Server available at http://127.0.0.1:3000
>
> [Thu Feb  7 11:02:35 2019] [debug] GET "/titles" (604f6be1)
> [Thu Feb  7 11:02:35 2019] [debug] Routing to a callback
> [Thu Feb  7 11:02:35 2019] [error] Premature connection close
> [Thu Feb  7 11:02:35 2019] [debug] Template
> "exception.development.html.ep" not found
> [Thu Feb  7 11:02:35 2019] [debug] Template "exception.html.ep" not found
> [Thu Feb  7 11:02:35 2019] [debug] Rendering template "mojo/debug.html.ep"
> [Thu Feb  7 11:02:35 2019] [debug] Rendering template
> "mojo/menubar.html.ep"
> [Thu Feb  7 11:02:35 2019] [debug] Your secret passphrase needs to be
> changed
> [Thu Feb  7 11:02:35 2019] [debug] 500 Internal Server Error (0.028391s,
> 35.222/s)
>
>
> Thinking that it might be due to a scoping issue with $ua, I moved that
> out to have file scope, but then it produced this error:
> Use of inherited AUTOLOAD for non-method Mojo::Server::Sandbox::
> b205866ab9c46b3f8105929c8a269a25::trim() is deprecated. This will be
> fatal in Perl 5.28 at /home/rkb/test/titles_promise.pl line 23.
> [Thu Feb  7 11:19:23 2019] [error] Undefined subroutine ::Server::
> Sandbox::b205866ab9c46b3f8105929c8a269a25::trim called at /usr/local/share
> /perl/5.26.1/Mojo/Promise.pm line 123.
>
>
>
> On Thursday, February 7, 2019 at 9:48:20 AM UTC-8, Ron Bergin wrote:
>>
>> I'm brand new to Mojolicious and working on a lite script that goes to 75
>> urls and retrieves text in an h3 tag. I've worked up a working test script
>> that outputs to console and now I'm working on adjusting it to be a web app
>> but I can't seem to get the correct syntax to store/retrieve the data.
>> What am I doing wrong?
>>
>> #!/usr/bin/perl
>>
>> use Mojolicious::Lite;
>>
>> my @urls  = (
>> 'https://mojolicious.org',
>> 'https://metacpan.org',
>> 'https://perlmonks.org',
>> 'http://www.google.com',
>> );
>>
>> get '/titles' => sub {
>> my $ua= Mojo::UserAgent->new;
>> my $async = Mojo::IOLoop::Delay->new;
>>
>> $async->steps(
>> sub {
>> my $self = shift;
>> $ua->get($_, $self->begin) for @urls;
>> },
>> sub {
>> my ($self, @tx) = @_;
>> my $titles  = [];
>>
>> foreach (@tx) {
>> my $title  = '';
>> my $status = $_->res->is_success ? 'Connected' : $_->
>> error->{message};
>>
>> if ( $_->res->is_success ) {
>> $title = $_->res->dom->at('title')->text;
>> $title =~ s/^\s+|\s+$//g; # couldn't get Mojo::Util
>> trim function to work
>> }
>>
>> push @$titles, {host => $_->req->url->host, status =>
>> $status, title => $title};
>> }
>> $self->stash(titles => $titles);
>> }
>> );
>> } => 'titles';
>>
>> app->start;
>>
>> __DATA__
>>
>> @@ titles.html.ep
>> % layout 'titles';
>>
>> %= dumper stash('titles')
>>
>> 
>> 
>>   Host
>>   Status
>>   Title
>> 
>>
>> 
>> % for my $server ( stash('titles') ) {
>>   
>> <%= $server->{host} %>
>> <%= $server->{status} %>
>> <%= $server->{title} =%>
>>   
>> % }
>> 
>>
>> @@ layouts/titles.html.ep
>> 
>>
>> 
>>
>> 
>>   Titles
>> 
>>
>> 
>> Titles
>> <%= content =%>
>> 
>>
>> 
>>
>>
>> Here's the output:
>>
>>> $ ./titles_web.pl get /titles
>>> [Thu Feb  7 09:45:54 2019] [debug] GET "/titles" (fcf21ce8)
>>> [Thu Feb  7 09:45:54 2019] [debug] Routing to a callback
>>> [Thu Feb  7 09:45:54 2019] [debug] Rendering template "titles.html.ep"
>>> from DATA section
>>> [Thu Feb  7 09:45:54 2019] [debug] Rendering template
>>> "layouts/titles.html.ep" from DATA section
>>> [Thu Feb  7 09:45:54 2019] [debug] 200 OK (0.00267s, 374.532/s)
>>> 
>>>
>>> 
>>>
>>> 
>>>   Titles
>>> 
>>>
>>> 
>>> Titles
>>>
>>> undef
>>>
>>>
>>> 
>>> 
>>>   Host
>>>   Status
>>>   Title
>>> 
>>>
>>> 
>>>   
>>> 
>>> 
>>> 
>>> 
>>> 
>>> 
>>>
>>> 
>>>
>>
>> --
> 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 

[Mojolicious] Re: Need help with stashing data and retrieving it in the template

2019-02-07 Thread Ron Bergin
Dan, thank you for your input.  I haven't looked at promises much but I was 
under the impression (based on some of Joel Berger's examples as well as 
one or two other key mojolicious developers) that delays were supposed to 
be cleaner and more desirable.

I knew about the issue with the for loop initialization.  Using the correct 
loop gave me a 500 error so I temporarily "broke" that while 
troubleshooting the passing of the data via the dumper helper.  I corrected 
that loop and swapped in your code but it is giving a "Premature connection 
close" error.
$ morbo ./titles_promise.pl
Server available at http://127.0.0.1:3000

[Thu Feb  7 11:02:35 2019] [debug] GET "/titles" (604f6be1)
[Thu Feb  7 11:02:35 2019] [debug] Routing to a callback
[Thu Feb  7 11:02:35 2019] [error] Premature connection close
[Thu Feb  7 11:02:35 2019] [debug] Template "exception.development.html.ep" 
not found
[Thu Feb  7 11:02:35 2019] [debug] Template "exception.html.ep" not found
[Thu Feb  7 11:02:35 2019] [debug] Rendering template "mojo/debug.html.ep"
[Thu Feb  7 11:02:35 2019] [debug] Rendering template "mojo/menubar.html.ep"
[Thu Feb  7 11:02:35 2019] [debug] Your secret passphrase needs to be 
changed
[Thu Feb  7 11:02:35 2019] [debug] 500 Internal Server Error (0.028391s, 
35.222/s)


Thinking that it might be due to a scoping issue with $ua, I moved that out 
to have file scope, but then it produced this error:
Use of inherited AUTOLOAD for non-method Mojo::Server::Sandbox::
b205866ab9c46b3f8105929c8a269a25::trim() is deprecated. This will be fatal 
in Perl 5.28 at /home/rkb/test/titles_promise.pl line 23.
[Thu Feb  7 11:19:23 2019] [error] Undefined subroutine ::Server::
Sandbox::b205866ab9c46b3f8105929c8a269a25::trim called at /usr/local/share/
perl/5.26.1/Mojo/Promise.pm line 123.



On Thursday, February 7, 2019 at 9:48:20 AM UTC-8, Ron Bergin wrote:
>
> I'm brand new to Mojolicious and working on a lite script that goes to 75 
> urls and retrieves text in an h3 tag. I've worked up a working test script 
> that outputs to console and now I'm working on adjusting it to be a web app 
> but I can't seem to get the correct syntax to store/retrieve the data.  
> What am I doing wrong?
>
> #!/usr/bin/perl
>
> use Mojolicious::Lite;
>
> my @urls  = (
> 'https://mojolicious.org',
> 'https://metacpan.org',
> 'https://perlmonks.org',
> 'http://www.google.com',
> );
>
> get '/titles' => sub {
> my $ua= Mojo::UserAgent->new;
> my $async = Mojo::IOLoop::Delay->new;
>
> $async->steps(
> sub { 
> my $self = shift;
> $ua->get($_, $self->begin) for @urls;
> },
> sub {
> my ($self, @tx) = @_;
> my $titles  = [];
>
> foreach (@tx) {
> my $title  = '';
> my $status = $_->res->is_success ? 'Connected' : $_->error
> ->{message};
>
> if ( $_->res->is_success ) {
> $title = $_->res->dom->at('title')->text;
> $title =~ s/^\s+|\s+$//g; # couldn't get Mojo::Util 
> trim function to work
> }
>
> push @$titles, {host => $_->req->url->host, status => 
> $status, title => $title};
> }
> $self->stash(titles => $titles);
> }
> );
> } => 'titles';
>
> app->start;
>
> __DATA__
>
> @@ titles.html.ep
> % layout 'titles';
>
> %= dumper stash('titles')
>
> 
> 
>   Host
>   Status
>   Title
> 
>
> 
> % for my $server ( stash('titles') ) {
>   
> <%= $server->{host} %>
> <%= $server->{status} %>
> <%= $server->{title} =%>
>   
> % }
> 
>
> @@ layouts/titles.html.ep
> 
>
> 
>
> 
>   Titles
> 
>
> 
> Titles
> <%= content =%>
> 
>
> 
>
>
> Here's the output:
>
>> $ ./titles_web.pl get /titles
>> [Thu Feb  7 09:45:54 2019] [debug] GET "/titles" (fcf21ce8)
>> [Thu Feb  7 09:45:54 2019] [debug] Routing to a callback
>> [Thu Feb  7 09:45:54 2019] [debug] Rendering template "titles.html.ep" 
>> from DATA section
>> [Thu Feb  7 09:45:54 2019] [debug] Rendering template 
>> "layouts/titles.html.ep" from DATA section
>> [Thu Feb  7 09:45:54 2019] [debug] 200 OK (0.00267s, 374.532/s)
>> 
>>
>> 
>>
>> 
>>   Titles
>> 
>>
>> 
>> Titles
>>
>> undef
>>
>>
>> 
>> 
>>   Host
>>   Status
>>   Title
>> 
>>
>> 
>>   
>> 
>> 
>> 
>> 
>> 
>> 
>>
>> 
>>
>
>

-- 
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] Re: Need help with stashing data and retrieving it in the template

2019-02-07 Thread Dan Book
One more thing: you need to keep a strong reference to the Mojo::UserAgent
object until it completes, so you should define $ua outside of the action
method which will end before the async code occurs.

-Dan

On Thu, Feb 7, 2019 at 1:23 PM Dan Book  wrote:

> Your commented 'for' loop is correct; you need to dereference
> stash('titles') to pass the list of hashrefs to the loop. The other problem
> is that you need to prevent auto-rendering until your async code has
> completed. You can do this by calling `$c->render_later` at the beginning
> of the action (`$c` is the first argument passed to the action, which you
> need to retrieve), and then `$c->render` after you've set the stash value.
> You can even include the stash update in the render call:
> `$c->render(titles => $titles);`
>
> I would additionally recommend to use promises rather than delays as it
> will lead to much more concise code with the promise methods available from
> Mojo::UserAgent. Also, you want to hold a reference to the transaction, and
> add exception handling, as described here:
> https://github.com/mojolicious/mojo/wiki/Upgrading#delay-helper-removed
>
> Here is how it might look as a start:
>
> get '/titles' => sub {
> my $c = shift;
> $c->render_later;
> my $tx = $c->tx;
> my $ua= Mojo::UserAgent->new;
> my @promises = map { $ua->get_p($_) } @urls;
> Mojo::Promise->all(@promises)->then(sub {
> my @results = @_;
> my $titles = [];
> foreach my $result (@results) {
> my $tx = $result->[0];
> my $title = trim($tx->res->dom->at('title')->text);
> push @$titles, {host => $tx->req->url->host, title => $title};
> }
> $c->render(titles => $titles);
> })->catch(sub { $c->reply->exception(pop); undef $tx });
> } => 'titles';
>
> -Dan
>
> On Thu, Feb 7, 2019 at 12:57 PM Ron Bergin  wrote:
>
>> Note: I cross posted this question on perlmonks
>> https://perlmonks.org/?node_id=1229558
>>
>> On Thursday, February 7, 2019 at 9:48:20 AM UTC-8, Ron Bergin wrote:
>>>
>>> I'm brand new to Mojolicious and working on a lite script that goes to
>>> 75 urls and retrieves text in an h3 tag. I've worked up a working test
>>> script that outputs to console and now I'm working on adjusting it to be a
>>> web app but I can't seem to get the correct syntax to store/retrieve the
>>> data.  What am I doing wrong?
>>>
>>> #!/usr/bin/perl
>>>
>>> use Mojolicious::Lite;
>>>
>>> my @urls  = (
>>> 'https://mojolicious.org',
>>> 'https://metacpan.org',
>>> 'https://perlmonks.org',
>>> 'http://www.google.com',
>>> );
>>>
>>> get '/titles' => sub {
>>> my $ua= Mojo::UserAgent->new;
>>> my $async = Mojo::IOLoop::Delay->new;
>>>
>>> $async->steps(
>>> sub {
>>> my $self = shift;
>>> $ua->get($_, $self->begin) for @urls;
>>> },
>>> sub {
>>> my ($self, @tx) = @_;
>>> my $titles  = [];
>>>
>>> foreach (@tx) {
>>> my $title  = '';
>>> my $status = $_->res->is_success ? 'Connected' : $_->
>>> error->{message};
>>>
>>> if ( $_->res->is_success ) {
>>> $title = $_->res->dom->at('title')->text;
>>> $title =~ s/^\s+|\s+$//g; # couldn't get Mojo::Util
>>> trim function to work
>>> }
>>>
>>> push @$titles, {host => $_->req->url->host, status =>
>>> $status, title => $title};
>>> }
>>> $self->stash(titles => $titles);
>>> }
>>> );
>>> } => 'titles';
>>>
>>> app->start;
>>>
>>> __DATA__
>>>
>>> @@ titles.html.ep
>>> % layout 'titles';
>>>
>>> %= dumper stash('titles')
>>>
>>> 
>>> 
>>>   Host
>>>   Status
>>>   Title
>>> 
>>>
>>> 
>>> % for my $server ( stash('titles') ) {
>>>   
>>> <%= $server->{host} %>
>>> <%= $server->{status} %>
>>> <%= $server->{title} =%>
>>>   
>>> % }
>>> 
>>>
>>> @@ layouts/titles.html.ep
>>> 
>>>
>>> 
>>>
>>> 
>>>   Titles
>>> 
>>>
>>> 
>>> Titles
>>> <%= content =%>
>>> 
>>>
>>> 
>>>
>>>
>>> Here's the output:
>>>
 $ ./titles_web.pl get /titles
 [Thu Feb  7 09:45:54 2019] [debug] GET "/titles" (fcf21ce8)
 [Thu Feb  7 09:45:54 2019] [debug] Routing to a callback
 [Thu Feb  7 09:45:54 2019] [debug] Rendering template "titles.html.ep"
 from DATA section
 [Thu Feb  7 09:45:54 2019] [debug] Rendering template
 "layouts/titles.html.ep" from DATA section
 [Thu Feb  7 09:45:54 2019] [debug] 200 OK (0.00267s, 374.532/s)
 

 

 
   Titles
 

 
 Titles

 undef


 
 
   Host
   Status
   Title
 

 
   
 
 
 
 
 
 

 

>>>
>>> --
>> 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

Re: [Mojolicious] Re: Need help with stashing data and retrieving it in the template

2019-02-07 Thread Dan Book
Your commented 'for' loop is correct; you need to dereference
stash('titles') to pass the list of hashrefs to the loop. The other problem
is that you need to prevent auto-rendering until your async code has
completed. You can do this by calling `$c->render_later` at the beginning
of the action (`$c` is the first argument passed to the action, which you
need to retrieve), and then `$c->render` after you've set the stash value.
You can even include the stash update in the render call:
`$c->render(titles => $titles);`

I would additionally recommend to use promises rather than delays as it
will lead to much more concise code with the promise methods available from
Mojo::UserAgent. Also, you want to hold a reference to the transaction, and
add exception handling, as described here:
https://github.com/mojolicious/mojo/wiki/Upgrading#delay-helper-removed

Here is how it might look as a start:

get '/titles' => sub {
my $c = shift;
$c->render_later;
my $tx = $c->tx;
my $ua= Mojo::UserAgent->new;
my @promises = map { $ua->get_p($_) } @urls;
Mojo::Promise->all(@promises)->then(sub {
my @results = @_;
my $titles = [];
foreach my $result (@results) {
my $tx = $result->[0];
my $title = trim($tx->res->dom->at('title')->text);
push @$titles, {host => $tx->req->url->host, title => $title};
}
$c->render(titles => $titles);
})->catch(sub { $c->reply->exception(pop); undef $tx });
} => 'titles';

-Dan

On Thu, Feb 7, 2019 at 12:57 PM Ron Bergin  wrote:

> Note: I cross posted this question on perlmonks
> https://perlmonks.org/?node_id=1229558
>
> On Thursday, February 7, 2019 at 9:48:20 AM UTC-8, Ron Bergin wrote:
>>
>> I'm brand new to Mojolicious and working on a lite script that goes to 75
>> urls and retrieves text in an h3 tag. I've worked up a working test script
>> that outputs to console and now I'm working on adjusting it to be a web app
>> but I can't seem to get the correct syntax to store/retrieve the data.
>> What am I doing wrong?
>>
>> #!/usr/bin/perl
>>
>> use Mojolicious::Lite;
>>
>> my @urls  = (
>> 'https://mojolicious.org',
>> 'https://metacpan.org',
>> 'https://perlmonks.org',
>> 'http://www.google.com',
>> );
>>
>> get '/titles' => sub {
>> my $ua= Mojo::UserAgent->new;
>> my $async = Mojo::IOLoop::Delay->new;
>>
>> $async->steps(
>> sub {
>> my $self = shift;
>> $ua->get($_, $self->begin) for @urls;
>> },
>> sub {
>> my ($self, @tx) = @_;
>> my $titles  = [];
>>
>> foreach (@tx) {
>> my $title  = '';
>> my $status = $_->res->is_success ? 'Connected' : $_->
>> error->{message};
>>
>> if ( $_->res->is_success ) {
>> $title = $_->res->dom->at('title')->text;
>> $title =~ s/^\s+|\s+$//g; # couldn't get Mojo::Util
>> trim function to work
>> }
>>
>> push @$titles, {host => $_->req->url->host, status =>
>> $status, title => $title};
>> }
>> $self->stash(titles => $titles);
>> }
>> );
>> } => 'titles';
>>
>> app->start;
>>
>> __DATA__
>>
>> @@ titles.html.ep
>> % layout 'titles';
>>
>> %= dumper stash('titles')
>>
>> 
>> 
>>   Host
>>   Status
>>   Title
>> 
>>
>> 
>> % for my $server ( stash('titles') ) {
>>   
>> <%= $server->{host} %>
>> <%= $server->{status} %>
>> <%= $server->{title} =%>
>>   
>> % }
>> 
>>
>> @@ layouts/titles.html.ep
>> 
>>
>> 
>>
>> 
>>   Titles
>> 
>>
>> 
>> Titles
>> <%= content =%>
>> 
>>
>> 
>>
>>
>> Here's the output:
>>
>>> $ ./titles_web.pl get /titles
>>> [Thu Feb  7 09:45:54 2019] [debug] GET "/titles" (fcf21ce8)
>>> [Thu Feb  7 09:45:54 2019] [debug] Routing to a callback
>>> [Thu Feb  7 09:45:54 2019] [debug] Rendering template "titles.html.ep"
>>> from DATA section
>>> [Thu Feb  7 09:45:54 2019] [debug] Rendering template
>>> "layouts/titles.html.ep" from DATA section
>>> [Thu Feb  7 09:45:54 2019] [debug] 200 OK (0.00267s, 374.532/s)
>>> 
>>>
>>> 
>>>
>>> 
>>>   Titles
>>> 
>>>
>>> 
>>> Titles
>>>
>>> undef
>>>
>>>
>>> 
>>> 
>>>   Host
>>>   Status
>>>   Title
>>> 
>>>
>>> 
>>>   
>>> 
>>> 
>>> 
>>> 
>>> 
>>> 
>>>
>>> 
>>>
>>
>> --
> 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 

Re: [Mojolicious] Re: Need help with stashing data and retrieving it in the template

2019-02-07 Thread Stefan Adams
Take a look at this Advent Calendar blog post
.
Specifically, "Web Apps" at the bottom.

On Thu, Feb 7, 2019 at 11:57 AM Ron Bergin  wrote:

> Note: I cross posted this question on perlmonks
> https://perlmonks.org/?node_id=1229558
>
> On Thursday, February 7, 2019 at 9:48:20 AM UTC-8, Ron Bergin wrote:
>>
>> I'm brand new to Mojolicious and working on a lite script that goes to 75
>> urls and retrieves text in an h3 tag. I've worked up a working test script
>> that outputs to console and now I'm working on adjusting it to be a web app
>> but I can't seem to get the correct syntax to store/retrieve the data.
>> What am I doing wrong?
>>
>> #!/usr/bin/perl
>>
>> use Mojolicious::Lite;
>>
>> my @urls  = (
>> 'https://mojolicious.org',
>> 'https://metacpan.org',
>> 'https://perlmonks.org',
>> 'http://www.google.com',
>> );
>>
>> get '/titles' => sub {
>> my $ua= Mojo::UserAgent->new;
>> my $async = Mojo::IOLoop::Delay->new;
>>
>> $async->steps(
>> sub {
>> my $self = shift;
>> $ua->get($_, $self->begin) for @urls;
>> },
>> sub {
>> my ($self, @tx) = @_;
>> my $titles  = [];
>>
>> foreach (@tx) {
>> my $title  = '';
>> my $status = $_->res->is_success ? 'Connected' : $_->
>> error->{message};
>>
>> if ( $_->res->is_success ) {
>> $title = $_->res->dom->at('title')->text;
>> $title =~ s/^\s+|\s+$//g; # couldn't get Mojo::Util
>> trim function to work
>> }
>>
>> push @$titles, {host => $_->req->url->host, status =>
>> $status, title => $title};
>> }
>> $self->stash(titles => $titles);
>> }
>> );
>> } => 'titles';
>>
>> app->start;
>>
>> __DATA__
>>
>> @@ titles.html.ep
>> % layout 'titles';
>>
>> %= dumper stash('titles')
>>
>> 
>> 
>>   Host
>>   Status
>>   Title
>> 
>>
>> 
>> % for my $server ( stash('titles') ) {
>>   
>> <%= $server->{host} %>
>> <%= $server->{status} %>
>> <%= $server->{title} =%>
>>   
>> % }
>> 
>>
>> @@ layouts/titles.html.ep
>> 
>>
>> 
>>
>> 
>>   Titles
>> 
>>
>> 
>> Titles
>> <%= content =%>
>> 
>>
>> 
>>
>>
>> Here's the output:
>>
>>> $ ./titles_web.pl get /titles
>>> [Thu Feb  7 09:45:54 2019] [debug] GET "/titles" (fcf21ce8)
>>> [Thu Feb  7 09:45:54 2019] [debug] Routing to a callback
>>> [Thu Feb  7 09:45:54 2019] [debug] Rendering template "titles.html.ep"
>>> from DATA section
>>> [Thu Feb  7 09:45:54 2019] [debug] Rendering template
>>> "layouts/titles.html.ep" from DATA section
>>> [Thu Feb  7 09:45:54 2019] [debug] 200 OK (0.00267s, 374.532/s)
>>> 
>>>
>>> 
>>>
>>> 
>>>   Titles
>>> 
>>>
>>> 
>>> Titles
>>>
>>> undef
>>>
>>>
>>> 
>>> 
>>>   Host
>>>   Status
>>>   Title
>>> 
>>>
>>> 
>>>   
>>> 
>>> 
>>> 
>>> 
>>> 
>>> 
>>>
>>> 
>>>
>>
>> --
> 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] Need help with stashing data and retrieving it in the template

2019-02-07 Thread Ron Bergin
I'm brand new to Mojolicious and working on a lite script that goes to 75 
urls and retrieves text in an h3 tag. I've worked up a working test script 
that outputs to console and now I'm working on adjusting it to be a web app 
but I can't seem to get the correct syntax to store/retrieve the data.  
What am I doing wrong?

#!/usr/bin/perl

use Mojolicious::Lite;

my @urls  = (
'https://mojolicious.org',
'https://metacpan.org',
'https://perlmonks.org',
'http://www.google.com',
);

get '/titles' => sub {
my $ua= Mojo::UserAgent->new;
my $async = Mojo::IOLoop::Delay->new;

$async->steps(
sub { 
my $self = shift;
$ua->get($_, $self->begin) for @urls;
},
sub {
my ($self, @tx) = @_;
my $titles  = [];

foreach (@tx) {
my $title  = '';
my $status = $_->res->is_success ? 'Connected' : $_->error
->{message};

if ( $_->res->is_success ) {
$title = $_->res->dom->at('title')->text;
$title =~ s/^\s+|\s+$//g; # couldn't get Mojo::Util 
trim function to work
}

push @$titles, {host => $_->req->url->host, status => 
$status, title => $title};
}
$self->stash(titles => $titles);
}
);
} => 'titles';

app->start;

__DATA__

@@ titles.html.ep
% layout 'titles';

%= dumper stash('titles')



  Host
  Status
  Title



% for my $server ( stash('titles') ) {
  
<%= $server->{host} %>
<%= $server->{status} %>
<%= $server->{title} =%>
  
% }


@@ layouts/titles.html.ep





  Titles



Titles
<%= content =%>





Here's the output:

> $ ./titles_web.pl get /titles
> [Thu Feb  7 09:45:54 2019] [debug] GET "/titles" (fcf21ce8)
> [Thu Feb  7 09:45:54 2019] [debug] Routing to a callback
> [Thu Feb  7 09:45:54 2019] [debug] Rendering template "titles.html.ep" 
> from DATA section
> [Thu Feb  7 09:45:54 2019] [debug] Rendering template 
> "layouts/titles.html.ep" from DATA section
> [Thu Feb  7 09:45:54 2019] [debug] 200 OK (0.00267s, 374.532/s)
> 
>
> 
>
> 
>   Titles
> 
>
> 
> Titles
>
> undef
>
>
> 
> 
>   Host
>   Status
>   Title
> 
>
> 
>   
> 
> 
> 
> 
> 
> 
>
> 
>

-- 
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] Using Yancy REST API

2019-02-07 Thread Veesh Goldman
Hi,
I've been toying around with Mojo, and I was following along the tutorial
from preaction.me for a to-do app with Yancy.

If like to add multi Tennant support, which I see is built into Yancy, but
I want to know the best way to access the REST endpoints from inside of a
route.

I tried having the ua make a call to the endpoint, but for some reason it
gave me inconsistent results.

It's there some function available to use the built in rest API? It feels
stupid to have to rewrite SQL queries if I have an API to do it already.

-- 
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.