| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Nexmo::SMS; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 8 |  |  | 8 |  | 19685 | use warnings; | 
|  | 8 |  |  |  |  | 14 |  | 
|  | 8 |  |  |  |  | 234 |  | 
| 4 | 8 |  |  | 8 |  | 34 | use strict; | 
|  | 8 |  |  |  |  | 10 |  | 
|  | 8 |  |  |  |  | 319 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 | 8 |  |  | 8 |  | 2803 | use Nexmo::SMS::BinaryMessage; | 
|  | 8 |  |  |  |  | 11 |  | 
|  | 8 |  |  |  |  | 290 |  | 
| 7 | 8 |  |  | 8 |  | 3052 | use Nexmo::SMS::TextMessage; | 
|  | 8 |  |  |  |  | 14 |  | 
|  | 8 |  |  |  |  | 184 |  | 
| 8 | 8 |  |  | 8 |  | 2857 | use Nexmo::SMS::WAPPushMessage; | 
|  | 8 |  |  |  |  | 14 |  | 
|  | 8 |  |  |  |  | 194 |  | 
| 9 |  |  |  |  |  |  |  | 
| 10 | 8 |  |  | 8 |  | 2619 | use Nexmo::SMS::GetBalance; | 
|  | 8 |  |  |  |  | 14 |  | 
|  | 8 |  |  |  |  | 449 |  | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | # ABSTRACT: Module for the Nexmo SMS API! | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  | our $VERSION = '0.07'; | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  | This module simplifies sending SMS through the Nexmo API. | 
| 20 |  |  |  |  |  |  |  | 
| 21 |  |  |  |  |  |  |  | 
| 22 |  |  |  |  |  |  | use Nexmo::SMS; | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | my $nexmo = Nexmo::SMS->new( | 
| 25 |  |  |  |  |  |  | server   => 'http://rest.nexmo.com/sms/json', | 
| 26 |  |  |  |  |  |  | username => 'testuser1', | 
| 27 |  |  |  |  |  |  | password => 'testpasswd2', | 
| 28 |  |  |  |  |  |  | ); | 
| 29 |  |  |  |  |  |  |  | 
| 30 |  |  |  |  |  |  | my $sms = $nexmo->sms( | 
| 31 |  |  |  |  |  |  | text     => 'This is a test', | 
| 32 |  |  |  |  |  |  | from     => 'Test02', | 
| 33 |  |  |  |  |  |  | to       => '452312432', | 
| 34 |  |  |  |  |  |  | ) or die $nexmo->errstr; | 
| 35 |  |  |  |  |  |  |  | 
| 36 |  |  |  |  |  |  | my $response = $sms->send || die $sms->errstr; | 
| 37 |  |  |  |  |  |  |  | 
| 38 |  |  |  |  |  |  | if ( $response->is_success ) { | 
| 39 |  |  |  |  |  |  | print "SMS was sent...\n"; | 
| 40 |  |  |  |  |  |  | } | 
| 41 |  |  |  |  |  |  |  | 
| 42 |  |  |  |  |  |  | =head1 METHODS | 
| 43 |  |  |  |  |  |  |  | 
| 44 |  |  |  |  |  |  | =head2 new | 
| 45 |  |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  | create a new object | 
| 47 |  |  |  |  |  |  |  | 
| 48 |  |  |  |  |  |  | my $foo = Nexmo::SMS->new( | 
| 49 |  |  |  |  |  |  | server   => 'http://rest.nexmo.com/sms/json', | 
| 50 |  |  |  |  |  |  | username => 'testuser1', | 
| 51 |  |  |  |  |  |  | password => 'testpasswd2', | 
| 52 |  |  |  |  |  |  | ); | 
| 53 |  |  |  |  |  |  |  | 
| 54 |  |  |  |  |  |  | Those parameters are optional and they are used as defaults for the message objects | 
| 55 |  |  |  |  |  |  |  | 
| 56 |  |  |  |  |  |  | =cut | 
| 57 |  |  |  |  |  |  |  | 
| 58 |  |  |  |  |  |  | my @attrs = qw(server username password);; | 
| 59 |  |  |  |  |  |  |  | 
| 60 |  |  |  |  |  |  | for my $attr ( @attrs ) { | 
| 61 | 8 |  |  | 8 |  | 37 | no strict 'refs'; | 
|  | 8 |  |  |  |  | 10 |  | 
|  | 8 |  |  |  |  | 3547 |  | 
| 62 |  |  |  |  |  |  | *{ __PACKAGE__ . '::' . $attr } = sub { | 
| 63 | 43 |  |  | 43 |  | 52 | my ($self,$value) = @_; | 
| 64 |  |  |  |  |  |  |  | 
| 65 | 43 |  |  |  |  | 354 | my $key = '__' . $attr . '__'; | 
| 66 | 43 | 100 |  |  |  | 183 | $self->{$key} = $value if @_ == 2; | 
| 67 | 43 |  |  |  |  | 106 | return $self->{$key}; | 
| 68 |  |  |  |  |  |  | }; | 
| 69 |  |  |  |  |  |  | } | 
| 70 |  |  |  |  |  |  |  | 
| 71 |  |  |  |  |  |  | sub new { | 
| 72 | 7 |  |  | 7 | 1 | 105 | my ($class,%param) = @_; | 
| 73 |  |  |  |  |  |  |  | 
| 74 | 7 |  |  |  |  | 22 | my $self = bless {}, $class; | 
| 75 |  |  |  |  |  |  |  | 
| 76 | 7 |  | 100 |  |  | 38 | $param{server} ||= 'http://rest.nexmo.com/sms/json'; | 
| 77 |  |  |  |  |  |  |  | 
| 78 | 7 |  |  |  |  | 20 | for my $attr ( @attrs ) { | 
| 79 | 21 | 100 |  |  |  | 61 | if ( exists $param{$attr} ) { | 
| 80 | 19 |  |  |  |  | 60 | $self->$attr( $param{$attr} ); | 
| 81 |  |  |  |  |  |  | } | 
| 82 |  |  |  |  |  |  | } | 
| 83 |  |  |  |  |  |  |  | 
| 84 | 7 |  |  |  |  | 27 | return $self; | 
| 85 |  |  |  |  |  |  | } | 
| 86 |  |  |  |  |  |  |  | 
| 87 |  |  |  |  |  |  | =head2 sms | 
| 88 |  |  |  |  |  |  |  | 
| 89 |  |  |  |  |  |  | Create a new message object or returns C. | 
| 90 |  |  |  |  |  |  |  | 
| 91 |  |  |  |  |  |  | my $sms = $nexmo->sms( | 
| 92 |  |  |  |  |  |  | text     => 'This is a test', | 
| 93 |  |  |  |  |  |  | from     => 'Test02', | 
| 94 |  |  |  |  |  |  | to       => '452312432', | 
| 95 |  |  |  |  |  |  | ) or die $nexmo->errstr; | 
| 96 |  |  |  |  |  |  |  | 
| 97 |  |  |  |  |  |  | Send a binary message | 
| 98 |  |  |  |  |  |  |  | 
| 99 |  |  |  |  |  |  | my $binary = $nexmo->sms( | 
| 100 |  |  |  |  |  |  | type     => 'binary', | 
| 101 |  |  |  |  |  |  | udh      => '06050415811581',   # hex encoded udh | 
| 102 |  |  |  |  |  |  | body     => '0011223344556677', # hex encoded body | 
| 103 |  |  |  |  |  |  | from     => 'Test02', | 
| 104 |  |  |  |  |  |  | to       => '452312432', | 
| 105 |  |  |  |  |  |  | ) or die $nexmo->errstr; | 
| 106 |  |  |  |  |  |  |  | 
| 107 |  |  |  |  |  |  | =cut | 
| 108 |  |  |  |  |  |  |  | 
| 109 |  |  |  |  |  |  | sub sms { | 
| 110 | 8 |  |  | 8 | 1 | 5477 | my ($self,%param) = @_; | 
| 111 |  |  |  |  |  |  |  | 
| 112 | 8 |  |  |  |  | 42 | my %types = ( | 
| 113 |  |  |  |  |  |  | text    => 'Nexmo::SMS::TextMessage', | 
| 114 |  |  |  |  |  |  | unicode => 'Nexmo::SMS::TextMessage', | 
| 115 |  |  |  |  |  |  | binary  => 'Nexmo::SMS::BinaryMessage', | 
| 116 |  |  |  |  |  |  | wappush => 'Nexmo::SMS::WAPPushMessage', | 
| 117 |  |  |  |  |  |  | ); | 
| 118 |  |  |  |  |  |  |  | 
| 119 | 8 |  |  |  |  | 16 | my $requested_type = $param{type}; | 
| 120 | 8 | 50 | 66 |  |  | 54 | if ( exists $param{type} and !$types{$requested_type} ) { | 
| 121 | 0 |  |  |  |  | 0 | $self->errstr("Type $requested_type not supported (yet)!"); | 
| 122 | 0 |  |  |  |  | 0 | return; | 
| 123 |  |  |  |  |  |  | } | 
| 124 |  |  |  |  |  |  |  | 
| 125 | 8 |  | 100 |  |  | 41 | my $type   = $requested_type || 'text'; | 
| 126 | 8 |  |  |  |  | 14 | my $module = $types{$type}; | 
| 127 |  |  |  |  |  |  |  | 
| 128 | 8 | 100 |  |  |  | 28 | $param{type} = $type if $type ne 'text'; | 
| 129 |  |  |  |  |  |  |  | 
| 130 |  |  |  |  |  |  | # check for needed params | 
| 131 | 8 |  |  |  |  | 14 | my $sub_name  = 'check_needed_params'; | 
| 132 | 8 |  |  |  |  | 94 | my $check_sub = $module->can( $sub_name ); | 
| 133 | 8 | 50 |  |  |  | 28 | if ( !$check_sub ) { | 
| 134 | 0 |  |  |  |  | 0 | $self->errstr("$module does not know about sub $sub_name"); | 
| 135 | 0 |  |  |  |  | 0 | return; | 
| 136 |  |  |  |  |  |  | } | 
| 137 |  |  |  |  |  |  |  | 
| 138 | 8 |  | 66 |  |  | 39 | $param{server}   ||= $self->server; | 
| 139 | 8 |  | 66 |  |  | 34 | $param{username} ||= $self->username; | 
| 140 | 8 |  | 66 |  |  | 31 | $param{password} ||= $self->password; | 
| 141 |  |  |  |  |  |  |  | 
| 142 | 8 |  |  |  |  | 54 | my $params_not_ok = $module->$sub_name( %param ); | 
| 143 | 8 | 100 |  |  |  | 26 | if ( $params_not_ok ) { | 
| 144 | 3 |  |  |  |  | 11 | $self->errstr("Check params $params_not_ok"); | 
| 145 | 3 |  |  |  |  | 10 | return; | 
| 146 |  |  |  |  |  |  | } | 
| 147 |  |  |  |  |  |  |  | 
| 148 |  |  |  |  |  |  | # create new message | 
| 149 | 5 |  |  |  |  | 42 | my $message = $module->new( %param ); | 
| 150 |  |  |  |  |  |  |  | 
| 151 |  |  |  |  |  |  | # return message | 
| 152 | 5 |  |  |  |  | 44 | return $message; | 
| 153 |  |  |  |  |  |  | } | 
| 154 |  |  |  |  |  |  |  | 
| 155 |  |  |  |  |  |  | =head2 errstr | 
| 156 |  |  |  |  |  |  |  | 
| 157 |  |  |  |  |  |  | return the "last" error as string. | 
| 158 |  |  |  |  |  |  |  | 
| 159 |  |  |  |  |  |  | print $nexmo->errstr; | 
| 160 |  |  |  |  |  |  |  | 
| 161 |  |  |  |  |  |  | =cut | 
| 162 |  |  |  |  |  |  |  | 
| 163 |  |  |  |  |  |  | sub errstr { | 
| 164 | 6 |  |  | 6 | 1 | 12 | my ($self,$message) = @_; | 
| 165 |  |  |  |  |  |  |  | 
| 166 | 6 | 100 |  |  |  | 14 | $self->{__errstr__} = $message if @_ == 2; | 
| 167 | 6 |  |  |  |  | 8 | return $self->{__errstr__}; | 
| 168 |  |  |  |  |  |  | } | 
| 169 |  |  |  |  |  |  |  | 
| 170 |  |  |  |  |  |  | =head2 get_balance | 
| 171 |  |  |  |  |  |  |  | 
| 172 |  |  |  |  |  |  | my $balance = $nexmo->get_balance; | 
| 173 |  |  |  |  |  |  |  | 
| 174 |  |  |  |  |  |  | =cut | 
| 175 |  |  |  |  |  |  |  | 
| 176 |  |  |  |  |  |  | sub get_balance { | 
| 177 | 1 |  |  | 1 | 1 | 655 | my ($self,%param) = @_; | 
| 178 |  |  |  |  |  |  |  | 
| 179 | 1 |  | 33 |  |  | 6 | $param{server}   ||= $self->server; | 
| 180 | 1 |  | 33 |  |  | 5 | $param{username} ||= $self->username; | 
| 181 | 1 |  | 33 |  |  | 5 | $param{password} ||= $self->password; | 
| 182 |  |  |  |  |  |  |  | 
| 183 | 1 |  |  |  |  | 9 | my $balance = Nexmo::SMS::GetBalance->new( | 
| 184 |  |  |  |  |  |  | %param, | 
| 185 |  |  |  |  |  |  | ); | 
| 186 |  |  |  |  |  |  |  | 
| 187 | 1 |  |  |  |  | 4 | return $balance->get_balance; | 
| 188 |  |  |  |  |  |  | } | 
| 189 |  |  |  |  |  |  |  | 
| 190 |  |  |  |  |  |  | =head2 get_pricing | 
| 191 |  |  |  |  |  |  |  | 
| 192 |  |  |  |  |  |  | Not implemented yet... | 
| 193 |  |  |  |  |  |  |  | 
| 194 |  |  |  |  |  |  | =cut | 
| 195 |  |  |  |  |  |  |  | 
| 196 |  |  |  |  |  |  | sub get_pricing { | 
| 197 | 0 |  |  | 0 | 1 |  | warn "not implemented yet\n"; | 
| 198 | 0 |  |  |  |  |  | return; | 
| 199 |  |  |  |  |  |  | } | 
| 200 |  |  |  |  |  |  |  | 
| 201 |  |  |  |  |  |  | =head1 Attributes | 
| 202 |  |  |  |  |  |  |  | 
| 203 |  |  |  |  |  |  | These attributes are available for C objects. For each | 
| 204 |  |  |  |  |  |  | attribute there is a getter/setter: | 
| 205 |  |  |  |  |  |  |  | 
| 206 |  |  |  |  |  |  | $nexmo->server( 'servername' ); | 
| 207 |  |  |  |  |  |  | my $server = $nexmo->server; | 
| 208 |  |  |  |  |  |  |  | 
| 209 |  |  |  |  |  |  | =over 4 | 
| 210 |  |  |  |  |  |  |  | 
| 211 |  |  |  |  |  |  | =item * password | 
| 212 |  |  |  |  |  |  |  | 
| 213 |  |  |  |  |  |  | =item * server | 
| 214 |  |  |  |  |  |  |  | 
| 215 |  |  |  |  |  |  | =item * username | 
| 216 |  |  |  |  |  |  |  | 
| 217 |  |  |  |  |  |  | =back | 
| 218 |  |  |  |  |  |  |  | 
| 219 |  |  |  |  |  |  | =head1 ACKNOWLEDGEMENTS | 
| 220 |  |  |  |  |  |  |  | 
| 221 |  |  |  |  |  |  | Jui-Nan Lin added support for Unicode messages, thanks! | 
| 222 |  |  |  |  |  |  | (see https://github.com/reneeb/perl-Nexmo-SMS/pull/1/files) | 
| 223 |  |  |  |  |  |  |  | 
| 224 |  |  |  |  |  |  | =cut | 
| 225 |  |  |  |  |  |  |  | 
| 226 |  |  |  |  |  |  | 1; # End of Nexmo::SMS |