Skip to content

Commit

Permalink
Merge pull request #535 from metacpan/mickey/scripts_ua_and_proxy
Browse files Browse the repository at this point in the history
script role: ua attribute (lazy) + proxy attribute + cleanup
  • Loading branch information
oalders committed Nov 11, 2016
2 parents b47d34a + 1decb59 commit 9f8729d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
30 changes: 28 additions & 2 deletions lib/MetaCPAN/Role/Script.pm
Expand Up @@ -13,7 +13,7 @@ use MetaCPAN::Queue ();
use Moose::Role;
use Carp ();

has 'cpan' => (
has cpan => (
is => 'ro',
isa => Dir,
lazy => 1,
Expand All @@ -30,9 +30,21 @@ has die_on_error => (
documentation => 'Die on errors instead of simply logging',
);

has ua => (
is => 'ro',
lazy => 1,
builder => '_build_ua',
);

has proxy => (
is => 'ro',
isa => Str,
default => '',
);

has es => (
isa => ES,
is => 'ro',
isa => ES,
required => 1,
coerce => 1,
documentation => 'Elasticsearch http connection string',
Expand Down Expand Up @@ -108,6 +120,20 @@ sub _build_model {
return MetaCPAN::Model->new( es => $self->es );
}

sub _build_ua {
my $self = shift;
my $ua = LWP::UserAgent->new;
my $proxy = $self->proxy;

if ($proxy) {
$proxy eq 'env'
? $ua->env_proxy
: $ua->proxy( [qw<http https>], $proxy );
}

return $ua;
}

sub _build_cpan {
my $self = shift;
my @dirs = (
Expand Down
7 changes: 1 addition & 6 deletions lib/MetaCPAN/Script/Ratings.pm
Expand Up @@ -18,18 +18,13 @@ has ratings => (

sub run {
my $self = shift;
my $ua = LWP::UserAgent->new;

if ( my $proxy = $ENV{http_proxy} || $ENV{HTTP_PROXY} ) {
$ua->proxy( ['http'], $proxy );
}

log_info { 'Downloading ' . $self->ratings };

my @path = qw( var tmp ratings.csv );
my $target = $self->home->file(@path);
my $md5 = -e $target ? $self->digest($target) : 0;
my $res = $ua->mirror( $self->ratings, $target );
my $res = $self->ua->mirror( $self->ratings, $target );
if ( $md5 eq $self->digest($target) ) {
log_info {'No changes to ratings.csv'};
return;
Expand Down
5 changes: 0 additions & 5 deletions lib/MetaCPAN/Script/Tickets.pm
Expand Up @@ -46,11 +46,6 @@ has source => (
default => sub { [qw(rt github)] },
);

has ua => (
is => 'ro',
default => sub { LWP::UserAgent->new },
);

has pithub => (
is => 'ro',
isa => 'Pithub',
Expand Down

0 comments on commit 9f8729d

Please sign in to comment.