File Coverage

blib/lib/Dancer/Plugin/MPD.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


line stmt bran cond sub pod time code
1             package Dancer::Plugin::MPD;
2              
3 1     1   63352 use warnings;
  1         2  
  1         31  
4 1     1   5 use strict;
  1         3  
  1         106  
5 1     1   211181 use Dancer::Plugin;
  1         314907  
  1         135  
6 1     1   2710 use Audio::MPD;
  0            
  0            
7              
8             our $VERSION = '0.03';
9             my $mpd;
10              
11             register mpd => sub {
12              
13             if ($mpd && $mpd->ping) {
14             return $mpd;
15             } else {
16             return $mpd = _connect();
17             }
18             };
19              
20             sub _connect {
21              
22             my $conf = plugin_setting;
23              
24             # Only pass settings to Audio::MPD if they're specified in the config;
25             # Audio::MPD does sensible things by default, so in the majority of cases
26             # (MPD listening on localhost, on port 6600, with no password set), no
27             # configuration will be needed at all.
28             my %mpd_conf;
29             for my $setting (qw(host port password conntype)) {
30             $mpd_conf{$setting} = $conf->{$setting} if exists $conf->{$setting};
31             }
32              
33             # Audio::MPD's default conntype is 'once', which re-establishes a connection
34             # for every request. For performance, we probably want to re-use a
35             # connection.
36             $mpd_conf{conntype} ||= 'reuse';
37              
38             my $mpd = Audio::MPD->new(\%mpd_conf);
39              
40             Dancer::Factory::Hook->instance->execute_hooks('mpd_connected', $mpd);
41              
42             return $mpd;
43             }
44              
45            
46             Dancer::Factory::Hook->instance->install_hooks(qw(mpd_connected));
47              
48             register_plugin;
49              
50             =head1 NAME
51              
52             Dancer::Plugin::MPD - easy connection to MPD from Dancer apps
53              
54             =head1 DESCRIPTION
55              
56             Provides an easy way to connect to MPD (L<www.musicpd.org>) from a L<Dancer>
57             app. Handles obtaining the connection, making sure the connection is still
58             alive, and reconnecting if not.
59              
60              
61              
62             =head1 SYNOPSIS
63              
64             In your L<Dancer> app, calling the C<mpd> keyword will get you an MPD
65             connection.
66              
67             use Dancer::Plugin::MPD;
68             my $current_song = mpd->current;
69             mpd->next;
70              
71              
72             =head1 CONFIGURATION
73              
74             L<Audio::MPD> does sensible things by default, so in the majority of cases (MPD
75             running on the same host as the app, no password needed, and happy to reuse the
76             connection for performance), no configuration will be required at all.
77              
78             However, the following config settings can be used in your app's C<config.yml>:
79              
80             plugins:
81             MPD:
82             host: localhost
83             port: 6600
84             password: verysecret
85             conntype: reuse
86              
87              
88             =head1 HOOKS
89              
90             =over 4
91              
92             =item * C<mpd_connected>
93              
94             Called when a connection is established, and receives the L<Audio::MPD> object
95             as its parameter.
96              
97             hook 'mpd_connected' => sub {
98             my $mpd = shift;
99             $mpd->repeat(1);
100             $mpd->random(1);
101             $mpd->play;
102             };
103              
104             =back
105              
106             =head1 AUTHOR
107              
108             David Precious, C<< <davidp at preshweb.co.uk> >>
109              
110             =head1 ACKNOWLEDGEMENTS
111              
112             Alan Berndt
113              
114              
115             =head1 BUGS
116              
117             Please report any bugs or feature requests to C<bug-dancer-plugin-mpd at rt.cpan.org>, or through
118             the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Dancer-Plugin-MPD>. I will be notified, and then you'll
119             automatically be notified of progress on your bug as I make changes.
120              
121              
122              
123              
124             =head1 SUPPORT
125              
126             You can find documentation for this module with the perldoc command.
127              
128             perldoc Dancer::Plugin::MPD
129              
130              
131             You can also look for information at:
132              
133             =over 4
134              
135             =item * RT: CPAN's request tracker
136              
137             L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Dancer-Plugin-MPD>
138              
139             =item * AnnoCPAN: Annotated CPAN documentation
140              
141             L<http://annocpan.org/dist/Dancer-Plugin-MPD>
142              
143             =item * CPAN Ratings
144              
145             L<http://cpanratings.perl.org/d/Dancer-Plugin-MPD>
146              
147             =item * Search CPAN
148              
149             L<http://search.cpan.org/dist/Dancer-Plugin-MPD/>
150              
151             =back
152              
153              
154              
155             =head1 LICENSE AND COPYRIGHT
156              
157             Copyright 2010-12 David Precious.
158              
159             This program is free software; you can redistribute it and/or modify it
160             under the terms of either: the GNU General Public License as published
161             by the Free Software Foundation; or the Artistic License.
162              
163             See http://dev.perl.org/licenses/ for more information.
164              
165              
166             =cut
167              
168             1; # End of Dancer::Plugin::MPD