Permalink
Browse files

add on_click event

  • Loading branch information...
1 parent 62e23cb commit a1d3c1f15901bb74a80ee1e61f69c5e19cf9a93b @hideo55 committed Jul 26, 2011
Showing with 69 additions and 54 deletions.
  1. +69 −54 metacpan-growler.pl
View
@@ -1,22 +1,23 @@
-#!/usr/bin/env perl
+#!/usr/bin/perl
use strict;
use warnings;
use Cocoa::Growl;
+use Cocoa::EventLoop;
use AnyEvent;
use AnyEvent::HTTP;
use JSON;
use Data::MessagePack;
use Cache::LRU;
use Encode;
-our $VERSION = '0.01';
+our $VERSION = '0.02';
my $app_name = 'MetaCPAN Growler';
my $app_domain = 'org.github.hideo55.metacpangrowler';
-my $uri = 'http://api.metacpan.org/v0/release/_search';
-my $post_data = JSON::encode_json(
+my $search_uri = 'http://api.metacpan.org/v0/release/_search';
+my $post_data = JSON::encode_json(
{ 'size' => 20,
'from' => 0,
'sort' => [ { 'date' => { 'order' => 'desc', }, }, ],
@@ -29,8 +30,8 @@
Cocoa::Growl::growl_register(
app => $app_name,
icon => 'http://metacpan.org/favicon.ico',
- notifications => [ 'Release new module', 'Fatal Error', 'Error' ],
- defaults => [ 'Release new module', 'Fatal Error' ],
+ notifications => [ 'Update', 'Error' ],
+ defaults => [ 'Update', 'Error' ],
);
my %options = ( interval => 300, maxGrowls => 10, cacheSize => 100, );
@@ -54,54 +55,68 @@
sub get_metacpan_info {
my $max_growls = shift;
- http_post $uri, $post_data, sub {
- my $mod_info
- = $_[1]->{Status} == 200
- ? eval { JSON::decode_json( $_[0] ) }
- : undef;
-
- unless ($mod_info) {
-
- Cocoa::Growl::growl_notify(
- name => 'Error',
- title => $app_name,
- description => "Can't parse the metacpan response.",
- );
- return;
- }
-
- my @to_growl;
- for my $entry ( @{ $mod_info->{hits}{hits} } ) {
- my $id = $entry->{fields}{id};
- next if $Seen{$id}++;
- next
- if @to_growl >= $max_growls; # not last, so that we can cache them in %Seen
- push @to_growl, $entry;
- }
-
- for my $entry (@to_growl) {
- my $author_id = $entry->{fields}{author};
- get_author(
- $author_id,
- sub {
- my $author = shift;
- $author->{name} ||= $author_id;
- my $title = $author->{name};
- my $description = $entry->{fields}{name};
- $description .= ' : ' . $entry->{fields}{abstract}
- if $entry->{fields}{abstract};
- my $icon = $author->{avatar} ? "$author->{avatar}" : q{};
-
- Cocoa::Growl::growl_notify(
- name => 'Release new module',
- title => encode_utf8($title),
- description => encode_utf8($description),
- icon => $author->{avatar},
- );
- }
- );
- }
- };
+ for my $uri ($search_uri) {
+
+ http_post $uri, $post_data,
+ headers => {},
+ persistent => 0,
+ sub {
+ my $mod_info
+ = $_[1]->{Status} == 200
+ ? eval { JSON::decode_json( $_[0] ) }
+ : undef;
+
+ unless ($mod_info) {
+
+ Cocoa::Growl::growl_notify(
+ name => 'Error',
+ title => $app_name,
+ description => "Can't parse the metacpan response.",
+ );
+ return;
+ }
+
+ my @to_growl;
+ for my $entry ( @{ $mod_info->{hits}{hits} } ) {
+ my $id = $entry->{fields}{id};
+ next if $Seen{$id}++;
+ next
+ if @to_growl >= $max_growls
+ ; # not last, so that we can cache them in %Seen
+ push @to_growl, $entry;
+ }
+
+ for my $entry (@to_growl) {
+ my $author_id = $entry->{fields}{author};
+ get_author(
+ $author_id,
+ sub {
+ my $author = shift;
+ $author->{name} ||= $author_id;
+ my $title = $author->{name};
+ my $name = $entry->{fields}{name};
+ my $description = $name;
+ $description = ' : ' . $entry->{fields}{abstract}
+ if $entry->{fields}{abstract};
+ my $icon
+ = $author->{avatar} ? "$author->{avatar}" : q{};
+
+ Cocoa::Growl::growl_notify(
+ name => 'Update',
+ title => encode_utf8($title),
+ description => encode_utf8($description),
+ icon => $author->{avatar},
+ on_click => sub {
+ my $link
+ = "http://metacpan.org/release/${author_id}/${name}";
+ system( "open", $link );
+ },
+ );
+ }
+ );
+ }
+ };
+ }
}
sub get_preferences {

0 comments on commit a1d3c1f

Please sign in to comment.