web mashups with catalyst
Post on 30-May-2018
233 Views
Preview:
TRANSCRIPT
-
8/14/2019 Web Mashups With Catalyst
1/23
Web Mashups with Catalyst
Jon Allen (JJ)
http://perl.jonallen.info - jj@jonallen.info
-
8/14/2019 Web Mashups With Catalyst
2/23
perl.jonallen.info
The Plan
Build a blended search application using CatalystAggregate search results from:
Twitter Google Blog Search Flickr
Web Mashups with Catalyst
-
8/14/2019 Web Mashups With Catalyst
3/23
perl.jonallen.info
About Catalyst
Perl-based web framework MVC architecture Flexible URL dispatcher
Self-contained, no external file Philosophy / coding practices
DRY Dont Repeat Yourself Glue Thin Controllers / Model Wrappers Maintainability, maintainability, and maintainability
Web Mashups with Catalyst
-
8/14/2019 Web Mashups With Catalyst
4/23
perl.jonallen.info
Create the application
Web Mashups with Catalyst
$ catalyst.pl MyApp
MyApp/lib/
MyApp.pmMyApp/
Model/View/Controller/
Root.pmroot/
static/
script/myapp.conf
-
8/14/2019 Web Mashups With Catalyst
5/23
perl.jonallen.info
Create the application
Web Mashups with Catalyst
$ catalyst.pl MyApp
MyApp/lib/
MyApp.pmMyApp/
Model/View/Controller/
Root.pmroot/
static/
script/myapp.conf
Core applicationMyApp.pm
Setup, plugins,default config
Root.pmRoot controller
-
8/14/2019 Web Mashups With Catalyst
6/23
perl.jonallen.info
Create the application
Web Mashups with Catalyst
$ catalyst.pl MyApp
MyApp/lib/
MyApp.pmMyApp/
Model/View/Controller/
Root.pmroot/
static/
script/myapp.conf
DataStatic files
CSS, images,JavaScript, etc
TemplatesForm definitions
-
8/14/2019 Web Mashups With Catalyst
7/23
perl.jonallen.info
Create the application
Web Mashups with Catalyst
$ catalyst.pl MyApp
MyApp/lib/
MyApp.pmMyApp/
Model/View/Controller/
Root.pmroot/
static/
script/myapp.conf
ScriptsDevelopment serverProduction serverComponent creation
Models, Views,Controllers
-
8/14/2019 Web Mashups With Catalyst
8/23
perl.jonallen.info
Create the application
Web Mashups with Catalyst
$ catalyst.pl MyApp
MyApp/lib/
MyApp.pmMyApp/
Model/View/Controller/
Root.pmroot/
static/
script/myapp.conf
ExtrasConfig fileTests (t/ directory)Makefile.PLDependenciesREADME / Changes
-
8/14/2019 Web Mashups With Catalyst
9/23
perl.jonallen.info
Install Catalyst::View::TT
Edit "lib/MyApp.pm"
Create a view
Web Mashups with Catalyst
__PACKAGE__->config->{default_view} = 'TT';
__PACKAGE__->config('View::TT' => {
INCLUDE_PATH =>__PACKAGE__->path_to('root','templates'),
});
$ script/myapp_create.pl view TT TT$ mkdir root/templates
-
8/14/2019 Web Mashups With Catalyst
10/23
perl.jonallen.info
Set up the homepage
Create "root/templates/homepage.tt" Text box that submits query to "/search"
Edit "lib/MyApp/Controller/Root.pm"
Web Mashups with Catalyst
sub index :Path :Args(0) {my ( $self, $c ) = @_;
$c->stash->{template} = 'homepage.tt';}
Stash = per-requeststorage$c = Catalystcontext object
Attributes controlsURL dispatcher
-
8/14/2019 Web Mashups With Catalyst
11/23
perl.jonallen.info
Create a search controller
Web Mashups with Catalyst
$ script/myapp_create.pl controller Search
sub index :Path :Args(0) {my ( $self, $c ) = @_;
my $q = $c->request->param('q') or do {$c->response->redirect($c->uri_for('/'));return;
};
$c->stash->{query} = $q;$c->stash->{template} = 'results.tt';$c->stash->{tweets} = $c->model('Twitter')->search($q);
$c->stash->{blogs} = $c->model('Google')->search($q);$c->stash->{photos} = $c->model('Flickr')->search($q);}
Edit "lib/MyApp/Controller/Search.pm"
"Thin Controller"no business logic
-
8/14/2019 Web Mashups With Catalyst
12/23
perl.jonallen.info
Models
We don't have any (yet), but we have defined aninterface Single method (search), returns data structure containing
list of objects Create "root/templates/results.tt"
Web Mashups with Catalyst
[% FOREACH tweet IN tweets.results %]
[% tweet.from_user %][% tweet.text %]
[% END %]
Stash availablein templates
-
8/14/2019 Web Mashups With Catalyst
13/23
perl.jonallen.info
Model wrappers
Model = thin wrapper around an existing class Makes business logic available for use in other systems
Helper modules Catalyst::Model::Adaptor Catalyst::Model::Factory Catalyst::Model::Factory::PerRequest
Which one should you use? Depends when you want the class instantiated
Web Mashups with Catalyst
-
8/14/2019 Web Mashups With Catalyst
14/23
perl.jonallen.info
Building the Twitter model
Net::Twitter::Search Existing module from CPAN Has a "search" method, gives the output we need
Web Mashups with Catalyst
$ script/myapp_create.pl model Twitter \Adaptor Net::Twitter::Search
New Model Called "Twitter"
Use helper moduleCatalyst::Model::Adaptor Based onNet::Twitter::Search
-
8/14/2019 Web Mashups With Catalyst
15/23
perl.jonallen.info
Twitter model done!
But we haven't written any code!All done by Catalyst::Model::Adaptor Net::Twitter::Search already has the interface we
want (search method), so it will just work
Web Mashups with Catalyst
-
8/14/2019 Web Mashups With Catalyst
16/23
perl.jonallen.info
Creating the Google model
REST::Google::Search::Blogs Different interface, needs config (API key) We need to build our own model
Edit "lib/MyApp.pm"
Can override this in "myapp.conf"Web Mashups with Catalyst
$ script/myapp_create.pl model Google
__PACKAGE__->config('Model::Google' => {referer => 'http://yoursite.com'
});
-
8/14/2019 Web Mashups With Catalyst
17/23
perl.jonallen.info
Coding the Google model
Edit "lib/MyApp/Model/Google.pm"
Web Mashups with Catalyst
use REST::Google::Search::Blogs;
sub search {my ($self, $query) = @_;
REST::Google::Search::Blogs->http_referer($self->{referer}
);return REST::Google::Search::Blogs->new(
q => $query,rsz => 'large',
);}
-
8/14/2019 Web Mashups With Catalyst
18/23
perl.jonallen.info
Displaying the Google results
Edit "root/templates/results.tt"
Web Mashups with Catalyst
[% FOREACH blog IN blogs.responseData.results %]
[% blog.titleNoFormatting %] by [% blog.author %]
[% blog.content %]
[% END %]
-
8/14/2019 Web Mashups With Catalyst
19/23
perl.jonallen.info
Unicode
Twitter and Google APIs both return Unicode We need to enable Unicode support in Catalyst Install Catalyst::Plugin::Unicode from CPAN Edit "lib/MyApp.pm" and add "Unicode" to the list of
plugins
Web Mashups with Catalyst
use Catalyst qw/ConfigLoaderStatic::SimpleUnicode
/;
-
8/14/2019 Web Mashups With Catalyst
20/23
perl.jonallen.info
Flickr
Flickr::Embed Flickr::API Same concept as the Google API
Store our API key in config Create model with a "search" method to return the
results Enough code, let's see the app!
Web Mashups with Catalyst
-
8/14/2019 Web Mashups With Catalyst
21/23
perl.jonallen.info
Screenshot
Web Mashups with Catalyst
-
8/14/2019 Web Mashups With Catalyst
22/23
perl.jonallen.info
Summary
Catalyst is a great way to turn existing code into aweb application Mainly config, very little programming involved! Easy to extend and maintain
Models can be any source of business logic or data Not just databases!
Where possible, models should be a thin wrapperaround an external class Supports code reuse
Web Mashups with Catalyst
-
8/14/2019 Web Mashups With Catalyst
23/23
perl.jonallen.infoWeb Mashups with Catalyst
KTHKSBYE!
Thank you for listening!
Any questions?
http://perl.jonallen.info/talks
top related