/[Grep]/lib/Grep/Action/Fetch.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /lib/Grep/Action/Fetch.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 89 - (hide annotations)
Fri Feb 23 21:54:39 2007 UTC (17 years, 3 months ago) by dpavlin
File size: 2054 byte(s)
report feed name in error message
1 dpavlin 3 use strict;
2     use warnings;
3    
4     =head1 NAME
5    
6     Grep::Action::Fetch
7    
8     =cut
9    
10     package Grep::Action::Fetch;
11     use base qw/Grep::Action Jifty::Action/;
12    
13     use XML::Feed;
14 dpavlin 21 use LWP::UserAgent;
15 dpavlin 3
16 dpavlin 47 use Grep::Search;
17 dpavlin 72 use Grep::Source;
18 dpavlin 47
19 dpavlin 3 use Data::Dump qw/dump/;
20    
21     use Jifty::Param::Schema;
22     use Jifty::Action schema {
23    
24     param q =>
25     type is 'text',
26     label is 'Search for',
27     hint is 'enter few words to search for';
28    
29     param feed =>
30     label is 'From feed',
31 dpavlin 7 render as 'select',
32 dpavlin 3 available are defer {
33     my $feeds = Grep::Model::FeedCollection->new;
34     $feeds->order_by({ column => 'title', order => 'ASC' });
35     $feeds->unlimit;
36     warn "feeds ", $feeds->build_select_query;
37     [{
38     display_from => 'title',
39     value_from => 'id',
40     collection => $feeds,
41     }];
42     };
43    
44 dpavlin 25 param item_fragment =>
45     label is 'Show',
46     render as 'select',
47 dpavlin 28 # valid are qw/result result_short/,
48     # available are qw/result result_short/;
49     available are qw/long short title/;
50 dpavlin 25
51 dpavlin 3 };
52    
53     =head2 take_action
54    
55 dpavlin 29 Returns C<Grep::Model::ItemCollection> of fatched items from Feed which will
56     also be stored in local cache.
57    
58 dpavlin 3 =cut
59    
60     sub take_action {
61     my $self = shift;
62    
63     # Custom action code
64    
65 dpavlin 72 my $feed_id = $self->argument_value('feed') or die "no feed?";
66     my $q = $self->argument_value('q') or die "no q?";
67    
68 dpavlin 3 my $feed = Grep::Model::Feed->new();
69     $feed->load_by_cols( id => $feed_id );
70 dpavlin 72 die "can't load feed ", $feed_id unless ($feed->id);
71 dpavlin 3
72 dpavlin 72 my $source = Grep::Source->new({ feed => $feed });
73     my $items = $source->search( $q );
74     my $new = $source->new_items;
75 dpavlin 3
76 dpavlin 71 if ( my $count = $items->count ) {
77    
78     # construct a proper sentence :-)
79     my $message = $count
80     . ( $new == $count ? ' new' : '' )
81     . ( $new == 0 ? ' old' : '' )
82     . ' results '
83     . ( $new && $new < $count ? "of which $new new " : '' )
84 dpavlin 72 . "for '$q' in " . $source->feed->title;
85 dpavlin 71
86     $self->result->message( $message );
87    
88     $self->result->content( items => $items );
89     $self->result->content( count => $items->count );
90    
91     return $items;
92    
93     } else {
94 dpavlin 89 return $self->result->error( "No results for '$q' in " . $feed->title );
95 dpavlin 71 }
96     }
97    
98    
99 dpavlin 3 1;
100    

  ViewVC Help
Powered by ViewVC 1.1.26