File Coverage

blib/lib/Mojolicious/Plugin/SMS.pm
Criterion Covered Total %
statement 17 18 94.4
branch 3 4 75.0
condition 2 5 40.0
subroutine 4 4 100.0
pod 1 1 100.0
total 27 32 84.3


line stmt bran cond sub pod time code
1             package Mojolicious::Plugin::SMS;
2 1     1   830 use Mojo::Base 'Mojolicious::Plugin';
  1         3  
  1         8  
3 1     1   755 use SMS::Send;
  1         14651  
  1         202  
4              
5             our $VERSION = '0.02';
6              
7             sub register {
8 1     1 1 47 my ($self, $app, $conf) = @_;
9 1   50     4 $conf ||= {};
10 1         2 $conf->{driver} = delete $conf->{driver};
11 1         5 my $sms_send = SMS::Send->new($conf->{driver}, %$conf);
12              
13             $app->helper(
14             sms => sub {
15 2     2   28978 my $self = shift;
16 2         4 my %params;
17 2 100 33     19 if (@_ == 2) {
    50          
18 1         6 @params{qw(to text)} = @_;
19             } elsif (@_ > 2 && @_ % 2 == 0) {
20 1         8 %params = @_;
21             } else {
22 0         0 die "Invalid params passed to sms helper!";
23             }
24 2         15 return $sms_send->send_sms(%params);
25             }
26 1         907 );
27             }
28              
29             1;
30              
31             =head1 NAME
32              
33             Mojolicious::Plugin::SMS - Easy SMS sending from Mojolicious apps
34              
35             =head1 SYNOPSIS
36              
37             # Mojolicious::Lite
38             plugin 'SMS' => {
39             driver => 'Test'
40             };
41              
42             # Mojolicious
43             $self->plugin(SMS => {
44             driver => 'Nexmo',
45             _username => 'testuser',
46             _password => 'testpassword'
47             _from => 'Bender'
48             });
49              
50             # in controller named params
51             $self->sms(
52             to => '+380506022375',
53             text => 'use Perl or die;'
54             );
55              
56             # in controller positional params
57             $self->sms('+380506022375', 'use Perl or die;');
58              
59             =head1 DESCRIPTION
60              
61             Provides a quick and easy way to send SMS messages using L drivers
62             (of which there are many, so chances are the service you want to use is already
63             supported; if not, they're easy to write, and if you want to change providers
64             later, you can simply update a few lines in your config file, and you're done.
65              
66             =head1 OPTIONS
67              
68             L has one required option 'driver', all other options
69             are passed to appropriate L driver.
70              
71             =head2 C
72              
73             L driver name. This is a required option. You may specify 'Test' if
74             you need a testing driver.
75              
76             =head1 HELPERS
77              
78             L implements one helper.
79              
80             =head2 sms
81              
82             Send an SMS message. You can pass the destination and message as positional
83             params:
84              
85             sms $to, $message;
86              
87             Or, you can use named params:
88              
89             sms to => $to, text => $message;
90              
91             The latter form may be clearer, and would allow any additional driver-specific
92             parameters to be passed too, but the former is terser. The choice is yours.
93              
94             =head1 METHODS
95              
96             L inherits all methods from L
97             and implements the following new ones.
98              
99             =head2 C
100              
101             $plugin->register;
102              
103             Register plugin hooks and helpers in L application.
104              
105             =head1 SEE ALSO
106              
107             L, L, L.
108              
109             =head1 AUTHOR
110              
111             Yuriy Syrota
112              
113             =head1 COPYRIGHT & LICENSE
114              
115             Copyright (C) 2011 by Yuriy Syrota.
116              
117             This program is free software; you can redistribute it and/or modify it
118             under the same terms as Perl itself.
119              
120             =cut