| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Nexmo::SMS::BinaryMessage; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 8 |  |  | 8 |  | 55 | use strict; | 
|  | 8 |  |  |  |  | 14 |  | 
|  | 8 |  |  |  |  | 314 |  | 
| 4 | 8 |  |  | 8 |  | 76 | use warnings; | 
|  | 8 |  |  |  |  | 15 |  | 
|  | 8 |  |  |  |  | 329 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 | 8 |  |  | 8 |  | 212131 | use Nexmo::SMS::Response; | 
|  | 8 |  |  |  |  | 23 |  | 
|  | 8 |  |  |  |  | 243 |  | 
| 7 |  |  |  |  |  |  |  | 
| 8 | 8 |  |  | 8 |  | 112770 | use LWP::UserAgent; | 
|  | 8 |  |  |  |  | 147088 |  | 
|  | 8 |  |  |  |  | 206 |  | 
| 9 | 8 |  |  | 8 |  | 49 | use JSON::PP; | 
|  | 8 |  |  |  |  | 17 |  | 
|  | 8 |  |  |  |  | 1076 |  | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | # ABSTRACT: Module that respresents a binary message for the Nexmo SMS API! | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  | our $VERSION = '0.02'; | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | my %attrs = ( | 
| 17 |  |  |  |  |  |  | body              => 'required', | 
| 18 |  |  |  |  |  |  | udh               => 'required', | 
| 19 |  |  |  |  |  |  | type              => 'required', | 
| 20 |  |  |  |  |  |  | from              => 'required', | 
| 21 |  |  |  |  |  |  | to                => 'required', | 
| 22 |  |  |  |  |  |  | server            => 'required', | 
| 23 |  |  |  |  |  |  | username          => 'required', | 
| 24 |  |  |  |  |  |  | password          => 'required', | 
| 25 |  |  |  |  |  |  | status_report_req => 'optional', | 
| 26 |  |  |  |  |  |  | client_ref        => 'optional', | 
| 27 |  |  |  |  |  |  | network_code      => 'optional', | 
| 28 |  |  |  |  |  |  | ); | 
| 29 |  |  |  |  |  |  |  | 
| 30 |  |  |  |  |  |  | for my $attr ( keys %attrs ) { | 
| 31 | 8 |  |  | 8 |  | 46 | no strict 'refs'; | 
|  | 8 |  |  |  |  | 21 |  | 
|  | 8 |  |  |  |  | 5959 |  | 
| 32 |  |  |  |  |  |  | *{ __PACKAGE__ . '::' . $attr } = sub { | 
| 33 | 19 |  |  | 19 |  | 30 | my ($self,$value) = @_; | 
| 34 |  |  |  |  |  |  |  | 
| 35 | 19 |  |  |  |  | 39 | my $key = '__' . $attr . '__'; | 
| 36 | 19 | 100 |  |  |  | 57 | $self->{$key} = $value if @_ == 2; | 
| 37 | 19 |  |  |  |  | 80 | return $self->{$key}; | 
| 38 |  |  |  |  |  |  | }; | 
| 39 |  |  |  |  |  |  | } | 
| 40 |  |  |  |  |  |  |  | 
| 41 |  |  |  |  |  |  |  | 
| 42 |  |  |  |  |  |  | sub new { | 
| 43 | 1 |  |  | 1 | 1 | 7 | my ($class,%param) = @_; | 
| 44 |  |  |  |  |  |  |  | 
| 45 | 1 |  |  |  |  | 4 | my $self = bless {}, $class; | 
| 46 |  |  |  |  |  |  |  | 
| 47 | 1 |  |  |  |  | 5 | for my $attr ( keys %attrs ) { | 
| 48 | 11 | 100 |  |  |  | 118 | if ( exists $param{$attr} ) { | 
| 49 | 8 |  |  |  |  | 27 | $self->$attr( $param{$attr} ); | 
| 50 |  |  |  |  |  |  | } | 
| 51 |  |  |  |  |  |  | } | 
| 52 |  |  |  |  |  |  |  | 
| 53 |  |  |  |  |  |  | $self->user_agent( | 
| 54 | 1 |  |  |  |  | 14 | LWP::UserAgent->new( | 
| 55 |  |  |  |  |  |  | agent => 'Perl module ' . __PACKAGE__ . ' ' . $VERSION, | 
| 56 |  |  |  |  |  |  | ), | 
| 57 |  |  |  |  |  |  | ); | 
| 58 |  |  |  |  |  |  |  | 
| 59 | 1 |  |  |  |  | 11 | return $self; | 
| 60 |  |  |  |  |  |  | } | 
| 61 |  |  |  |  |  |  |  | 
| 62 |  |  |  |  |  |  |  | 
| 63 |  |  |  |  |  |  | sub user_agent { | 
| 64 | 2 |  |  | 2 | 1 | 7095 | my ($self,$ua) = @_; | 
| 65 |  |  |  |  |  |  |  | 
| 66 | 2 | 100 |  |  |  | 12 | $self->{__ua__} = $ua if @_ == 2; | 
| 67 | 2 |  |  |  |  | 8 | return $self->{__ua__}; | 
| 68 |  |  |  |  |  |  | } | 
| 69 |  |  |  |  |  |  |  | 
| 70 |  |  |  |  |  |  |  | 
| 71 |  |  |  |  |  |  | sub errstr { | 
| 72 | 0 |  |  | 0 | 1 | 0 | my ($self,$message) = @_; | 
| 73 |  |  |  |  |  |  |  | 
| 74 | 0 | 0 |  |  |  | 0 | $self->{__errstr__} = $message if @_ == 2; | 
| 75 | 0 |  |  |  |  | 0 | return $self->{__errstr__}; | 
| 76 |  |  |  |  |  |  | } | 
| 77 |  |  |  |  |  |  |  | 
| 78 |  |  |  |  |  |  |  | 
| 79 |  |  |  |  |  |  | sub send { | 
| 80 | 1 |  |  | 1 | 1 | 731 | my ($self) = shift; | 
| 81 |  |  |  |  |  |  |  | 
| 82 | 1 |  |  |  |  | 3 | my %optional; | 
| 83 | 1 | 50 |  |  |  | 5 | $optional{'client-ref'}        = $self->client_ref        if $self->client_ref; | 
| 84 | 1 | 50 |  |  |  | 4 | $optional{'status-report-req'} = $self->status_report_req if $self->status_report_req; | 
| 85 | 1 | 50 |  |  |  | 5 | $optional{'network-code'}      = $self->network_code      if $self->network_code; | 
| 86 |  |  |  |  |  |  |  | 
| 87 | 1 |  |  |  |  | 4 | my $response = $self->user_agent->post( | 
| 88 |  |  |  |  |  |  | $self->server, | 
| 89 |  |  |  |  |  |  | { | 
| 90 |  |  |  |  |  |  | %optional, | 
| 91 |  |  |  |  |  |  | username => $self->username, | 
| 92 |  |  |  |  |  |  | password => $self->password, | 
| 93 |  |  |  |  |  |  | from     => $self->from, | 
| 94 |  |  |  |  |  |  | to       => $self->to, | 
| 95 |  |  |  |  |  |  | body     => $self->body, | 
| 96 |  |  |  |  |  |  | udh      => $self->udh, | 
| 97 |  |  |  |  |  |  | type     => $self->type, | 
| 98 |  |  |  |  |  |  | }, | 
| 99 |  |  |  |  |  |  | ); | 
| 100 |  |  |  |  |  |  |  | 
| 101 | 1 | 50 | 33 |  |  | 26 | if ( !$response || !$response->is_success ) { | 
| 102 | 0 |  |  |  |  | 0 | $self->errstr("Request was not successful: " . $response->status_line); | 
| 103 | 0 | 0 |  |  |  | 0 | warn $response->content if $response; | 
| 104 | 0 |  |  |  |  | 0 | return; | 
| 105 |  |  |  |  |  |  | } | 
| 106 |  |  |  |  |  |  |  | 
| 107 | 1 |  |  |  |  | 18 | my $json            = $response->content; | 
| 108 | 1 |  |  |  |  | 26 | my $response_object = Nexmo::SMS::Response->new( json => $json ); | 
| 109 |  |  |  |  |  |  |  | 
| 110 | 1 | 50 |  |  |  | 6 | if ( $response_object->is_error ) { | 
| 111 | 0 |  |  |  |  | 0 | $self->errstr( $response_object->errstr ); | 
| 112 |  |  |  |  |  |  | } | 
| 113 |  |  |  |  |  |  |  | 
| 114 | 1 |  |  |  |  | 17 | return $response_object; | 
| 115 |  |  |  |  |  |  | } | 
| 116 |  |  |  |  |  |  |  | 
| 117 |  |  |  |  |  |  |  | 
| 118 |  |  |  |  |  |  | sub check_needed_params { | 
| 119 | 2 |  |  | 2 | 1 | 10 | my ($class,%params) = @_; | 
| 120 |  |  |  |  |  |  |  | 
| 121 | 2 |  |  |  |  | 4 | my @params_not_ok; | 
| 122 |  |  |  |  |  |  |  | 
| 123 | 2 |  |  |  |  | 10 | for my $attr ( keys %attrs ) { | 
| 124 | 22 | 100 | 100 |  |  | 95 | if ( $attrs{$attr} eq 'required' and !$params{$attr} ) { | 
| 125 | 4 |  |  |  |  | 6 | push @params_not_ok, $attr; | 
| 126 |  |  |  |  |  |  | } | 
| 127 |  |  |  |  |  |  | } | 
| 128 |  |  |  |  |  |  |  | 
| 129 | 2 |  |  |  |  | 15 | return join ", ", @params_not_ok; | 
| 130 |  |  |  |  |  |  | } | 
| 131 |  |  |  |  |  |  |  | 
| 132 |  |  |  |  |  |  |  | 
| 133 |  |  |  |  |  |  | 1; | 
| 134 |  |  |  |  |  |  |  | 
| 135 |  |  |  |  |  |  |  | 
| 136 |  |  |  |  |  |  | __END__ |