Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
try to fetch default production version from clientinfo.metacpan.org
  • Loading branch information
mickeyn committed Aug 28, 2016
1 parent 6f142f9 commit 34eb148
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion lib/MetaCPAN/Client/Request.pm
Expand Up @@ -9,6 +9,7 @@ use JSON::MaybeXS qw<decode_json encode_json>;
use Search::Elasticsearch;
use Try::Tiny;
use HTTP::Tiny;
use Ref::Util qw< is_hashref >;

has domain => (
is => 'ro',
Expand All @@ -18,8 +19,19 @@ has domain => (
);

has version => (
is => 'ro',
required => 1,
default => sub {
my $info = $_[0]->_clientinfo;
$info or return 'v0'; # last known production version
$info->{production}{version};
},
);

has _clientinfo => (
is => 'ro',
default => sub { 'v0' },
lazy => 1,
builder => '_build_clientinfo',
);

has base_url => (
Expand Down Expand Up @@ -75,6 +87,20 @@ sub _build_ua {
return HTTP::Tiny->new( @{ $self->ua_args } );
}

sub _build_clientinfo {
my $self = shift;

my $info;
eval {
$info = $self->ua->get( 'https://clientinfo.metacpan.org' );
$info = decode_json( $info->{content} );
is_hashref($info) and exists $info->{production} or die;
1;
} or return undef;

return $info;
}

sub fetch {
my $self = shift;
my $url = shift or croak 'fetch must be called with a URL parameter';
Expand Down

0 comments on commit 34eb148

Please sign in to comment.