File Coverage

blib/lib/Perlanet/Trait/YAMLConfig.pm
Criterion Covered Total %
statement 36 39 92.3
branch 9 10 90.0
condition n/a
subroutine 9 9 100.0
pod 1 1 100.0
total 55 59 93.2


line stmt bran cond sub pod time code
1             package Perlanet::Trait::YAMLConfig;
2              
3 6     6   2291 use strict;
  6         10  
  6         146  
4 6     6   19 use warnings;
  6         8  
  6         111  
5              
6 6     6   46 use 5.6.0;
  6         13  
7             our $VERSION = '0.58';
8              
9 6     6   20 use Moose::Role;
  6         6  
  6         33  
10 6     6   19330 use namespace::autoclean;
  6         6  
  6         36  
11              
12             =head1 NAME
13              
14             Perlanet::Trait::YAMLConfig - configure Perlanet through a YAML configuration
15             file
16              
17             =head1 SYNOPSIS
18              
19             package MyPerlanet;
20             extends 'Perlanet';
21             with 'Perlanet::Traits::YAMLConfig';
22              
23             my $perlanet = MyPerlanet->new_with_config(
24             configfile => 'whatever.yml'
25             );
26              
27             $perlanet->run;
28              
29             =head1 DESCRIPTION
30              
31             Allows you to move the configuration of Perlanet to an external YAML
32             configuration file.
33              
34             =head2 Example Configuration File
35              
36             title: planet test
37             description: A Test Planet
38             url: http://planet.example.com/
39             author:
40             name: Dave Cross
41             email: dave@dave.org.uk
42             entries: 20
43             opml: opml.xml
44             page:
45             file: index.html
46             template: index.tt
47             feed:
48             file: atom.xml
49             format: Atom
50             cache_dir: /tmp/feeds
51             feeds:
52             - url: http://blog.dave.org.uk/atom.xml
53             title: Dave's Blog
54             web: http://blog.dave.org.uk/
55             - url: http://use.perl.org/~davorg/journal/rss
56             title: Dave's use.perl Journal
57             web: http://use.perl.org/~davorg/journal/
58             - url: http://www.oreillynet.com/pub/feed/31?au=2607
59             title: Dave on O'Reillynet
60             web: http://www.oreillynet.com/pub/au/2607
61              
62             =head1 METHODS
63              
64             =head2 THIRTY_DAYS
65              
66             The default length of caching, if caching options are present in the
67             configuration
68              
69             =head2 get_config_from_file
70              
71             Extracts the configuration from a YAML file
72              
73             =cut
74              
75             with 'MooseX::ConfigFromFile';
76              
77 6     6   397 use Carp qw( carp croak );
  6         8  
  6         277  
78 6     6   23 use YAML qw( LoadFile );
  6         6  
  6         299  
79              
80 6     6   24 use constant THIRTY_DAYS => 30 * 24 * 60 * 60;
  6         11  
  6         1424  
81              
82             sub get_config_from_file {
83 5     5 1 50108 my ($self, $file) = @_;
84              
85 5 100       473 open my $cfg_file, '<:utf8', $file
86             or croak "Cannot open file $file: $!";
87              
88 4         24 my $cfg = LoadFile($cfg_file);
89              
90             $cfg->{feeds} = [ map {
91 5         189 Perlanet::Feed->new($_)
92 4         64582 } @{ $cfg->{feeds} } ];
  4         17  
93              
94             $cfg->{max_entries} = $cfg->{entries}
95 4 100       23 if $cfg->{entries};
96              
97 4 100       15 if ($cfg->{cache_dir}) {
98 1         3 eval { require CHI; };
  1         9  
99              
100 1 50       85 if ($@) {
101 0         0 carp "You need to install CHI to enable caching.\n";
102 0         0 carp "Caching disabled for this run.\n";
103 0         0 delete $cfg->{cache_dir};
104             }
105             }
106              
107             $cfg->{cache_dir}
108             and $cfg->{cache} = CHI->new(
109             driver => 'File',
110             root_dir => delete $cfg->{cache_dir},
111 4 100       20 expires_in => THIRTY_DAYS,
112             );
113              
114 4         85142 return $cfg;
115             }
116              
117             =head1 AUTHOR
118              
119             Oliver Charles, <oliver.g.charles@googlemail.com>
120              
121             =head1 COPYRIGHT AND LICENSE
122              
123             Copyright (c) 2010 by Magnum Solutions Ltd.
124              
125             This library is free software; you can redistribute it and/or modify
126             it under the same terms as Perl itself, either Perl version 5.10.0 or,
127             at your option, any later version of Perl 5 you may have available.
128              
129             =cut
130              
131             1;