summaryrefslogtreecommitdiff
path: root/BatchPayment/Transport/File.pm
blob: 27bc241e338af299a7fe6163083bc1374a8d0ba0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package Business::BatchPayment::Transport::File;

=head2 File transport

The simplest case.  Takes two arguments, 'input' and 'output'.  These can 
be open filehandles or strings naming files.  If unspecified, they default 
to /dev/null.

=cut

use strict;
use Moose;
use IO::File;
with 'Business::BatchPayment::Transport';

has 'input' => (
  is => 'rw',
  isa => 'Maybe[FileHandle|Str]',
  default => sub {
    warn "no input passed to file transport; using /dev/null";
    '/dev/null'
  },
  #lazy => 1,
);

has 'output' => (
  is => 'rw',
  isa => 'Maybe[FileHandle|Str]',
  default => sub {
    warn "no output passed to file transport; using /dev/null";
    '/dev/null'
  },
  #lazy => 1,
);

sub upload {
  my $self = shift;
  my $text = shift;
  my $fh;
  if ( ref $self->output ) {
    $fh = $self->output;
  } else {
    $fh = IO::File->new();
    $fh->open($self->output,'>')
      or die "couldn't write to ".$self->output;
  }
  print $fh $text;
}

sub download {
  my $self = shift;
  my $fh;
  if ( ref $self->input ) {
    $fh = $self->input;
  } else {
    $fh = IO::File->new();
    $fh->open($self->input,'<')
      or die "couldn't read from ".$self->input;
  }
  local $/;
  my $text = <$fh>;
}

1;