File Coverage

blib/lib/Perlanet/Trait/YAMLConfig.pm
Criterion Covered Total %
statement 21 37 56.7
branch 0 10 0.0
condition n/a
subroutine 7 8 87.5
pod 1 1 100.0
total 29 56 51.7


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