Skip to content

Commit

Permalink
Added the ability to include or exclude specific sites
Browse files Browse the repository at this point in the history
  • Loading branch information
davorg committed Mar 29, 2024
1 parent 79e3476 commit f646495
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 3 deletions.
1 change: 1 addition & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ MYMETA.yml
README.md
t/00-load.t
t/01-basic.t
t/02-include-exclude.t
t/pod.t
t/pod_coverage.t
33 changes: 30 additions & 3 deletions lib/Amazon/Sites.pm
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,38 @@ use Feature::Compat::Class;
use feature 'signatures';
no warnings 'experimental::signatures';

our $VERSION = '0.0.5';
our $VERSION = '0.1.0';

class Amazon::Sites {
use Amazon::Site;

field %sites = _init_sites();
field $include :param = [];
field $exclude :param = [];
field %sites = _init_sites($include, $exclude);

ADJUST {
if (@$include and @$exclude) {
die "You can't specify both include and exclude";
}
}

=head1 METHODS
=head2 new
Creates a new Amazon::Sites object.
my $sites = Amazon::Sites->new;
You can also specify a list of sites to include or exclude:
# Only include the US site
my $sites = Amazon::Sites->new(include => [ 'US' ]);
# Exclude the US site
my $sites = Amazon::Sites->new(exclude => [ 'US' ]);
At most one of `include` or `exclude` can be specified.
=head2 sites_hash
Returns a hash where the keys are the two-letter country codes and the values are
Expand Down Expand Up @@ -73,17 +92,25 @@ Returns a list of L<Amazon::Site> objects, sorted by the sort order.
return \@sites;
}

sub _init_sites {
sub _init_sites($include, $exclude) {
my %sites;
my @cols = qw[code country tldn currency sort];

my $where = tell DATA;

while (<DATA>) {
chomp;
my %site;
@site{@cols} = split;

next if @$include and not grep { $site{code} eq $_ } @$include;
next if @$exclude and grep { $site{code} eq $_ } @$exclude;

$sites{$site{code}} = Amazon::Site->new(%site);
}

seek DATA, $where, 0;

return %sites;
}

Expand Down
31 changes: 31 additions & 0 deletions t/02-include-exclude.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use strict;
use warnings;

use Test::More;
use Test::Exception;

Check failure on line 5 in t/02-include-exclude.t

View workflow job for this annotation

GitHub Actions / test / Perl 5.34 on ubuntu-latest

Can't locate Test/Exception.pm in @inc (you may need to install the Test::Exception module) (@inc contains: /home/runner/work/amazon-sites/amazon-sites/lib /home/runner/work/amazon-sites/amazon-sites/local/lib/perl5 /home/runner/work/amazon-sites/amazon-sites/local/lib/perl5/x86_64-linux /home/runner/work/_actions/shogo82148/actions-setup-perl/v1/scripts/lib /opt/hostedtoolcache/perl/5.34.3/x64/lib/site_perl/5.34.3/x86_64-linux /opt/hostedtoolcache/perl/5.34.3/x64/lib/site_perl/5.34.3 /opt/hostedtoolcache/perl/5.34.3/x64/lib/5.34.3/x86_64-linux /opt/hostedtoolcache/perl/5.34.3/x64/lib/5.34.3)

Check failure on line 5 in t/02-include-exclude.t

View workflow job for this annotation

GitHub Actions / test / Perl 5.34 on ubuntu-latest

BEGIN failed--compilation aborted

Check failure on line 5 in t/02-include-exclude.t

View workflow job for this annotation

GitHub Actions / test / Perl 5.36 on ubuntu-latest

Can't locate Test/Exception.pm in @inc (you may need to install the Test::Exception module) (@inc contains: /home/runner/work/amazon-sites/amazon-sites/lib /home/runner/work/amazon-sites/amazon-sites/local/lib/perl5 /home/runner/work/amazon-sites/amazon-sites/local/lib/perl5/x86_64-linux /home/runner/work/_actions/shogo82148/actions-setup-perl/v1/scripts/lib /opt/hostedtoolcache/perl/5.36.3/x64/lib/site_perl/5.36.3/x86_64-linux /opt/hostedtoolcache/perl/5.36.3/x64/lib/site_perl/5.36.3 /opt/hostedtoolcache/perl/5.36.3/x64/lib/5.36.3/x86_64-linux /opt/hostedtoolcache/perl/5.36.3/x64/lib/5.36.3)

Check failure on line 5 in t/02-include-exclude.t

View workflow job for this annotation

GitHub Actions / test / Perl 5.36 on ubuntu-latest

BEGIN failed--compilation aborted

Check failure on line 5 in t/02-include-exclude.t

View workflow job for this annotation

GitHub Actions / test / Perl 5.38 on ubuntu-latest

Can't locate Test/Exception.pm in @inc (you may need to install the Test::Exception module) (@inc entries checked: /home/runner/work/amazon-sites/amazon-sites/lib /home/runner/work/amazon-sites/amazon-sites/local/lib/perl5 /home/runner/work/amazon-sites/amazon-sites/local/lib/perl5/x86_64-linux /home/runner/work/_actions/shogo82148/actions-setup-perl/v1/scripts/lib /opt/hostedtoolcache/perl/5.38.2/x64/lib/site_perl/5.38.2/x86_64-linux /opt/hostedtoolcache/perl/5.38.2/x64/lib/site_perl/5.38.2 /opt/hostedtoolcache/perl/5.38.2/x64/lib/5.38.2/x86_64-linux /opt/hostedtoolcache/perl/5.38.2/x64/lib/5.38.2)

Check failure on line 5 in t/02-include-exclude.t

View workflow job for this annotation

GitHub Actions / test / Perl 5.38 on ubuntu-latest

BEGIN failed--compilation aborted

use Amazon::Sites;

throws_ok { Amazon::Sites->new( include => [ 'UK' ], exclude => [ 'US' ]) }
qr[You can't specify both include and exclude],
'Can\'t specify both include and exclude';

my $sites = Amazon::Sites->new(exclude => [ 'US' ]);
my $az_us = $sites->site('US');
ok(! $az_us, 'US is excluded');
my $az_uk = $sites->site('UK');
ok($az_uk, 'UK is included');
is_deeply([ $sites->codes ],
[ qw(AE AU BE BR CA CN DE EG ES FR IN IT JP MX NL PL SA SE SG TR UK) ],
'Correct codes are included');

$sites = Amazon::Sites->new(include => [ 'UK' ]);
$az_us = $sites->site('US');
ok(! $az_us, 'US is excluded');
$az_uk = $sites->site('UK');
ok($az_uk, 'UK is included');
is_deeply([ $sites->codes ],
[ qw(UK) ],
'Correct code is included');

done_testing;

0 comments on commit f646495

Please sign in to comment.