Permalink
Browse files

widget cpanmodules

  • Loading branch information...
1 parent a38d025 commit b6faf1e2c5a69dd9a8b38d8087d867ba1c07ffc6 @yanick committed Mar 6, 2011
View
@@ -30,6 +30,9 @@ template_extension .mason
<ironman>
id yanick
</ironman>
+<cpan_modules>
+ author yanick
+</cpan_modules>
<cpan_author>
dist XML::XSS
dist XML::XPathScript
View
@@ -16,10 +16,12 @@ use Catalyst qw/
ConfigLoader
Static::Simple
Cache
- PageCache
Sitemap
VersionedURI
+ SubRequest
+ PageCache
/;
+# PageCache
extends 'Catalyst';
@@ -48,7 +50,7 @@ __PACKAGE__->config(
class => 'Cache::FileCache',
} },
'Plugin::PageCache' => {
- set_http_headers => 1
+ set_http_headers => 1,
},
);
@@ -0,0 +1,100 @@
+package Galuga::Controller::Widget::CpanModules;
+use Moose;
+use namespace::autoclean;
+
+use LWP::Simple;
+use JSON;
+use DateTime::Format::Flexible;
+use Template::Declare;
+
+BEGIN {extends 'Catalyst::Controller'; }
+
+sub distributions {
+ my( $self, $c ) = @_;
+
+ my $page =
+ LWP::Simple::get sprintf 'http://api.metacpan.org/dist/_search?q=author:"%s"',
+ $c->config->{widgets}{cpan_modules}{author};
+
+ my $json = from_json($page);
+
+ return map { {
+ name => $_->{name},
+ version => $_->{version},
+ url => 'http://search.cpan.org/dist/' . $_->{name},
+ date => DateTime::Format::Flexible->parse_datetime(
+ $_->{release_date}
+ ), } }
+ map { $_->{_source} }
+ @{ $json->{hits}{hits} };
+
+}
+
+sub index :Path {
+ my ( $self, $c ) = @_;
+
+ my $widget = $c->cache->get( __PACKAGE__ );
+
+ unless ( $widget ) {
+ Template::Declare->init( dispatch_to => [
+ 'Galuga::Controller::Widget::CpanModules::Template' ] );
+
+ $widget = Template::Declare->show( 'widget',
+ $self->distributions($c) );
+
+ $c->cache->set( __PACKAGE__, $widget, '1 day' );
+ }
+
+ $c->response->body( $widget );
+}
+
+__PACKAGE__->meta->make_immutable;
+
+package Galuga::Controller::Widget::CpanModules::Template;
+
+use Template::Declare::Tags;
+use base 'Template::Declare';
+
+template widget => sub {
+ my ( $self, @distributions ) = @_;
+ div {
+ class is "widget cpan_modules";
+ h3 { "CPAN Modules" };
+ show( 'distribution', $_ ) for reverse sort { $a->{date} <=> $b->{date} } @distributions;
+ };
+ script {
+ outs_raw <<'END_JAVASCRIPT';
+var $dist = $('.cpan_modules .distribution:eq(2)')
+ .nextAll()
+ .wrapAll('<div/>')
+ .addClass('cpan_modules_more')
+ .hide()
+ .end();
+
+if( $dist.length > 0 ) {
+ $('<a href="javascript:void(0)">show all</a>')
+ .addClass('more')
+ .appendTo( '.cpan_modules' )
+ .click( function(){
+ $(this).hide();
+ $('.cpan_modules_more').show();
+ });
+}
+END_JAVASCRIPT
+ };
+};
+
+template distribution => sub {
+ my $self = shift;
+ my $dist = shift;
+
+ div {
+ class is 'distribution';
+ div { class is 'name'; a { href is $dist->{url}; $dist->{name} } };
+ div { class is 'version'; $dist->{version}; };
+ div { class is 'date'; $dist->{date}->strftime( '%e %b %Y' ); };
+ }
+
+};
+
+1;
View
@@ -101,3 +101,28 @@ div.widget ul {
padding-left: 20px;
}
+/* widgets */
+
+div.cpan_modules .distribution .version:before {
+ content: "version: ";
+}
+
+div.cpan_modules .distribution .version {
+ width: 45%;
+ display: inline-block;
+}
+
+div.cpan_modules .distribution .date {
+ width: 45%;
+ display: inline-block;
+ text-align: right;
+}
+
+div.cpan_modules .distribution {
+ margin-bottom: 5px;
+}
+
+div.cpan_modules a.more {
+ margin-left: 2em;
+}
+
View
@@ -19,7 +19,7 @@
<& SELF:css_header &>
</style>
<script type="text/javascript"
- src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
+ src="<% $c->uri_for( '/static/jquery/jquery-1.5.1.min.js' ) %>"></script>
<script type="text/javascript"
src="<% $c->uri_for(
View
@@ -1,6 +1,7 @@
<div class="right_column">
-<& /widgets/cpan_author.mason &>
+%# $c->clear_cached_page( '/widget/cpanmodules' );
+<% $c->subreq( '/widget/cpanmodules' ) %>
<& /widgets/ironman.mason &>
Oops, something went wrong.
@@ -0,0 +1,9 @@
+use strict;
+use warnings;
+use Test::More;
+
+BEGIN { use_ok 'Catalyst::Test', 'Galuga' }
+BEGIN { use_ok 'Galuga::Controller::Widget::CpanModules' }
+
+ok( request('/widget/cpanmodules')->is_success, 'Request should succeed' );
+done_testing();

0 comments on commit b6faf1e

Please sign in to comment.