Acme-CatFS

 view release on metacpan or  search on metacpan

lib/Acme/CatFS.pm  view on Meta::CPAN

  format  => 's',
  default => sub { 'cat.jpg' },
  doc     => 'name of the file (default is cat.jpg)',
);

option forking => (
  is  => 'ro',
  doc => 'if enable, will fork and exit (default false)',
);

option debug => (
  is  => 'ro',
  doc => 'if enable, will run Fuse::Simple in debug mode (default false)',
);

option cached => (
  is  => 'ro',
  doc => 'if enable, will cached the picture instead choose another each open (default false)',
);

sub _get_cat_picture {
  my $self = shift;
  state $cached_content;
  
  if($self->cached && $cached_content){
    return $cached_content;
  }

  my $content = try { 
    LWP::Simple::get($self->cat_url) 
  } catch {
    carp $_ if $self->debug;
  };

  if($self->cached){
    $cached_content = $content
  }

  $content
}

sub run {

lib/Acme/CatFS.pm  view on Meta::CPAN

    fork and exit  
  }

  my $mountpoint = $self->mountpoint;
  my $cat_file   = $self->cat_file;

  say "Initializing Fuse mountpoint '$mountpoint'... ";

  Fuse::Simple::main(
    mountpoint => $mountpoint,
    debug      => $self->debug,
    '/'        => {
      $cat_file => sub {
        $self->_get_cat_picture
      },
    },
  );
}

END {
   say "Don't forget run 'fusermount -u <mountpoint>'"
}

=head1 NAME

Acme::CatFS

=head1 SYNOPSIS

  Acme::CatFS->new(mountpoint => '/tmp/catfs', debug => 0, cat_file => 'kitten.jpg')->run();

=head1 DESCRIPTION

Acme::CatFS will create a Fuse mountpoint and generate one virtual file, a random image of a cat. Will return a different image each time.

It is the equivalent to:

  Fuse::Simple::main(
    mountpoint => $mountpoint,
    "/"        => {

lib/Acme/CatFS.pm  view on Meta::CPAN

Specify the directory mountpoint for Fuse. Should be an empty directory.

=head2 cat_url

Specify the url for the random pic of cat. Default is 'thecatapi.com' service.

=head2 cat_file

Specify the name of the file. Default is 'cat.jpg'

=head2 debug

If true, will run Fuse::Simple::main in debug mode.

=head2 forking

If true, we will fork then exit.

=head2 cached

if true, we will cache the cat picture instead download a new one.

=head1 SEE ALSO

t/01-basic.t  view on Meta::CPAN

      $random_cat_pic = shift;

      'random'
    };

  mock 'Fuse::Simple'
    => method 'main'
    => should {
      my (%params) = @_;

      ok ! $params{debug}, 'debug should be false';
      is $params{mountpoint}, '/', 'mountpoint should be /';
      is ref($params{'/'}->{'cat.jpg'}), 'CODE', 'cat.jpg should be a CODEREF';
      is $params{'/'}->{'cat.jpg'}->(), 'random', 'CODEREF should call LWP::Simple::get';
    };

   my $catfs = Acme::CatFS->new(mountpoint => '/');

   $catfs->run;

   is $random_cat_pic, $catfs->cat_url, 'should call LWP::Simple::get with cat_url';



( run in 1.294 second using v1.01-cache-2.11-cpan-49f99fa48dc )