Sunteți pe pagina 1din 23

Web Mashups with Catalyst

Jon Allen (JJ)


http://perl.jonallen.info - jj@jonallen.info
The Plan
•  Build a “blended search” application using Catalyst

•  Aggregate search results from:


–  Twitter
–  Google Blog Search
–  Flickr

Web Mashups with Catalyst


perl.jonallen.info

About Catalyst
•  Perl-based web framework

•  MVC architecture
•  Flexible URL dispatcher
–  Self-contained, no external file
•  Philosophy / coding practices
–  DRY – Don’t Repeat Yourself
–  Glue – Thin Controllers / Model Wrappers
–  Maintainability, maintainability, and maintainability

Web Mashups with Catalyst


perl.jonallen.info

Create the application
$ catalyst.pl MyApp

MyApp/
lib/
MyApp.pm
MyApp/
Model/
View/
Controller/
Root.pm
root/
static/
script/
myapp.conf

Web Mashups with Catalyst


perl.jonallen.info

Create the application
$ catalyst.pl MyApp

MyApp/
lib/
MyApp.pm Core application
MyApp/ •  MyApp.pm
Model/ •  Setup, plugins,
View/ default config
Controller/ •  Root.pm
Root.pm •  Root controller
root/
static/
script/
myapp.conf

Web Mashups with Catalyst


perl.jonallen.info

Create the application
$ catalyst.pl MyApp

MyApp/
lib/
MyApp.pm Data
MyApp/ •  Static files
Model/ •  CSS, images,
View/ JavaScript, etc
Controller/ •  Templates
Root.pm •  Form definitions
root/
static/
script/
myapp.conf

Web Mashups with Catalyst


perl.jonallen.info

Create the application
$ catalyst.pl MyApp

MyApp/
lib/
MyApp.pm Scripts
MyApp/ •  Development server
Model/ •  Production server
View/ •  Component creation
Controller/ •  Models, Views,
Root.pm Controllers
root/
static/
script/
myapp.conf

Web Mashups with Catalyst


perl.jonallen.info

Create the application
$ catalyst.pl MyApp

MyApp/
lib/
MyApp.pm Extras
MyApp/ •  Config file
Model/ •  Tests (t/ directory)
View/ •  Makefile.PL
Controller/ •  Dependencies
Root.pm •  README / Changes
root/
static/
script/
myapp.conf

Web Mashups with Catalyst


perl.jonallen.info

Create a view
•  Install Catalyst::View::TT
$ script/myapp_create.pl view TT TT
$ mkdir root/templates

•  Edit "lib/MyApp.pm"
__PACKAGE__->config->{default_view} = 'TT';

__PACKAGE__->config(
'View::TT' => {
INCLUDE_PATH =>
__PACKAGE__->path_to('root','templates'),
}
);

Web Mashups with Catalyst


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"
sub index :Path :Args(0) {
Attributes – controls
my ( $self, $c ) = @_; URL dispatcher

$c->stash->{template} = 'homepage.tt';
}

$c = Catalyst Stash = per-request


context object storage

Web Mashups with Catalyst


perl.jonallen.info

Create a search controller
$ script/myapp_create.pl controller Search

•  Edit "lib/MyApp/Controller/Search.pm"
sub index :Path :Args(0) {
my ( $self, $c ) = @_;

my $q = $c->request->param('q') or do {
$c->response->redirect($c->uri_for('/'));
return;
};
"Thin Controller"
no business logic
$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);
}

Web Mashups with Catalyst


perl.jonallen.info

Models
•  We don't have any (yet), but we have defined an
interface
–  Single method (search), returns data structure containing
list of objects
–  Create "root/templates/results.tt" Stash available
in templates
[% FOREACH tweet IN tweets.results %]
<div class="tweet">
<div class="user">[% tweet.from_user %]</div>
<div class="text">[% tweet.text %]</div>
</div>
[% END %]

Web Mashups with Catalyst


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


perl.jonallen.info

Building the Twitter model
•  Net::Twitter::Search
–  Existing module from CPAN
–  Has a "search" method, gives the output we need

New Model Called "Twitter"

$ script/myapp_create.pl model Twitter \


Adaptor Net::Twitter::Search

Use helper module Based on


Catalyst::Model::Adaptor Net::Twitter::Search

Web Mashups with Catalyst


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


perl.jonallen.info

Creating the Google model
•  REST::Google::Search::Blogs
–  Different interface, needs config (API key)
–  We need to build our own model
$ script/myapp_create.pl model Google

•  Edit "lib/MyApp.pm"
__PACKAGE__->config('Model::Google' => {
referer => 'http://yoursite.com'
});

•  Can override this in "myapp.conf"

Web Mashups with Catalyst


perl.jonallen.info

Coding the Google model
•  Edit "lib/MyApp/Model/Google.pm"
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',
);
}

Web Mashups with Catalyst


perl.jonallen.info

Displaying the Google results
•  Edit "root/templates/results.tt"
[% FOREACH blog IN blogs.responseData.results %]
<div class="blog">
<p class="title">
<a href="[% blog.postUrl %]">
[% blog.titleNoFormatting %]
</a> by [% blog.author %]
</p>
<p class="content">
[% blog.content %]
</p>
</div>
[% END %]

Web Mashups with Catalyst


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

use Catalyst qw/


ConfigLoader
Static::Simple
Unicode
/;

Web Mashups with Catalyst


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


perl.jonallen.info

Screenshot

Web Mashups with Catalyst


perl.jonallen.info

Summary
•  Catalyst is a great way to turn existing code into a
web 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 wrapper
around an external class
–  Supports code reuse

Web Mashups with Catalyst


perl.jonallen.info

KTHKSBYE!

Thank you for listening!

Any questions?
http://perl.jonallen.info/talks

Web Mashups with Catalyst


perl.jonallen.info

S-ar putea să vă placă și