Catmandu-EuropePMC

 view release on metacpan or  search on metacpan

lib/Catmandu/Importer/EuropePMC.pm  view on Meta::CPAN

package Catmandu::Importer::EuropePMC;

our $VERSION = '1.011';

use Catmandu::Sane;
use Catmandu::Importer::XML;
use Try::Tiny;
use Furl;
use Moo;

with 'Catmandu::Importer';

use constant BASE_URL => 'https://www.ebi.ac.uk/europepmc/webservices/rest';

has base_url   => ( is => 'ro', default => sub { return BASE_URL; } );
has source => ( is => 'ro', default => sub { return "MED"; } );
has module => ( is => 'ro', default => sub { return "search"; } );
has query  => ( is => 'ro' );
has pmid   => ( is => 'ro' );
has db     => ( is => 'ro' );
has page   => ( is => 'ro' );
has raw => (is => 'ro');

sub BUILD {
    my $self = shift;

    Catmandu::BadVal->throw("Either 'pmid' or 'query' is required.")
        unless $self->pmid || $self->query;

}

my %PATH_MAPPING = (
    search        => 'result',
    citations     => 'citation',
    references    => 'reference',
    databaseLinks => 'dbCrossReference'
);

sub _request {
    my ( $self, $url ) = @_;

    my $ua = Furl->new( timeout => 20 );

    my $res;
    try {
        $res = $ua->get($url);
        #die $res->status_line unless $res->is_success;

        $res->is_success ? return $res->content : return '';
    }
    catch {
        Catmandu::Error->throw("Status code: $res->status_line");
    };

}

sub _parse {
    my ( $self, $in ) = @_;

    my $path;
    ($self->raw) ? ($path = '')
        : ($path = $PATH_MAPPING{ $self->module });
    my $xml = Catmandu::Importer::XML->new( file => \$in, path => $path );

    return $xml->to_array;
}



( run in 1.311 second using v1.01-cache-2.11-cpan-5c0b1e786e0 )