Skip to content

Commit

Permalink
add support for get_balance
Browse files Browse the repository at this point in the history
  • Loading branch information
reneeb committed Aug 28, 2011
1 parent 20b2942 commit 9232869
Show file tree
Hide file tree
Showing 6 changed files with 257 additions and 5 deletions.
3 changes: 3 additions & 0 deletions Changes
@@ -1,5 +1,8 @@
Revision history for Nexmo-SMS

0.03 Aug 28, 2011 - "Amsterdam"
+ add support for get_balance()

0.02 Aug 28, 2011 - "London"
+ Support WAPPush and Binary messages
+ more tests
Expand Down
5 changes: 5 additions & 0 deletions MANIFEST
Expand Up @@ -7,6 +7,8 @@ lib/Nexmo/SMS/Response.pm
lib/Nexmo/SMS/Response/Message.pm
lib/Nexmo/SMS/BinaryMessage.pm
lib/Nexmo/SMS/TextMessage.pm
lib/Nexmo/SMS/WAPPushMessage.pm
lib/Nexmo/SMS/GetBalance.pm

MANIFEST

Expand All @@ -15,6 +17,9 @@ t/01-send-sms-success.t
t/02-send-sms-success-full-params.t
t/03-send-sms-error.t
t/04-send-binary-success.t
t/05-send-wappush-success.t
t/06-check-params.t
t/07-get-balance.t
t/boilerplate.t
t/pod-coverage.t
t/pod.t
21 changes: 16 additions & 5 deletions lib/Nexmo/SMS.pm
Expand Up @@ -7,17 +7,19 @@ use Nexmo::SMS::BinaryMessage;
use Nexmo::SMS::TextMessage;
use Nexmo::SMS::WAPPushMessage;

use Nexmo::SMS::GetBalance;

=head1 NAME
Nexmo::SMS - Module for the Nexmo SMS API!
=head1 VERSION
Version 0.02
Version 0.03
=cut

our $VERSION = '0.02';
our $VERSION = '0.03';


=head1 SYNOPSIS
Expand Down Expand Up @@ -169,13 +171,22 @@ sub errstr {

=head2 get_balance
Not implemented yet...
my $balance = $nexmo->get_balance;
=cut

sub get_balance {
warn "not implemented yet\n";
return;
my ($self,%param) = @_;

$param{server} ||= $self->server;
$param{username} ||= $self->username;
$param{password} ||= $self->password;

my $balance = Nexmo::SMS::GetBalance->new(
%param,
);

return $balance->get_balance;
}

=head2 get_pricing
Expand Down
180 changes: 180 additions & 0 deletions lib/Nexmo/SMS/GetBalance.pm
@@ -0,0 +1,180 @@
package Nexmo::SMS::GetBalance;

use strict;
use warnings;

use LWP::UserAgent;
use JSON::PP;

=head1 NAME
Nexmo::SMS::GetBalance - Module to ask for the balance for the Nexmo SMS API!
=head1 VERSION
Version 0.01
=cut

our $VERSION = '0.01';

my %attrs = (
server => 'required',
username => 'required',
password => 'required',
);

for my $attr ( keys %attrs ) {
no strict 'refs';
*{ __PACKAGE__ . '::' . $attr } = sub {
my ($self,$value) = @_;

my $key = '__' . $attr . '__';
$self->{$key} = $value if @_ == 2;
return $self->{$key};
};
}

=head1 SYNOPSIS
This module simplifies sending SMS through the Nexmo API.
use Nexmo::SMS::GetBalance;
my $nexmo = Nexmo::SMS::GetBalance->new(
server => 'http://test.nexmo.com/sms/json',
username => 'testuser1',
password => 'testpasswd2',
);
my $balance = $sms->get_balance;
=head1 METHODS
=head2 new
create a new object
my $object = Nexmo::SMS::GetBalance->new(
server => 'http://test.nexmo.com/sms/json',
username => 'testuser1',
password => 'testpasswd2',
);
This method recognises these parameters:
server => 'required',
username => 'required',
password => 'required',
=cut

sub new {
my ($class,%param) = @_;

my $self = bless {}, $class;

for my $attr ( keys %attrs ) {
if ( exists $param{$attr} ) {
$self->$attr( $param{$attr} );
}
}

$self->user_agent(
LWP::UserAgent->new(
agent => 'Perl module ' . __PACKAGE__ . ' ' . $VERSION,
),
);

return $self;
}

=head2 user_agent
Getter/setter for the user_agent attribute of the object. By default a new
object of LWP::UserAgent is used, but you can use your own class as long as it
is compatible to LWP::UserAgent.
$sms->user_agent( MyUserAgent->new );
my $ua = $sms->user_agent;
=cut

sub user_agent {
my ($self,$ua) = @_;

$self->{__ua__} = $ua if @_ == 2;
return $self->{__ua__};
}

=head2 get_balance
This actually calls the Nexmo SMS API. It returns the balance of the account.
my $balance = $object->get_balance;
=cut

sub get_balance {
my ($self) = shift;

use Data::Dumper;

my $url = sprintf "%saccount/get-balance/%s/%s",
$self->server,
$self->username,
$self->password;

my $ua = $self->user_agent;
$ua->default_header( 'Accept' => 'application/json' );

my $response = $ua->get(
$url,
);

if ( !$response || !$response->is_success ) {
return;
}

my $json = $response->content;
my $coder = JSON::PP->new->utf8->pretty->allow_nonref;
my $perl = $coder->decode( $json );

return if !$perl || ref $perl ne 'HASH';
return $perl->{'value'};
}

=head1 Attributes
These attributes are available for C<Nexmo::SMS::GetBalance> objects. For each
attribute there is a getter/setter:
$nexmo->server( 'servername' );
my $server = $nexmo->server;
=over 4
=item * password
=item * server
=item * username
=back
=cut

1;

=head1 ACKNOWLEDGEMENTS
=head1 COPYRIGHT & LICENSE
Copyright 2011 Renee Baecker.
This program is released under the following license: artistic_2
=cut
33 changes: 33 additions & 0 deletions lib/Nexmo/SMS/MockLWP.pm
Expand Up @@ -34,6 +34,34 @@ our $VERSION = 0.01;
my $perl = $coder->decode( $json );
my $from = $params->{from};

if ( $url =~ /get-balance/ ) {
$from = 'get-balance';
$url = 'get-balance';
}

my $subhash = $perl->{$url}->{$from};
my $response = $coder->encode( $subhash );

my $http_response = HTTP::Response->new( 200 );
$http_response->content( $response );

return $http_response;
};

*LWP::UserAgent::get = sub {
my ($object,$url,$params) = @_;

my $json = do{ undef $/; <DATA> };

my $coder = JSON::PP->new->ascii->pretty->allow_nonref;
my $perl = $coder->decode( $json );
my $from = $params->{from};

if ( $url =~ /get-balance/ ) {
$from = 'get-balance';
$url = 'get-balance';
}

my $subhash = $perl->{$url}->{$from};
my $response = $coder->encode( $subhash );

Expand Down Expand Up @@ -135,5 +163,10 @@ __DATA__
}
]
}
},
"get-balance" : {
"get-balance" : {
"value" : 4.15
}
}
}
20 changes: 20 additions & 0 deletions t/07-get-balance.t
@@ -0,0 +1,20 @@
#!perl -T

use strict;
use warnings;

use Test::More tests => 2;

use Nexmo::SMS::MockLWP;
use Nexmo::SMS;

my $nexmo = Nexmo::SMS->new(
server => 'http://rest.nexmo.com/sms/json',
username => 'testuser',
password => 'testpasswd',
);

ok( $nexmo->isa( 'Nexmo::SMS' ), '$nexmo is a Nexmo::SMS' );

my $value = $nexmo->get_balance() or diag $nexmo->errstr;
is $value, 4.15, 'Balance is 4.15';

0 comments on commit 9232869

Please sign in to comment.