Skip to content

Commit

Permalink
improve version input + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mickeyn committed Aug 27, 2016
1 parent 607a9c1 commit c663ba1
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
10 changes: 7 additions & 3 deletions lib/MetaCPAN/Client.pm
Expand Up @@ -32,12 +32,16 @@ sub BUILDARGS {
my ( $class, %args ) = @_;

# default 'domain' for 'version => v0|v1'
if ( exists $ENV{METACPAN_VERSION} or exists $args{version} ) {
my $version = $ENV{METACPAN_VERSION} || $args{version};
$version and $args{domain} //=
my $version = exists $ENV{METACPAN_VERSION} ? $ENV{METACPAN_VERSION} : undef;
$version //= exists $args{version} ? $args{version} : undef;
if ( defined $version ) {
$version =~ s/^(?!v)/v/;
$version eq 'v0' or $version eq 'v1' or croak "invalid version number";
$args{domain} //=
$version eq 'v0' ? 'api.metacpan.org' :
$version eq 'v1' ? 'fastapi.metacpan.org' :
undef;
$args{version} = $version;
}

$args{'request'} ||= MetaCPAN::Client::Request->new(
Expand Down
4 changes: 1 addition & 3 deletions lib/MetaCPAN/Client/Request.pm
Expand Up @@ -19,9 +19,7 @@ has domain => (

has version => (
is => 'ro',
default => sub {
return ( $ENV{METACPAN_VERSION} ? $ENV{METACPAN_VERSION} : 'v0' );
},
default => sub { 'v0' },
);

has base_url => (
Expand Down
10 changes: 5 additions & 5 deletions t/request.t
Expand Up @@ -7,7 +7,7 @@ use Test::More tests => 9;
use MetaCPAN::Client;
use MetaCPAN::Client::Request;

my $req = MetaCPAN::Client::Request->new( domain => 'mydomain', version => 'z' );
my $req = MetaCPAN::Client::Request->new( domain => 'mydomain', version => 'v1' );
isa_ok( $req, 'MetaCPAN::Client::Request' );
can_ok(
$req,
Expand All @@ -17,8 +17,8 @@ can_ok(
);

is( $req->domain, 'mydomain', 'Correct domain' );
is( $req->version, 'z', 'Correct version' );
is( $req->base_url, 'https://mydomain/z', 'Correct base_url' );
is( $req->version, 'v1', 'Correct version' );
is( $req->base_url, 'https://mydomain/v1', 'Correct base_url' );
isa_ok( $req->ua, 'HTTP::Tiny' );

my $ver = $MetaCPAN::Client::VERSION || 'xx';
Expand All @@ -28,6 +28,6 @@ is_deeply(
'Correct UA args',
);

my $client = MetaCPAN::Client->new( domain => 'foo', version => 'bar' );
my $client = MetaCPAN::Client->new( domain => 'foo', version => 'v1' );
is ( $client->request->domain, 'foo', 'domain set in request' );
is ( $client->request->version, 'bar', 'version set in request' );
is ( $client->request->version, 'v1', 'version set in request' );

0 comments on commit c663ba1

Please sign in to comment.