diff --git a/lib/WebService/MyAffiliates.pm b/lib/WebService/MyAffiliates.pm index 7c28fa1..fff9f24 100644 --- a/lib/WebService/MyAffiliates.pm +++ b/lib/WebService/MyAffiliates.pm @@ -16,7 +16,7 @@ sub errstr { return $errstr } sub new { ## no critic (ArgUnpacking) my $class = shift; - my %args = @_ % 2 ? %{$_[0]} : @_; + my %args = @_ % 2 ? %{ $_[0] } : @_; for (qw/user pass host/) { $args{$_} || croak "Param $_ is required."; @@ -36,7 +36,7 @@ sub __http_tiny { return $self->{http_tiny} if exists $self->{http_tiny}; - my $http_tiny = HTTP::Tiny->new(timeout => $self->{timeout}); + my $http_tiny = HTTP::Tiny->new( timeout => $self->{timeout} ); $self->{http_tiny} = $http_tiny; return $http_tiny; @@ -45,25 +45,36 @@ sub __http_tiny { ## https://myaffiliates.atlassian.net/wiki/display/PUB/Feed+1%3A+Users+Feed sub get_users { ## no critic (ArgUnpacking) my $self = shift; - my %args = @_ % 2 ? %{$_[0]} : @_; + my %args = @_ % 2 ? %{ $_[0] } : @_; my $url = Mojo::URL->new('/feeds.php?FEED_ID=1'); - $url->query(\%args) if %args; - return $self->request($url->to_string); + $url->query( \%args ) if %args; + return $self->request( $url->to_string ); +} + +sub get_users_status { ## no critic (ArgUnpacking) + my $self = shift; + my %args = @_ % 2 ? %{ $_[0] } : @_; + my $url = Mojo::URL->new('/feeds.php?FEED_ID=3'); + $url->query( \%args ) if %args; + return $self->request( $url->to_string ); } sub create_affiliate { ## no critic (ArgUnpacking) my $self = shift; - my %args = @_ % 2 ? %{$_[0]} : @_; - my $parameters = +{map { ('PARAM_' . $_ => $args{$_}) } keys %args}; + my %args = @_ % 2 ? %{ $_[0] } : @_; + my $parameters = +{ map { ( 'PARAM_' . $_ => $args{$_} ) } keys %args }; my $url = Mojo::URL->new('/feeds.php?FEED_ID=26'); $url->query($parameters); - my $res = $self->request($url->to_string); + my $res = $self->request( $url->to_string ); my $error_count = $res->{INIT}->{ERROR_COUNT}; if ($error_count) { - my $init = $res->{INIT}; - my @errors = ref $init->{ERROR} eq 'ARRAY' ? $init->{ERROR}->@* : ($init->{ERROR}); + my $init = $res->{INIT}; + my @errors = + ref $init->{ERROR} eq 'ARRAY' + ? $init->{ERROR}->@* + : ( $init->{ERROR} ); $errstr = map { $_->{MSG} . " " . $_->{DETAIL} } @errors; return; } @@ -74,10 +85,17 @@ sub create_affiliate { ## no critic (ArgUnpacking) } sub get_user { - my ($self, $id) = @_; + my ( $self, $id ) = @_; + + $id or croak "id is required."; + my $user = $self->get_users( USER_ID => $id ) or return; + return $user->{USER}; +} - $id or croak "id is required."; - my $user = $self->get_users(USER_ID => $id) or return; +sub get_users_status_by_affiliate_id { + my ( $self, $ids ) = @_; + $ids or croak "id is required."; + my $user = $self->get_users_status( USER_IDS => $ids ); return $user->{USER}; } @@ -86,68 +104,73 @@ sub decode_token { my $self = shift; my @tokens = @_ or croak 'Must pass at least one token.'; - return $self->request('/feeds.php?FEED_ID=4&TOKENS=' . url_escape(join(',', @tokens))); + return $self->request( + '/feeds.php?FEED_ID=4&TOKENS=' . url_escape( join( ',', @tokens ) ) ); } ## https://myaffiliates.atlassian.net/wiki/display/PUB/Feed+5%3A+Encode+Token sub encode_token { ## no critic (ArgUnpacking) my $self = shift; - my %args = @_ % 2 ? %{$_[0]} : @_; + my %args = @_ % 2 ? %{ $_[0] } : @_; $args{USER_ID} or croak "USER_ID is required."; $args{SETUP_ID} or croak "SETUP_ID is required."; my $url = Mojo::URL->new('/feeds.php?FEED_ID=5'); - $url->query(\%args) if %args; - return $self->request($url->to_string); + $url->query( \%args ) if %args; + return $self->request( $url->to_string ); } ## https://myaffiliates.atlassian.net/wiki/display/PUB/Feed+6%3A+User+Transactions+Feed sub get_user_transactions { ## no critic (ArgUnpacking) my $self = shift; - my %args = @_ % 2 ? %{$_[0]} : @_; + my %args = @_ % 2 ? %{ $_[0] } : @_; $args{FROM_DATE} or croak 'FROM_DATE is reqired.'; my $url = Mojo::URL->new('/feeds.php?FEED_ID=6'); - $url->query(\%args) if %args; - return $self->request($url->to_string); + $url->query( \%args ) if %args; + return $self->request( $url->to_string ); } sub get_customers { ## no critic (ArgUnpacking) my $self = shift; - my %args = @_ % 2 ? %{$_[0]} : @_; + my %args = @_ % 2 ? %{ $_[0] } : @_; my $url = Mojo::URL->new('/feeds.php?FEED_ID=10'); - $url->query(\%args) if %args; + $url->query( \%args ) if %args; - my $res = $self->request($url->to_string); + my $res = $self->request( $url->to_string ); my $customers = - !exists $res->{PLAYER} ? [] - : ref $res->{PLAYER} eq 'ARRAY' ? $res->{PLAYER} - : [$res->{PLAYER}]; + !exists $res->{PLAYER} ? [] + : ref $res->{PLAYER} eq 'ARRAY' ? $res->{PLAYER} + : [ $res->{PLAYER} ]; return $customers; } sub request { - my ($self, $url, $method, %params) = @_; + my ( $self, $url, $method, %params ) = @_; $method ||= 'GET'; my $http_tiny = $self->__http_tiny; - my $header = {Authorization => 'Basic ' . b64_encode($self->{user} . ':' . $self->{pass}, '')}; - my @extra = %params ? (form => \%params) : (); - my $response = $http_tiny->get($self->{host} . $url, {headers => $header}); + my $header = { Authorization => 'Basic ' + . b64_encode( $self->{user} . ':' . $self->{pass}, '' ) }; + my @extra = %params ? ( form => \%params ) : (); + my $response = + $http_tiny->get( $self->{host} . $url, { headers => $header } ); - unless ($response->{success}) { + unless ( $response->{success} ) { $errstr = $response->{reason}; return; } - if ($response->{headers}{"content-type"} and $response->{headers}{"content-type"} =~ 'text/xml') { - return XMLin($response->{content}); + if ( $response->{headers}{"content-type"} + and $response->{headers}{"content-type"} =~ 'text/xml' ) + { + return XMLin( $response->{content} ); } $errstr = $response->{content}; @@ -156,7 +179,7 @@ sub request { ## un-documented helper sub get_affiliate_id_from_token { - my ($self, $token) = @_; + my ( $self, $token ) = @_; $token or croak 'Must pass a token to get_affiliate_id_from_token.'; @@ -179,7 +202,7 @@ Returns token_info hash =cut sub get_affiliate_details { - my ($self, $token) = @_; + my ( $self, $token ) = @_; croak 'Must pass a token to get_affiliate_email_from_token.' unless $token; diff --git a/t/basic.t b/t/basic.t index f77f400..6b9b4c2 100644 --- a/t/basic.t +++ b/t/basic.t @@ -11,7 +11,8 @@ my $aff = WebService::MyAffiliates->new( ok($aff); throws_ok { $aff->decode_token() } qr/Must pass at least one token/; -throws_ok { $aff->get_affiliate_id_from_token() } qr/Must pass a token to get_affiliate_id_from_token/, - 'Throws exception if no token given.'; +throws_ok { $aff->get_affiliate_id_from_token() } +qr/Must pass a token to get_affiliate_id_from_token/, + 'Throws exception if no token given.'; done_testing; diff --git a/t/create_affiliate.t b/t/create_affiliate.t index abc6320..99b6bca 100644 --- a/t/create_affiliate.t +++ b/t/create_affiliate.t @@ -20,19 +20,19 @@ $mock_aff->mock( sub { return +{ 'INIT' => { - 'ERROR_COUNT' => 0, + 'ERROR_COUNT' => 0, 'WARNING_COUNT' => 0, - }}; + } + }; - }); + } +); -ok($aff->create_affiliate({}), 'A hashref is a valid parameter'); +ok( $aff->create_affiliate( {} ), 'A hashref is a valid parameter' ); -my $hash_params = ( - first_name => 'Dummy' -); +my $hash_params = ( first_name => 'Dummy' ); -ok($aff->create_affiliate({}), 'A hash is a valid parameter'); +ok( $aff->create_affiliate( {} ), 'A hash is a valid parameter' ); $mock_aff->mock( 'request', @@ -43,9 +43,12 @@ $mock_aff->mock( 'ERROR' => { 'MSG' => 'An account with this email already exists.', 'DETAIL' => 'email' - }}}; + } + } + }; - }); + } +); my $args = { 'first_name' => 'Charles', @@ -68,7 +71,7 @@ my $args = { my $res = $aff->create_affiliate($args); -ok(!$res, 'Returns undef in case of a single error'); +ok( !$res, 'Returns undef in case of a single error' ); $mock_aff->mock( 'request', @@ -76,20 +79,25 @@ $mock_aff->mock( return +{ 'INIT' => { 'ERROR_COUNT' => 2, - 'ERROR' => [{ - 'MSG' => 'An account with this email already exists.', + 'ERROR' => [ + { + 'MSG' => 'An account with this email already exists.', 'DETAIL' => 'email' }, { 'DETAIL' => 'username', 'MSG' => 'Username not available' - }]}}; + } + ] + } + }; - }); + } +); $res = $aff->create_affiliate($args); -ok(!$res, 'Returns undef in case of multiple errors'); +ok( !$res, 'Returns undef in case of multiple errors' ); $mock_aff->mock( 'request', @@ -100,13 +108,16 @@ $mock_aff->mock( 'ERROR' => { 'MSG' => 'An account with this email already exists.', 'DETAIL' => 'email' - }}}; + } + } + }; - }); + } +); $res = $aff->create_affiliate($args); -ok(!$res, 'Returns undef in case of single error'); +ok( !$res, 'Returns undef in case of single error' ); $mock_aff->mock( 'request', @@ -117,7 +128,8 @@ $mock_aff->mock( 'WARNING_COUNT' => 1, 'WARNING_DETAIL' => { 'DETAIL' => 'password', - 'MSG' => 'Setting a password for a new affiliate is optional and will be deprecated in future' + 'MSG' => +'Setting a password for a new affiliate is optional and will be deprecated in future' }, 'USERNAME' => 'charles_babbage', 'PASSWORD' => 's3cr3t', @@ -126,13 +138,15 @@ $mock_aff->mock( 'COUNTRY' => 'GB', 'LANGUAGE' => 0, 'EMAIL' => 'repeated@email.com' - }}; - }); + } + }; + } +); $res = $aff->create_affiliate($args); -ok($res, 'Returns the ref correctly'); -ok(!$res->{PASSWORD}, 'It does not return the password'); +ok( $res, 'Returns the ref correctly' ); +ok( !$res->{PASSWORD}, 'It does not return the password' ); done_testing(); diff --git a/t/decode_token.t b/t/decode_token.t index 72214bc..cceb04c 100644 --- a/t/decode_token.t +++ b/t/decode_token.t @@ -1,12 +1,16 @@ #!/usr/bin/perl use strict; + # use warnings; use WebService::MyAffiliates; use Test::More; -plan skip_all => "ENV MYAFFILIATES_USER/MYAFFILIATES_PASS/MYAFFILIATES_HOST is required to continue." - unless $ENV{MYAFFILIATES_USER} and $ENV{MYAFFILIATES_PASS} and $ENV{MYAFFILIATES_HOST}; +plan skip_all => +"ENV MYAFFILIATES_USER/MYAFFILIATES_PASS/MYAFFILIATES_HOST is required to continue." + unless $ENV{MYAFFILIATES_USER} + and $ENV{MYAFFILIATES_PASS} + and $ENV{MYAFFILIATES_HOST}; my $aff = WebService::MyAffiliates->new( user => $ENV{MYAFFILIATES_USER}, pass => $ENV{MYAFFILIATES_PASS}, @@ -14,19 +18,33 @@ my $aff = WebService::MyAffiliates->new( ); my $token_info = $aff->decode_token('PQ4YXsO2q5mVAv0U_Fv2nWNd7ZgqdRLk'); + # use Data::Dumper; # diag(Dumper(\$token_info)); -is(ref $token_info->{'TOKEN'}, 'HASH', 'We got data back about one token, so the ->{\'TOKEN\'} key is a hash ref.'); -ok($token_info->{TOKEN}->{USER}, 'USER exists'); - -my $token_info = $aff->decode_token('PQ4YXsO2q5mVAv0U_Fv2nWNd7ZgqdRLk', 'PQ4YXsO2q5mVAv0U_Fv2nWNd7ZgqdRLk'); -is(ref $token_info->{'TOKEN'}, 'ARRAY', 'Got data back about two tokens, so the ->{\'TOKEN\'} key is an array ref.'); +is( + ref $token_info->{'TOKEN'}, + 'HASH', + 'We got data back about one token, so the ->{\'TOKEN\'} key is a hash ref.' +); +ok( $token_info->{TOKEN}->{USER}, 'USER exists' ); + +my $token_info = $aff->decode_token( 'PQ4YXsO2q5mVAv0U_Fv2nWNd7ZgqdRLk', + 'PQ4YXsO2q5mVAv0U_Fv2nWNd7ZgqdRLk' ); +is( + ref $token_info->{'TOKEN'}, + 'ARRAY', + 'Got data back about two tokens, so the ->{\'TOKEN\'} key is an array ref.' +); -my $affiliate_id = $aff->get_affiliate_id_from_token('jGZUKO3JWgyVAv0U_Fv2nVOqZLGcUW5p'); -is($affiliate_id, 6, 'Token has affiliate id 6'); +my $affiliate_id = + $aff->get_affiliate_id_from_token('jGZUKO3JWgyVAv0U_Fv2nVOqZLGcUW5p'); +is( $affiliate_id, 6, 'Token has affiliate id 6' ); -my $affiliate_details = $aff->get_affiliate_details('jGZUKO3JWgyVAv0U_Fv2nVOqZLGcUW5p'); -is($affiliate_details->{TOKEN}->{USER}->{EMAIL}, 'dummy@regentmarkets.com', 'Token has affiliate email dummy@regentmarkets.com'); +my $affiliate_details = + $aff->get_affiliate_details('jGZUKO3JWgyVAv0U_Fv2nVOqZLGcUW5p'); +is( $affiliate_details->{TOKEN}->{USER}->{EMAIL}, + 'dummy@regentmarkets.com', + 'Token has affiliate email dummy@regentmarkets.com' ); done_testing(); diff --git a/t/get_customers.t b/t/get_customers.t index c3d9c3d..9455fc5 100644 --- a/t/get_customers.t +++ b/t/get_customers.t @@ -20,19 +20,19 @@ $mock_http_tiny->mock( sub { return +{ - 'headers' => {'content-type' => 'text/xml'}, + 'headers' => { 'content-type' => 'text/xml' }, 'success' => 1, 'content' => '' }; - }); + } +); -my $args = { - 'AFFILIATE_ID' => '1234', -}; +my $args = { 'AFFILIATE_ID' => '1234', }; my $res = $aff->get_customers($args); -is($res->[0]->{AFFILIATE_ID}, '1234', 'Affiliate customer is retrieved correctly'); +is( $res->[0]->{AFFILIATE_ID}, + '1234', 'Affiliate customer is retrieved correctly' ); done_testing(); diff --git a/t/get_users.t b/t/get_users.t index 93c6d2f..9c7cbed 100644 --- a/t/get_users.t +++ b/t/get_users.t @@ -1,12 +1,16 @@ #!/usr/bin/perl use strict; + # use warnings; use WebService::MyAffiliates; use Test::More; -plan skip_all => "ENV MYAFFILIATES_USER/MYAFFILIATES_PASS/MYAFFILIATES_HOST is required to continue." - unless $ENV{MYAFFILIATES_USER} and $ENV{MYAFFILIATES_PASS} and $ENV{MYAFFILIATES_HOST}; +plan skip_all => +"ENV MYAFFILIATES_USER/MYAFFILIATES_PASS/MYAFFILIATES_HOST is required to continue." + unless $ENV{MYAFFILIATES_USER} + and $ENV{MYAFFILIATES_PASS} + and $ENV{MYAFFILIATES_HOST}; my $aff = WebService::MyAffiliates->new( user => $ENV{MYAFFILIATES_USER}, pass => $ENV{MYAFFILIATES_PASS}, @@ -18,9 +22,9 @@ my $user_info = $aff->get_user(2); # use Data::Dumper; # diag(Dumper(\$user_info)); -is($user_info->{ID}, 2); -ok($user_info->{SUBSCRIPTIONS}); -ok($user_info->{USER_VARIABLES}); +is( $user_info->{ID}, 2 ); +ok( $user_info->{SUBSCRIPTIONS} ); +ok( $user_info->{USER_VARIABLES} ); done_testing(); diff --git a/t/get_users_status.t b/t/get_users_status.t new file mode 100644 index 0000000..d2df987 --- /dev/null +++ b/t/get_users_status.t @@ -0,0 +1,29 @@ +#!/usr/bin/perl + +use strict; + +# use warnings; +use WebService::MyAffiliates; +use Test::More; + +plan skip_all => +"ENV MYAFFILIATES_USER/MYAFFILIATES_PASS/MYAFFILIATES_HOST is required to continue." + unless $ENV{MYAFFILIATES_USER} + and $ENV{MYAFFILIATES_PASS} + and $ENV{MYAFFILIATES_HOST}; +my $aff = WebService::MyAffiliates->new( + user => $ENV{MYAFFILIATES_USER}, + pass => $ENV{MYAFFILIATES_PASS}, + host => $ENV{MYAFFILIATES_HOST} +); + +my $user_info = $aff->get_users_status_by_affiliate_id(2); + +is( $user_info->{ID}, 2 ); +is( $user_info->{STATUS}, 'denied' ); +ok( $user_info->{SUBSCRIPTIONS} ); +ok( $user_info->{USER_VARIABLES} ); + +done_testing(); + +1;