File Coverage

blib/lib/Egg/View/FeedPP.pm
Criterion Covered Total %
statement 24 60 40.0
branch 0 10 0.0
condition 0 24 0.0
subroutine 8 16 50.0
pod n/a
total 32 110 29.0


line stmt bran cond sub pod time code
1             package Egg::View::FeedPP;
2             #
3             # Masatoshi Mizuno E<lt>lusheE<64>cpan.orgE<gt>
4             #
5             # $Id: FeedPP.pm 187 2007-08-07 19:00:30Z lushe $
6             #
7 2     2   32217 use strict;
  2         6  
  2         114  
8 2     2   13 use warnings;
  2         4  
  2         80  
9 2     2   13 use Carp qw/croak/;
  2         12  
  2         161  
10 2     2   2935 use UNIVERSAL::require;
  2         8305  
  2         23  
11 2     2   67 use base qw/Egg::View/;
  2         4  
  2         1128  
12 2     2   1216 use XML::FeedPP;
  2         36541  
  2         22  
13              
14             our $VERSION = '0.01';
15              
16             =head1 NAME
17              
18             Egg::View::FeedPP - XML::FeedPP for Egg::View.
19              
20             =head1 SYNOPSIS
21              
22             configuration.
23              
24             ...
25             VIEW=> [
26             [ FeedPP=> {
27             content_type => 'application/rss+xml',
28             charset => 'UTF-8',
29             } ],
30             ],
31              
32             example code.
33              
34             my $view= $e->view('FeedPP');
35             my $feed= $view->feed;
36             $feed->title('MY BLOG');
37             $feed->link('http://myblog.domain.name/');
38             for my $item (@items) {
39             $feed->add_item( $item->{url},
40             title => $item->{title},
41             description => $item->{description},
42             );
43             }
44             print $view->render;
45              
46             =head1 DESCRIPTION
47              
48             It is a module to use XML::FeedPP with VIEW.
49              
50             I think that you should operate the XML::FeedPP object directly from the
51             feed method though some methods of XML::FeedPP do bind.
52              
53             It has the function to cache feeding that XML::FeedPP generated.
54             * Egg::Plugin::Cache is used.
55              
56             =head1 CONFIGURATION
57              
58             'FeedPP' is added to the setting of VIEW.
59              
60             VIEW=> [ [ FeedPP=> { ... option ... } ] ],
61              
62             =head2 content_type
63              
64             Contents type when outputting it.
65              
66             Default is 'application/rss+xml'.
67              
68             =head2 charset
69              
70             Character set when outputting it.
71              
72             Default is 'UTF-8'.
73              
74             =head2 cache_name
75              
76             If the cash function is used, the cash name to use Egg::Plugin::Cache is set.
77              
78             * Even if cash is used, it is not indispensable because the thing specified
79             when the cash function is called can be done.
80              
81             see L<Egg::Plugin::Cache>.
82              
83             =head1 METHODS
84              
85             =cut
86              
87             __PACKAGE__->mk_accessors(qw/content_type charset/);
88              
89             {
90 2     2   151 no strict 'refs'; ## no critic.
  2         7  
  2         65  
91 2     2   12 no warnings 'redefine';
  2         4  
  2         1788  
92             for my $accessor (qw/title pubDate link add_item merge
93             remove_item clear_item sort_item uniq_item limit_item normalize/) {
94             *{__PACKAGE__."::$accessor"}= sub { shift->feed->$accessor(@_) };
95             }
96             *item= \&add_item;
97             };
98              
99             sub _setup {
100 0     0     my($class, $e, $conf)= @_;
101 0   0       $conf->{content_type} ||= 'application/rss+xml';
102 0   0       $conf->{charset} ||= 'UTF-8';
103             }
104              
105             =head2 new
106              
107             The object of this View is returned.
108              
109             my $view= $e->view('FeedPP');
110              
111             =cut
112             sub new {
113 0     0     my $view= shift->SUPER::new(@_);
114 0           $view->{content_type}= $view->config->{content_type};
115 0           $view->{charset} = $view->config->{charset};
116 0           $view;
117             }
118              
119             =head2 cache ( [CACHE_KEY] or [CACHE_NAME], [CACHE_KEY] {, [EXPIRES] } )
120              
121             The cash function is made effective.
122              
123             * Thing that can be used by Egg::Plugin::Cache's being appropriately set.
124              
125             CACHE_KEY is only passed if 'cache_name' is set with CONFIGURATION.
126              
127             When CACHE_NAME is passed, the cash is used.
128             * It gives priority more than set of 'cache_name'.
129              
130             EXPIRES is validity term to pass it to the set method when L<Cache::Memcached>
131             is used with L<Egg::Plugin::Cache>.
132             * It is not necessary usually.
133              
134             my $expr= 60* 60; # one hour.
135             my $view= $e->view('FeedPP');
136             unless ($view->cache('CACHE_NAME', 'CACHE_KEY', $expr)) {
137             #
138             # RSS feed generation code.
139             #
140             my $feed= $view->feed;
141             $feed->title('MYBLOG');
142             ....
143             ....
144             # ----------------
145             }
146              
147             * 'RSS feed generation code' part from the unless sentence though the image
148             might not be gripped easily for a moment You only have to move as usual
149             even if it removes.
150              
151             Anything need not be done excluding this.
152             If cash becomes a hit, the content of cash is output with output.
153              
154             =cut
155             sub cache {
156 0     0     my $view= shift;
157 0   0       my $name= shift || croak q{ I want cache name. };
158 0   0       my $ckey= shift || do {
159             my $tmp= $name;
160             $name= $view->config->{cache_name}
161             || croak q{ I want setup 'cache_name'. };
162             $tmp;
163             };
164 0   0       $view->{cache}= { name=> $name, key=> $ckey, expir=> (shift || undef) };
165 0 0         $view->{cache}{hit}= $view->e->cache($name)->get($ckey) ? 1: 0;
166             }
167              
168             =head2 feed_type ( [FEED_TYPE] )
169              
170             The type of the generation feeding to use it by the feed method is returned.
171              
172             The value that can be specified for FEED_TYPE is rss, rdf, and atom.
173              
174             FEED_TYPE returns and 'RSS' always returns when it is not given or wrong
175             specification is done.
176              
177             # Example of generating key to cash with value obtained from URI.
178             # example uri = http://domain/xml/hoge/rdf
179             #
180             my $feed_type= $view->feed_type( $e->snip->[2] );
181             $view->cache('FileCache', "xml_hoge_${feed_type}") || do {
182             my $feed= $view->feed($feed_type);
183             .....
184             ...
185             };
186              
187             =cut
188             sub feed_type {
189 0     0     my $view= shift;
190 0 0         { rss=> 'RSS', rdf=> 'RDF', atom=> 'Atom' }->{lc(shift)} || 'RSS';
191             }
192              
193             =head2 feed ( [FEED_TYPE] )
194              
195             The XML::FeedPP object is returned.
196              
197             When FEED_TYPE is specified, the corresponding module is read.
198              
199             If FEED_TYPE unspecifies it, 'RSS' is processed to have specified it.
200              
201             my $feed= $view->feed('Atom');
202              
203             see L<XML::FeedPP>.
204              
205             =cut
206             sub feed {
207 0   0 0     $_[0]->{feed} ||= do {
208 0           my $view= shift;
209 0           my $pkg = "XML::FeedPP::". $view->feed_type(shift);
210 0           $view->e->debug_out("# + view-FeedPP : $pkg" );
211 0           $pkg->new(@_);
212             };
213             }
214              
215             =head2 reset
216              
217             The XML::FeedPP context set in View is erased.
218              
219             =cut
220 0 0   0     sub reset { undef($_[0]->{feed}) if $_[0]->{feed} }
221              
222             =head2 render
223              
224             To_string of XML::FeedPP is called and feeding is generated.
225              
226             If cash is effective, the content of cash is returned.
227              
228             my $feed_text= $view->render($feed);
229              
230             =cut
231             sub render {
232 0     0     my $view= shift;
233 0 0         if (my $cc= $view->{cache}) {
234 0           my $cache= $view->e->cache($cc->{name});
235 0 0         $cc->{hit} ? $cache->get($cc->{key}): do {
236 0           $view->e->debug_out("# + view-FeedPP cache hit : No." );
237 0   0       my $feed= shift || $view->feed(@_);
238 0           my $body= $feed->to_string($view->{charset});
239 0           $cache->set($cc->{key}, $body, $cc->{expir});
240 0           $body;
241             };
242             } else {
243 0   0       my $feed= shift || $view->feed(@_);
244 0           $feed->to_string($view->{charset});
245             }
246             }
247              
248             =head2 output
249              
250             The result of the render method is received, and set contents type and
251             contents body are set in L<Egg::Response>.
252              
253             $view->output($feed);
254              
255             * It is not necessary to call it from the project code because it is called
256             by the operation of Egg usually.
257              
258             =cut
259             sub output {
260 0     0     my $view= shift;
261 0   0       my $feed= shift || $view->feed;
262 0           $view->e->response->content_type
263             ("$view->{content_type}; charset=$view->{charset}");
264 0           my $body= $view->render($feed, @_);
265 0           $view->e->response->body(\$body);
266             }
267              
268             =head1 BINDING METHODS
269              
270             Tentatively, Messod of following XML::FeedPP that seems to be necessary for
271             the feeding generation it is done and bind is done to this View.
272              
273             =over 4
274              
275             =item * item, title, pubDate, link, add_item, merge, remove_item, clear_item, sort_item, uniq_item, limit_item, normalize
276              
277             =back
278              
279             =head1 SEE ALSO
280              
281             L<XML::FeedPP>,
282             L<Egg::Plugin::Cache>,
283             L<Egg::Release>,
284              
285             =head1 AUTHOR
286              
287             Masatoshi Mizuno E<lt>lusheE<64>cpan.orgE<gt>
288              
289             =head1 COPYRIGHT
290              
291             Copyright (C) 2007 by Bee Flag, Corp. E<lt>http://egg.bomcity.com/E<gt>, All Rights Reserved.
292              
293             This library is free software; you can redistribute it and/or modify
294             it under the same terms as Perl itself, either Perl version 5.8.6 or,
295             at your option, any later version of Perl 5 you may have available.
296              
297             =cut
298              
299             1;