package HackaMol::Roles::FileFetchRole;
$HackaMol::Roles::FileFetchRole::VERSION = '0.051';
#ABSTRACT: Role for using LWP::Simple to fetch files from www 
use Moose::Role;
use Carp;
use Path::Tiny;
use HTTP::Tiny;
#use LWP::Simple;
use Data::Dumper;

has 'pdbserver',   is => 'rw', isa => 'Str', lazy => 1, default => '';
has 'overwrite',   is => 'rw', isa => 'Bool', lazy => 1, default => 0;

sub _fix_pdbid{
  my $pdbid = shift;
  $pdbid =~ s/\.pdb//; #just in case
  $pdbid .= '.pdb';
  return $pdbid;

sub get_pdbid{
  #return pdb contents downloaded from
  my $self = shift;
  my $pdbid = _fix_pdbid(shift);  
  my ($ok, $why) = HTTP::Tiny->can_ssl;
  if ($ok){
    my $pdb = HTTP::Tiny->new->get($self->pdbserver.$pdbid);
    return ( $pdb->{content} );
  else {
    warn "$why";
    return 0;

sub getstore_pdbid{
  #return array of lines from pdb downloaded from
  my $self = shift;
  my $pdbid =  _fix_pdbid(shift); 
  my $fpdbid = shift ;
  $fpdbid = $pdbid unless defined($fpdbid);
  $fpdbid = path($fpdbid);

  if ($fpdbid->exists and not $self->overwrite){
    carp "$fpdbid exists, set self->overwrite(1) to overwrite";
    return $fpdbid->stringify;
  my $pdb = $self->get_pdbid( $pdbid );
  if ($pdb){
    return ( $fpdbid->stringify );
    warn "could not connect, $fpdbid not written\n";
    return 0;

no Moose::Role;



=head1 NAME

HackaMol::Roles::FileFetchRole - Role for using LWP::Simple to fetch files from www 

=head1 VERSION

version 0.051


   use HackaMol;

   my $pdb = $HackaMol->new->get_pdbid("2cba");
   print $pdb;


FileFetchRole provides attributes and methods for pulling files from the internet.
Currently, the Role has one method and one attribute for interacting with the Protein Database.

=head1 METHODS

=head2 get_pdbid 

fetches a pdb from and returns the file in a string.

=head2 getstore_pdbid 

arguments: pdbid and filename for writing (optional). 
Fetches a pdb from and stores it in your working directory unless {it exists and overwrite(0)}. If a filename is not
passed to the method, it will write to $pdbid.pdb. use get_pdbid to return contents


=head2 overwrite    

isa lazy ro Bool that defaults to 0 (false).  If overwrite(1), then fetched files will be able to overwrite
those of same name in working directory.

=head2 pdbserver  

isa lazy rw Str that defaults to

=head1 SEE ALSO

=over 4

=item *


=item *



=head1 AUTHOR

Demian Riccardi <>


This software is copyright (c) 2017 by Demian Riccardi.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.