| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Nexmo::SMS::Response; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 8 |  |  | 8 |  | 33 | use strict; | 
|  | 8 |  |  |  |  | 11 |  | 
|  | 8 |  |  |  |  | 260 |  | 
| 4 | 8 |  |  | 8 |  | 28 | use warnings; | 
|  | 8 |  |  |  |  | 8 |  | 
|  | 8 |  |  |  |  | 137 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 | 8 |  |  | 8 |  | 2792 | use Nexmo::SMS::Response::Message; | 
|  | 8 |  |  |  |  | 14 |  | 
|  | 8 |  |  |  |  | 221 |  | 
| 7 |  |  |  |  |  |  |  | 
| 8 | 8 |  |  | 8 |  | 713 | use JSON::PP; | 
|  | 8 |  |  |  |  | 14598 |  | 
|  | 8 |  |  |  |  | 734 |  | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | # ABSTRACT: Module that represents a response from Nexmo SMS API! | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | our $VERSION = '0.01'; | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  | # create getter/setter | 
| 15 |  |  |  |  |  |  | my @attrs = qw(json message_count status); | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | for my $attr ( @attrs ) { | 
| 18 | 8 |  |  | 8 |  | 44 | no strict 'refs'; | 
|  | 8 |  |  |  |  | 9 |  | 
|  | 8 |  |  |  |  | 3116 |  | 
| 19 |  |  |  |  |  |  | *{ __PACKAGE__ . '::' . $attr } = sub { | 
| 20 | 28 |  |  | 28 |  | 48 | my ($self,$value) = @_; | 
| 21 |  |  |  |  |  |  |  | 
| 22 | 28 |  |  |  |  | 62 | my $key = '__' . $attr . '__'; | 
| 23 | 28 | 100 |  |  |  | 111 | $self->{$key} = $value if @_ == 2; | 
| 24 | 28 |  |  |  |  | 100 | return $self->{$key}; | 
| 25 |  |  |  |  |  |  | }; | 
| 26 |  |  |  |  |  |  | } | 
| 27 |  |  |  |  |  |  |  | 
| 28 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 29 |  |  |  |  |  |  |  | 
| 30 |  |  |  |  |  |  | This module represents a response from Nexmo. | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  | use Nexmo::SMS::Response; | 
| 34 |  |  |  |  |  |  |  | 
| 35 |  |  |  |  |  |  | my $nexmo = Nexmo::SMS::Response->new( | 
| 36 |  |  |  |  |  |  | json => '{ | 
| 37 |  |  |  |  |  |  | "message-count":"1", | 
| 38 |  |  |  |  |  |  | "messages":[ | 
| 39 |  |  |  |  |  |  | { | 
| 40 |  |  |  |  |  |  | "status":"4", | 
| 41 |  |  |  |  |  |  | "message-id":"message001", | 
| 42 |  |  |  |  |  |  | "client-ref":"Test001 - Reference", | 
| 43 |  |  |  |  |  |  | "remaining-balance":"20.0", | 
| 44 |  |  |  |  |  |  | "message-price":"0.05", | 
| 45 |  |  |  |  |  |  | "error-text":"" | 
| 46 |  |  |  |  |  |  | } | 
| 47 |  |  |  |  |  |  | ] | 
| 48 |  |  |  |  |  |  | }', | 
| 49 |  |  |  |  |  |  | ); | 
| 50 |  |  |  |  |  |  |  | 
| 51 |  |  |  |  |  |  | for my $message ( $response ) { | 
| 52 |  |  |  |  |  |  | print $message->status; | 
| 53 |  |  |  |  |  |  | } | 
| 54 |  |  |  |  |  |  |  | 
| 55 |  |  |  |  |  |  | =head1 METHODS | 
| 56 |  |  |  |  |  |  |  | 
| 57 |  |  |  |  |  |  | =head2 new | 
| 58 |  |  |  |  |  |  |  | 
| 59 |  |  |  |  |  |  | create a new object | 
| 60 |  |  |  |  |  |  |  | 
| 61 |  |  |  |  |  |  | my $foo = Nexmo::SMS::Response->new( | 
| 62 |  |  |  |  |  |  | json => '{ | 
| 63 |  |  |  |  |  |  | "message-count":"1", | 
| 64 |  |  |  |  |  |  | "messages":[ | 
| 65 |  |  |  |  |  |  | { | 
| 66 |  |  |  |  |  |  | "status":"4", | 
| 67 |  |  |  |  |  |  | "message-id":"message001", | 
| 68 |  |  |  |  |  |  | "client-ref":"Test001 - Reference", | 
| 69 |  |  |  |  |  |  | "remaining-balance":"20.0", | 
| 70 |  |  |  |  |  |  | "message-price":"0.05", | 
| 71 |  |  |  |  |  |  | "error-text":"" | 
| 72 |  |  |  |  |  |  | } | 
| 73 |  |  |  |  |  |  | ] | 
| 74 |  |  |  |  |  |  | }', | 
| 75 |  |  |  |  |  |  | ); | 
| 76 |  |  |  |  |  |  |  | 
| 77 |  |  |  |  |  |  | =cut | 
| 78 |  |  |  |  |  |  |  | 
| 79 |  |  |  |  |  |  | sub new { | 
| 80 | 5 |  |  | 5 | 1 | 18 | my ($class,%param) = @_; | 
| 81 |  |  |  |  |  |  |  | 
| 82 | 5 |  |  |  |  | 15 | my $self = bless {}, $class; | 
| 83 |  |  |  |  |  |  |  | 
| 84 | 5 | 50 |  |  |  | 22 | return $self if !$param{json}; | 
| 85 |  |  |  |  |  |  |  | 
| 86 |  |  |  |  |  |  | # decode json | 
| 87 | 5 |  |  |  |  | 43 | my $coder = JSON::PP->new->utf8->pretty->allow_nonref; | 
| 88 | 5 |  |  |  |  | 956 | my $perl  = $coder->decode( $param{json} ); | 
| 89 |  |  |  |  |  |  |  | 
| 90 | 5 |  |  |  |  | 14396 | $self->message_count( $perl->{'message-count'} ); | 
| 91 | 5 |  |  |  |  | 16 | $self->status( 0 ); | 
| 92 |  |  |  |  |  |  |  | 
| 93 |  |  |  |  |  |  | # for each message create a new message object | 
| 94 | 5 | 50 |  |  |  | 7 | for my $message ( @{ $perl->{messages} || [] } ) { | 
|  | 5 |  |  |  |  | 30 |  | 
| 95 | 6 | 50 |  |  |  | 80 | $self->_add_message( | 
| 96 | 6 |  |  |  |  | 11 | Nexmo::SMS::Response::Message->new( %{$message || {}} ) | 
| 97 |  |  |  |  |  |  | ); | 
| 98 |  |  |  |  |  |  | } | 
| 99 |  |  |  |  |  |  |  | 
| 100 | 5 |  |  |  |  | 62 | return $self; | 
| 101 |  |  |  |  |  |  | } | 
| 102 |  |  |  |  |  |  |  | 
| 103 |  |  |  |  |  |  | =head2 messages | 
| 104 |  |  |  |  |  |  |  | 
| 105 |  |  |  |  |  |  | returns the list of messages included in the response. Each element is an | 
| 106 |  |  |  |  |  |  | object of L. | 
| 107 |  |  |  |  |  |  |  | 
| 108 |  |  |  |  |  |  | my @messages = $response->messages; | 
| 109 |  |  |  |  |  |  |  | 
| 110 |  |  |  |  |  |  | =cut | 
| 111 |  |  |  |  |  |  |  | 
| 112 |  |  |  |  |  |  | sub messages { | 
| 113 | 4 |  |  | 4 | 1 | 12 | my ($self) = @_; | 
| 114 |  |  |  |  |  |  |  | 
| 115 | 4 | 50 |  |  |  | 7 | return @{ $self->{__messages__} || [] }; | 
|  | 4 |  |  |  |  | 29 |  | 
| 116 |  |  |  |  |  |  | } | 
| 117 |  |  |  |  |  |  |  | 
| 118 |  |  |  |  |  |  | sub _add_message { | 
| 119 | 6 |  |  | 6 |  | 14 | my ($self,$message) = @_; | 
| 120 |  |  |  |  |  |  |  | 
| 121 | 6 | 50 | 33 |  |  | 106 | if ( @_ == 2 and $message->isa( 'Nexmo::SMS::Response::Message' ) ) { | 
| 122 | 6 |  |  |  |  | 12 | push @{$self->{__messages__}}, $message; | 
|  | 6 |  |  |  |  | 154 |  | 
| 123 | 6 | 100 |  |  |  | 34 | if ( $message->status != 0 ) { | 
| 124 | 1 |  |  |  |  | 3 | $self->status(1); | 
| 125 | 1 |  |  |  |  | 3 | $self->errstr( $message->status_text . ' (' . $message->status_desc . ')' ); | 
| 126 |  |  |  |  |  |  | } | 
| 127 |  |  |  |  |  |  | } | 
| 128 |  |  |  |  |  |  | } | 
| 129 |  |  |  |  |  |  |  | 
| 130 |  |  |  |  |  |  | =head2 errstr | 
| 131 |  |  |  |  |  |  |  | 
| 132 |  |  |  |  |  |  | return the "last" error as string. | 
| 133 |  |  |  |  |  |  |  | 
| 134 |  |  |  |  |  |  | print $response->errstr; | 
| 135 |  |  |  |  |  |  |  | 
| 136 |  |  |  |  |  |  | =cut | 
| 137 |  |  |  |  |  |  |  | 
| 138 |  |  |  |  |  |  |  | 
| 139 |  |  |  |  |  |  | sub errstr { | 
| 140 | 2 |  |  | 2 | 1 | 3 | my ($self,$message) = @_; | 
| 141 |  |  |  |  |  |  |  | 
| 142 | 2 | 100 |  |  |  | 6 | $self->{__errstr__} = $message if @_ == 2; | 
| 143 | 2 |  |  |  |  | 8 | return $self->{__errstr__}; | 
| 144 |  |  |  |  |  |  | } | 
| 145 |  |  |  |  |  |  |  | 
| 146 |  |  |  |  |  |  | =head2 is_success | 
| 147 |  |  |  |  |  |  |  | 
| 148 |  |  |  |  |  |  | returns 1 if all messages have a status = 0, C otherwise. | 
| 149 |  |  |  |  |  |  |  | 
| 150 |  |  |  |  |  |  | =cut | 
| 151 |  |  |  |  |  |  |  | 
| 152 |  |  |  |  |  |  | sub is_success { | 
| 153 | 4 |  |  | 4 | 1 | 3844 | my ($self) = @_; | 
| 154 | 4 |  |  |  |  | 13 | return !$self->status; | 
| 155 |  |  |  |  |  |  | } | 
| 156 |  |  |  |  |  |  |  | 
| 157 |  |  |  |  |  |  | =head2 is_error | 
| 158 |  |  |  |  |  |  |  | 
| 159 |  |  |  |  |  |  | Returns 1 if an error occured, 0 otherwise... | 
| 160 |  |  |  |  |  |  |  | 
| 161 |  |  |  |  |  |  | =cut | 
| 162 |  |  |  |  |  |  |  | 
| 163 |  |  |  |  |  |  | sub is_error { | 
| 164 | 9 |  |  | 9 | 1 | 18 | my ($self) = @_; | 
| 165 | 9 |  |  |  |  | 25 | return $self->status; | 
| 166 |  |  |  |  |  |  | } | 
| 167 |  |  |  |  |  |  |  | 
| 168 |  |  |  |  |  |  | 1; | 
| 169 |  |  |  |  |  |  |  | 
| 170 |  |  |  |  |  |  | =head1 ATTRIBUTES | 
| 171 |  |  |  |  |  |  |  | 
| 172 |  |  |  |  |  |  | These attributes are available for C objects: | 
| 173 |  |  |  |  |  |  |  | 
| 174 |  |  |  |  |  |  | $nexmo->status( 'status' ); | 
| 175 |  |  |  |  |  |  | my $status = $nexmo->status; | 
| 176 |  |  |  |  |  |  |  | 
| 177 |  |  |  |  |  |  | =over 4 | 
| 178 |  |  |  |  |  |  |  | 
| 179 |  |  |  |  |  |  | =item * json | 
| 180 |  |  |  |  |  |  |  | 
| 181 |  |  |  |  |  |  | =item * message_count | 
| 182 |  |  |  |  |  |  |  | 
| 183 |  |  |  |  |  |  | =item * status | 
| 184 |  |  |  |  |  |  |  | 
| 185 |  |  |  |  |  |  | =back | 
| 186 |  |  |  |  |  |  |  |