Re: [Mojolicious] Re: newbie question about routing

2019-01-05 Thread Stefan Adams
Seems like you understand your problem and have a well-thought out design
pattern!  I think what you are wanting to accomplish elegantly is exactly
what Mojolicious was built for
!

Your two sample routes can be condensed into one to handle any number of
customers, subclassed from a base class:

  $r->get('/api/v1/:controller/box/:id')->to('#open_box');

$ mojo generate app
   :
$ cat my_app/lib/MyApp.pm my_app/lib/MyApp/Controller/Customer.pm
my_app/lib/MyApp/Controller/Kwikemart.pm
my_app/lib/MyApp/Controller/Ezymart.pm
package MyApp;
use Mojo::Base 'Mojolicious';
sub startup {
  my $self = shift;
  my $r = $self->routes;
  $r->get('/api/v1/:controller/box/:id')->to('#open_box');
}
1;
package MyApp::Controller::Customer;
use Mojo::Base 'Mojolicious::Controller';
sub open_box { die "open_box method not implemented by subclass" }
1;
package MyApp::Controller::Kwikemart;
use Mojo::Base 'MyApp::Controller::Customer';
sub open_box {
  my $self = shift;
  $self->render(text => 'Hello, Kwikemart Customer!');
}
1;
package MyApp::Controller::Ezymart;
use Mojo::Base 'MyApp::Controller::Customer';
sub open_box {
  my $self = shift;
  $self->render(text => 'Welcome to Ezymart!');
}
1;
$ perl my_app/script/my_app routes
/api/v1/:controller/box/:id  GET  apiv1controllerboxid
*$ perl my_app/script/my_app get /api/v1/kwikemart/box/1*

*Hello, Kwikemart Customer!*
*$ perl my_app/script/my_app get /api/v1/ezymart/box/1*

*Welcome to Ezymart!*

On Wed, Jan 2, 2019 at 9:04 PM  wrote:

> Hey, no problem. Mojolicious so far, can do just about anything I can
> think of, without much difficulty, as most stuff just work out of the box,
> which is a pleasant surprise.
>
> If you don't mind, I'll take a step back, and describe my design and
> reason behind it, and see if you can maybe spot something that's wrong with
> my approach. Here's the hypothetical scenario:
>
> 1. Two customers, one Kwikemart, and one Ezymart, they both handle boxes,
> but in different ways, one open it with a cutter, say, and the other one
> insist on using bare hands.
> 2. So I build one Customer class, and extend it, to implement actual
> customer. Both Kwikemart and Ezymart are children of Customer class, and
> each has a open_box() method, but within it, they each do their own thing.
> 3. In order to expose the boxes' content as RESTful resource, the url to
> access them, will be something like /api/v1/:customer/box/:boxid
>
> Of course, I could just code the route explicitly, since there's only two
> customer, by having:
>
>
>  $r->get('/api/v1/kwikemart/box/:id')->to('Customer::Kwikemart#open_box');
>  $r->get('/api/v1/ezymart/box/:id')->to('Customer::Ezymart#open_box');
>
> However, it just feels not elegant. The name of the class, is the only
> difference between the two different routes, and it'll be great to
> generalize it within the dispatching process, so if I have hundreds
> customers to do tomorrow, each of their own variants of 'open_box()'
> action, can be routed with just one call, instead of hundreds, as long as
> the url conforms to the '/api/v1/:customer/box/:id' format.
>
> Maybe I am just trying to be too clever for my own good, but I really
> think there must be an easy and elegant way to do this?
>
>
>
>
> On Wednesday, January 2, 2019 at 4:27:44 PM UTC-6, Stefan Adams wrote:
>>
>> Hi!  I'm glad you were able to figure that out and even using the more
>> advanced hook feature, but if I'm understanding your objective correctly, I
>> would advise that you follow the references I shared.  In essence, if I'm
>> reading it correctly, you are using a framework and then solving problems
>> on your own which are already solved by the framework.
>>
>> I just want to make sure you get off to a good start with Mojolicious so
>> that you can reap the full benefits of the framework and not become
>> irritated that it's not doing things that you want easily -- essentially
>> questioning why you're using this framework in the first place.  :)
>>
>> I'd recommend starting with a Mojolicious::Lite app.
>>
>> Start with the Hello World
>> 
>> in the Tutorial.  And then expand it for your needs, perhaps like so:
>>
>> #!/usr/bin/env perluse Mojolicious::Lite;
>> get '/:customer' => sub {
>>   my $c = shift;
>>   $c->render(inline => 'Hello, <%= param "customer" %>!');};
>>
>> app->start;
>>
>>
>> Also, just to show some other equivalent ways to do the same thing:
>>
>> #!/usr/bin/env perluse Mojolicious::Lite;
>> get '/:customer' => sub {
>>   my $c = shift;  my $customer = $c->param('customer');
>>   $c->render(inline => 'Hello, <%= $customer %>!', customer => $customer);};
>>
>> app->start;
>>
>>
>> Also:
>>
>> #!/usr/bin/env perluse Mojolicious::Lite;
>> get '/:customer' => sub {
>>   my $c = shift;  $c->render(inline => 'Hello, <%= $c->param("customer") 
>> %>!');};
>>
>> app->start;
>>
>>
>> On Wed, 

Re: [Mojolicious] Re: newbie question about routing

2019-01-02 Thread hwmbo . lord
Hey, no problem. Mojolicious so far, can do just about anything I can think 
of, without much difficulty, as most stuff just work out of the box, which 
is a pleasant surprise.

If you don't mind, I'll take a step back, and describe my design and reason 
behind it, and see if you can maybe spot something that's wrong with my 
approach. Here's the hypothetical scenario:

1. Two customers, one Kwikemart, and one Ezymart, they both handle boxes, 
but in different ways, one open it with a cutter, say, and the other one 
insist on using bare hands.
2. So I build one Customer class, and extend it, to implement actual 
customer. Both Kwikemart and Ezymart are children of Customer class, and 
each has a open_box() method, but within it, they each do their own thing.
3. In order to expose the boxes' content as RESTful resource, the url to 
access them, will be something like /api/v1/:customer/box/:boxid

Of course, I could just code the route explicitly, since there's only two 
customer, by having:


 $r->get('/api/v1/kwikemart/box/:id')->to('Customer::Kwikemart#open_box');
 $r->get('/api/v1/ezymart/box/:id')->to('Customer::Ezymart#open_box');

However, it just feels not elegant. The name of the class, is the only 
difference between the two different routes, and it'll be great to 
generalize it within the dispatching process, so if I have hundreds 
customers to do tomorrow, each of their own variants of 'open_box()' 
action, can be routed with just one call, instead of hundreds, as long as 
the url conforms to the '/api/v1/:customer/box/:id' format.

Maybe I am just trying to be too clever for my own good, but I really think 
there must be an easy and elegant way to do this?




On Wednesday, January 2, 2019 at 4:27:44 PM UTC-6, Stefan Adams wrote:
>
> Hi!  I'm glad you were able to figure that out and even using the more 
> advanced hook feature, but if I'm understanding your objective correctly, I 
> would advise that you follow the references I shared.  In essence, if I'm 
> reading it correctly, you are using a framework and then solving problems 
> on your own which are already solved by the framework.
>
> I just want to make sure you get off to a good start with Mojolicious so 
> that you can reap the full benefits of the framework and not become 
> irritated that it's not doing things that you want easily -- essentially 
> questioning why you're using this framework in the first place.  :)
>
> I'd recommend starting with a Mojolicious::Lite app.
>
> Start with the Hello World 
>  
> in the Tutorial.  And then expand it for your needs, perhaps like so:
>
> #!/usr/bin/env perluse Mojolicious::Lite;
> get '/:customer' => sub {
>   my $c = shift;
>   $c->render(inline => 'Hello, <%= param "customer" %>!');};
>
> app->start;
>
>
> Also, just to show some other equivalent ways to do the same thing:
>
> #!/usr/bin/env perluse Mojolicious::Lite;
> get '/:customer' => sub {
>   my $c = shift;  my $customer = $c->param('customer');
>   $c->render(inline => 'Hello, <%= $customer %>!', customer => $customer);};
>
> app->start;
>
>
> Also:
>
> #!/usr/bin/env perluse Mojolicious::Lite;
> get '/:customer' => sub {
>   my $c = shift;  $c->render(inline => 'Hello, <%= $c->param("customer") 
> %>!');};
>
> app->start;
>
>
> On Wed, Jan 2, 2019 at 3:39 PM > wrote:
>
>> Thanks Stefan. 
>>
>>
>> So, trying a different tack, I was able to parse the url of a request 
>> with a 'before_depatch' hook, and figure out what Customer Class should be 
>> used, before routing happens.
>>
>> Assuming the request url is /kwikemart/liquor/22, I figure out the 
>> customer name (kwikemart) like this:
>>
>>
>> sub startup {
>> my $self = shift;
>> $self->req->url->path =~ m#^/(\w+)/.+#;
>> $customer = $1;}
>>
>>
>> The question is, where to store this $customer, so I can use it to 
>> specify the correct route later? I still don't quite get how Mojo::Base 
>> Class work. Not sure how to correctly create custom attribute, that's local 
>> to each request, so it can be used within a $r->to() call.
>>
>>
>>
>>
>>
>> On Wednesday, January 2, 2019 at 4:34:20 AM UTC-6, hwmbo...@gmail.com 
>> wrote:
>>>
>>> Hi. I want to route a request to a controller that's dynamically 
>>> constructed, base on a place holder in the url. To illustrate, I want do 
>>> something like the following:
>>>
>>> use Mojo::Base 'Mojolicious';
>>>
>>> sub startup {
>>>
>>> my $r = $self->routes;
>>>
>>>my $route = $r->get('/:customer/boxs/:id');
>>>  
>>>
>>> [ somehow, extract the :customer holder into a variable $customer, then ]
>>>
>>>$route->(
>>>
>>> controller => "$customer::boxs',
>>>
>>> action => 'get_list',
>>>
>>> );
>>>
>>> }
>>>
>>>
>>> I tried but can't seem to find way to this, and I'm not sure if this is 
>>> possible?
>>> Some pointer is appreciated. Thanks. 
>>>
>>>  
>>>
>>> -- 
>> You received this message because you are subscribed 

Re: [Mojolicious] Re: Newbie question

2018-04-09 Thread Jan Eskilsson
Hi Tekki


search 1works but not search 2 so i guess I'm doing something wrong there.
The debug helper works though and the render tries to render the correct
template but it throws the error that $mytest needs to be declared.
sub search1 {
my $self = shift;

   $self->render(template =>,'stockitems/search',  mytest => 'my test');
}

sub search2 {
my $self = shift;

 $self->stockitems->search_p('*')->then(sub {
  my $results = shift;
  $self->debug('we have searched successfully');
  $self->render(template =>,'stockitems/search',  mytest => 'my test');
})->catch (sub {
  my $err = shift;

  $self->debug($err);
  $self->debug('we have some error');
  $self->render(template =>,'stockitems/search',  mytest => 'Errors');
})->wait;;

}


Thanks for any input to what I'm doing wrong.


Best Regards
Jan

2018-04-09 14:31 GMT+02:00 Tekki :

> As long as you get an error message, nothing is broken. ;)
> Mojolicious tries to render the correct template, so the error must be
> somewhere on the way to it.
>
> Am Montag, 9. April 2018 12:40:13 UTC+2 schrieb Jan Eskilsson:
>>
>> I start to believe that my installation is somehow broken, I also have
>> other strange behaviour. I will reinstall to see if it helps
>>
>> --
> 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.
>



-- 
Titles mean nothing.  The one with a servant's heart is the leader.

Please consider the environment before you print this email.

All incoming and outgoing emails and any attachments are subjected to a
virus scanner and are believed to be free of any virus, or any other defect
which might affect any computer or IT system into which they are received
and opened. Therefore, it is the responsibility of the recipient to ensure
that they are virus free and no responsibility is accepted by Jan Eskilsson
 for any loss or damage arising in any way from receipt or use thereof.

-- 
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: Newbie question

2018-04-09 Thread Tekki
As long as you get an error message, nothing is broken. ;)
Mojolicious tries to render the correct template, so the error must be 
somewhere on the way to it.

Am Montag, 9. April 2018 12:40:13 UTC+2 schrieb Jan Eskilsson:
>
> I start to believe that my installation is somehow broken, I also have 
> other strange behaviour. I will reinstall to see if it helps
>
>

-- 
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: Newbie question

2018-04-09 Thread Jan Eskilsson
Hi Tekki


Thank you for your response.

I start to believe that my installation is somehow broken, I also have
other strange behaviour. I will reinstall to see if it helps


Best Regards
Jan

2018-04-09 9:56 GMT+02:00 Tekki :

> This $self->render you have edited is probably not the place where you
> actually render the template.
>
> Regards
> Tekki
>
> Am Sonntag, 8. April 2018 17:54:18 UTC+2 schrieb Jan Eskilsson:
>>
>> In my controller i do
>>   $self->render(mytest => 'my test');
>>
>> --
> 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.
>



-- 
Titles mean nothing.  The one with a servant's heart is the leader.

Please consider the environment before you print this email.

All incoming and outgoing emails and any attachments are subjected to a
virus scanner and are believed to be free of any virus, or any other defect
which might affect any computer or IT system into which they are received
and opened. Therefore, it is the responsibility of the recipient to ensure
that they are virus free and no responsibility is accepted by Jan Eskilsson
 for any loss or damage arising in any way from receipt or use thereof.

-- 
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: Newbie question : How to run a bash script from inside a Mojolicious application ?

2017-10-31 Thread Jan Henning Thorsen
Hi,

You could also consider looking into these alternatives:

* https://metacpan.org/pod/Mojo::IOLoop::Subprocess - Run long running 
(blocking) code in a sub process
* https://metacpan.org/pod/Mojo::IOLoop::ForkCall - Pretty similar to 
Subprocess
* https://metacpan.org/pod/Mojo::IOLoop::ReadWriteFork - Similar to 
Subprocess/ForkCall, but you can write and read to the interactive process 
* https://metacpan.org/pod/Minion - Not quite the same, but it could be 
what you really want.


On Tuesday, October 31, 2017 at 1:50:32 AM UTC+8, Stefan Adams wrote:
>
>
>
> On Mon, Oct 30, 2017 at 8:56 AM, jay m  wrote:
>
>> pascal,
>>
>> system() and backticks work fine from a Mojolicious app. your problem is 
>> probably one of 3 things:
>>
>> * the environment inside the mojo app is different than your interactive 
>> shell environment. compare %ENV to your shell's printenv
>> * the current working directory when running the shell command isn't what 
>> you expected. try print `pwd`;
>> * the user running the mojo app doesn't have the right permissions for 
>> the command or files you're trying to use
>>
>
> Great advice.  I might also add that you need to consider how long the 
> bash script will take to execute and the impact it will have on the 
> Mojolicious application itself.  Being new to Mojolicious, I'd advise you 
> to read up on blocking and non-blocking operations 
> 
> .
>

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