Last active
August 29, 2015 14:17
-
-
Save oiami/236ba8bc2de07cb0c658 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use strict; | |
use warnings; | |
package WebService::SendGrid::Newsletter::Lists; | |
use WebService::SendGrid::Newsletter::Lists::Email; | |
=method new | |
Creates a new instance of WebService::SendGrid::Newsletter::Lists. | |
my $lists = WebService::SendGrid::Newsletter::Lists->new(sgn => $sgn); | |
Parameters: | |
=over 4 | |
=item * sgn | |
An instance of WebService::SendGrid::Newsletter. | |
=back | |
=cut | |
sub new { | |
my ($class, %args) = @_; | |
my $self = {}; | |
bless($self, $class); | |
$self->{sgn} = $args{sgn}; | |
return $self; | |
} | |
sub add { | |
my ($self, %args) = @_; | |
return $self->{sgn}->_send_request('lists/add', %args); | |
} | |
sub get { | |
my ($self, %args) = @_; | |
return $self->{sgn}->_send_request('lists/get', %args); | |
} | |
sub edit { | |
my ($self, %args) = @_; | |
return $self->{sgn}->_send_request('lists/edit', %args); | |
} | |
sub delete { | |
my ($self, %args) = @_; | |
return $self->{sgn}->_send_request('lists/delete', %args); | |
} | |
sub email { | |
my ($self) = @_; | |
if (!defined $self->{email}) { | |
$self->{email} = WebService::SendGrid::Newsletter::Lists::Email->new( | |
sgn => $self->{sgn} | |
); | |
} | |
return $self->{email}; | |
} | |
1; | |
#-------------------------------------------------------------------------------------------------------- | |
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use Test::More; | |
use Test::Exception; | |
BEGIN { | |
use_ok( | |
'WebService::SendGrid::Newsletter', | |
); | |
plan skip_all => 'SENDGRID_API_USER and SENDGRID_API_KEY are required to run live tests' | |
unless ($ENV{SENDGRID_API_USER} && $ENV{SENDGRID_API_KEY}); | |
} | |
my $sgn; | |
lives_ok { | |
$sgn = WebService::SendGrid::Newsletter->new( | |
api_user => $ENV{SENDGRID_API_USER}, | |
api_key => $ENV{SENDGRID_API_KEY}, | |
); | |
} 'Can successfully create a new instance'; | |
my $list_name = 'subscribers_test'; | |
$sgn->lists->add(list => $list_name, name => 'name'); | |
is($sgn->{last_response_code}, 200, 'Can successfully create new subscribers list'); | |
ok($sgn->{last_response}->{message}, 'Get response message'); | |
$sgn->lists->get(list => $list_name); | |
is($sgn->{last_response_code}, 200, 'Can successfully get new subscribers list'); | |
ok($sgn->{last_response}->[0]->{id}, 'Get list ID'); | |
is($sgn->{last_response}->[0]->{list}, "$list_name", 'Get list name'); | |
$sgn->lists->email->add( | |
list => $list_name, | |
data => { | |
name => 'Customer', | |
email => 'customer2@somemail.com' | |
} | |
); | |
is($sgn->{last_response_code}, 200, 'Can successfully add subscriber to the list'); | |
my $new_list = 'subscribers_new'; | |
$sgn->lists->edit(list => $list_name, newlist => $new_list); | |
is($sgn->{last_response}->{message}, 'success', 'Get correct response message'); | |
is($sgn->{last_response_code}, 200, 'Can successfully update subscriber list'); | |
$sgn->lists->delete(list => $new_list); | |
is($sgn->{last_response}->{message}, 'success', 'Get correct response message'); | |
is($sgn->{last_response_code}, 200, 'Can successfully delete subscriber list'); | |
done_testing(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment