| 1 | package TaskPipe::UserAgentManager::UserAgentHandler; | ||
| 2 | |||
| 3 | 1 | ▶ | use Moose; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use Module::Runtime 'require_module'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | with 'MooseX::ConfigCascade'; | |
| 6 | 1 | with 'TaskPipe::Role::MooseType_UserAgentType'; | |
| 7 | |||
| 8 | has settings => (is => 'ro', isa => __PACKAGE__.'::Settings', default => sub{ | ||
| 9 | 0 | ▶ | my $module = __PACKAGE__.'::Settings'; |
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||
| 10 | 0 | require_module( $module ); | |
| 11 | 0 | $module->new; | |
| 12 | 1 | }); | |
| 13 | |||
| 14 | 1 | has ua => (is => 'rw', isa => 'UserAgentType', lazy => 1, builder => 'build_ua'); | |
| 15 | 1 | has gm => (is => 'rw', isa => 'TaskPipe::SchemaManager'); | |
| 16 | has run_info => (is => 'rw', isa => 'TaskPipe::RunInfo', default => sub{ | ||
| 17 | 0 | ▶ | TaskPipe::RunInfo->new; |
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||
| 18 | 1 | }); | |
| 19 | has json_encoder => (is => 'ro', isa => 'JSON', default => sub{ | ||
| 20 | 0 | ▶ | my $json_enc = JSON->new; |
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||
| 21 | 0 | $json_enc->canonical; | |
| 22 | 0 | return $json_enc; | |
| 23 | 1 | }); | |
| 24 | |||
| 25 | |||
| 26 | |||
| 27 | sub build_ua{ | ||
| 28 | 0 | ▶ | my $self = shift; |
|
Subroutine
build_ua: not called CC=2 SCAR=17.9
Pod
undocumented
| |||
| 29 | |||
| 30 | 0 | my $ua = LWP::UserAgent->new( ssl_opts => { verify_hostname => 0 } ); | |
| 31 | 0 | $ua->cookie_jar( {} ); | |
| 32 | 0 | $ua->agent( $self->settings->agent ); | |
| 33 | 0 | $ua->timeout( $self->settings->timeout ); | |
| 34 | 0 | foreach my $header_name ( %{$self->settings->headers} ){ | |
| 35 | 0 | $ua->default_header($header_name => $self->settings->headers->{$header_name} ); | |
| 36 | } | ||
| 37 | |||
| 38 | 0 | return $ua; | |
| 39 | } | ||
| 40 | |||
| 41 | sub call{ | ||
| 42 | 0 | ▶ | my ($self,$method,@params) = @_; |
|
Subroutine
call: not called CC=1 SCAR=6.9
Pod
undocumented
| |||
| 43 | |||
| 44 | 0 | my $logger = Log::Log4perl->get_logger; | |
| 45 | 0 | $self->clear_cookies; | |
| 46 | 0 | $logger->info(uc($method)." ".$self->json_encoder->encode(\@params)); | |
| 47 | 0 | my $resp = $self->ua->$method(@params); | |
| 48 | |||
| 49 | 0 | return $resp; | |
| 50 | } | ||
| 51 | |||
| 52 | |||
| 53 | |||
| 54 | sub clear_cookies{ | ||
| 55 | 0 | ▶ | my ($self) = @_; |
|
Subroutine
clear_cookies: not called CC=1 SCAR=6.9
Pod
undocumented
| |||
| 56 | |||
| 57 | 0 | $self->ua->cookie_jar({}); | |
| 58 | } | ||
| 59 | |||
| 60 | =head1 NAME | ||
| 61 | |||
| 62 | TaskPipe::UserAgentManager::UserAgentHandler - standard useragent handler | ||
| 63 | |||
| 64 | =head1 DESCRIPTION | ||
| 65 | |||
| 66 | This is the base useragent handler module, which defines the useragent. You can tell L<TaskPipe::Task_Scrape> to use this module, by specifying | ||
| 67 | |||
| 68 | ua_handler_module: TaskPipe::UserAgentManager::UserAgentHandler | ||
| 69 | |||
| 70 | in the L<TaskPipe::Task_Scrape::Settings> section of the project config | ||
| 71 | |||
| 72 | =head1 AUTHOR | ||
| 73 | |||
| 74 | Tom Gracey <tomgracey@gmail.com> | ||
| 75 | |||
| 76 | =head1 COPYRIGHT AND LICENSE | ||
| 77 | |||
| 78 | Copyright (c) Tom Gracey 2018 | ||
| 79 | |||
| 80 | TaskPipe is free software, licensed under | ||
| 81 | |||
| 82 | The GNU Public License Version 3 | ||
| 83 | |||
| 84 | =cut | ||
| 85 | |||
| 86 | 1 | __PACKAGE__->meta->make_immutable; | |
| 87 | 1 | 1; | |