Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 129 lines (91 sloc) 2.659 kB
0240368 Split more things up into traits. Improved some docs too
Oliver Charles authored
1 package Perlanet::Trait::YAMLConfig;
9fa41a6 @davorg Added 'use strict' and 'use warnings' to all modules.
authored
2
3 use strict;
4 use warnings;
5
0240368 Split more things up into traits. Improved some docs too
Oliver Charles authored
6 use Moose::Role;
7 use namespace::autoclean;
8
02e0432 100% POD coverage
Oliver Charles authored
9 =head1 NAME
10
307c9fc @davorg Minor reformatting.
authored
11 Perlanet::Trait::YAMLConfig - configure Perlanet through a YAML configuration
12 file
02e0432 100% POD coverage
Oliver Charles authored
13
14 =head1 SYNOPSIS
15
16 package MyPerlanet;
17 extends 'Perlanet';
18 with 'Perlanet::Traits::YAMLConfig';
19
307c9fc @davorg Minor reformatting.
authored
20 my $perlanet = MyPerlanet->new_with_config(
21 configfile => 'whatever.yml'
22 );
23
62f245f @davorg Documentation typo.
authored
24 $perlanet->run;
02e0432 100% POD coverage
Oliver Charles authored
25
26 =head1 DESCRIPTION
27
307c9fc @davorg Minor reformatting.
authored
28 Allows you to move the configuration of Perlanet to an external YAML
29 configuration file.
02e0432 100% POD coverage
Oliver Charles authored
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
307c9fc @davorg Minor reformatting.
authored
63 The default length of caching, if caching options are present in the
64 configuration
02e0432 100% POD coverage
Oliver Charles authored
65
66 =head2 get_config_from_file
67
68 Extracts the configuration from a YAML file
69
70 =cut
71
0240368 Split more things up into traits. Improved some docs too
Oliver Charles authored
72 with 'MooseX::ConfigFromFile';
73
74 use Carp qw( carp croak );
75 use YAML qw( LoadFile );
76
77 use constant THIRTY_DAYS => 30 * 24 * 60 * 60;
78
79 sub get_config_from_file {
307c9fc @davorg Minor reformatting.
authored
80 my ($self, $file) = @_;
0240368 Split more things up into traits. Improved some docs too
Oliver Charles authored
81
307c9fc @davorg Minor reformatting.
authored
82 open my $cfg_file, '<:utf8', $file
83 or croak "Cannot open file $file: $!";
0240368 Split more things up into traits. Improved some docs too
Oliver Charles authored
84
307c9fc @davorg Minor reformatting.
authored
85 my $cfg = LoadFile($cfg_file);
0240368 Split more things up into traits. Improved some docs too
Oliver Charles authored
86
307c9fc @davorg Minor reformatting.
authored
87 $cfg->{feeds} = [ map {
88 Perlanet::Feed->new($_)
89 } @{ $cfg->{feeds} } ];
0240368 Split more things up into traits. Improved some docs too
Oliver Charles authored
90
307c9fc @davorg Minor reformatting.
authored
91 $cfg->{max_entries} = $cfg->{entries}
92 if $cfg->{entries};
0240368 Split more things up into traits. Improved some docs too
Oliver Charles authored
93
307c9fc @davorg Minor reformatting.
authored
94 if ($cfg->{cache_dir}) {
95 eval { require CHI; };
0240368 Split more things up into traits. Improved some docs too
Oliver Charles authored
96
307c9fc @davorg Minor reformatting.
authored
97 if ($@) {
98 carp "You need to install CHI to enable caching.\n";
99 carp "Caching disabled for this run.\n";
100 delete $cfg->{cache_dir};
0240368 Split more things up into traits. Improved some docs too
Oliver Charles authored
101 }
307c9fc @davorg Minor reformatting.
authored
102 }
0240368 Split more things up into traits. Improved some docs too
Oliver Charles authored
103
307c9fc @davorg Minor reformatting.
authored
104 $cfg->{cache_dir}
105 and $cfg->{cache} = CHI->new(
106 driver => 'File',
107 root_dir => delete $cfg->{cache_dir},
108 expires_in => THIRTY_DAYS,
109 );
0240368 Split more things up into traits. Improved some docs too
Oliver Charles authored
110
307c9fc @davorg Minor reformatting.
authored
111 return $cfg;
0240368 Split more things up into traits. Improved some docs too
Oliver Charles authored
112 }
113
02e0432 100% POD coverage
Oliver Charles authored
114 =head1 AUTHOR
115
116 Oliver Charles, <oliver.g.charles@googlemail.com>
117
118 =head1 COPYRIGHT AND LICENSE
119
307c9fc @davorg Minor reformatting.
authored
120 Copyright (c) 2010 by Magnum Solutions Ltd.
02e0432 100% POD coverage
Oliver Charles authored
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
0240368 Split more things up into traits. Improved some docs too
Oliver Charles authored
128 1;
Something went wrong with that request. Please try again.