This repository has been archived on 2023-08-16. You can view files and clone it, but cannot push or open issues or pull requests.
Foodloop-Server/lib/Pear/LocalLoop/Import/Role/CSV.pm

91 lines
1.9 KiB
Perl
Raw Permalink Normal View History

2019-07-02 14:21:01 +00:00
package Pear::LocalLoop::Import::Role::CSV;
use strict;
use warnings;
use Moo::Role;
use Text::CSV;
2019-07-05 16:56:21 +00:00
use Try::Tiny;
use Pear::LocalLoop::Error;
2019-07-02 14:21:01 +00:00
requires 'csv_required_columns';
has csv_file => (
2021-03-20 12:09:50 +00:00
is => 'ro',
predicate => 1,
2019-07-05 14:30:31 +00:00
);
has csv_string => (
2021-03-20 12:09:50 +00:00
is => 'ro',
predicate => 1,
2019-07-02 14:21:01 +00:00
);
2019-07-05 16:56:21 +00:00
has csv_error => (
2021-03-20 12:09:50 +00:00
is => 'ro',
predicate => 1,
2019-07-05 16:56:21 +00:00
);
2019-07-02 14:21:01 +00:00
has _csv_filehandle => (
2021-03-20 12:09:50 +00:00
is => 'lazy',
builder => sub {
my $self = shift;
my $fh;
if ( $self->has_csv_file ) {
open $fh, '<', $self->csv_file;
}
elsif ( $self->has_csv_string ) {
my $string = $self->csv_string;
open $fh, '<', \$string;
}
else {
die "Must provide csv_file or csv_string";
}
return $fh;
2019-07-05 14:30:31 +00:00
}
2019-07-02 14:21:01 +00:00
);
has text_csv_options => (
2021-03-20 12:09:50 +00:00
is => 'lazy',
builder => sub {
return {
binary => 1,
allow_whitespace => 1,
};
}
2019-07-02 14:21:01 +00:00
);
has _text_csv => (
2021-03-20 12:09:50 +00:00
is => 'lazy',
builder => sub {
return Text::CSV->new( shift->text_csv_options );
}
2019-07-02 14:21:01 +00:00
);
has csv_data => (
2021-03-20 12:09:50 +00:00
is => 'lazy',
builder => sub {
my $self = shift;
my $header_check = $self->check_headers;
return 0 unless $header_check;
return $self->_text_csv->getline_hr_all( $self->_csv_filehandle );
}
2019-07-02 14:21:01 +00:00
);
sub get_csv_line {
2021-03-20 12:09:50 +00:00
my $self = shift;
return $self->_text_csv->getline_hr( $self->_csv_filehandle );
}
2019-07-05 14:30:31 +00:00
sub check_headers {
2021-03-20 12:09:50 +00:00
my $self = shift;
my $req_headers = $self->csv_required_columns;
my @headers;
@headers = $self->_text_csv->header( $self->_csv_filehandle );
my %header_map = ( map { $_ => 1 } @headers );
for my $req_header (@$req_headers) {
next if $header_map{$req_header};
die "Require header [" . $req_header . "]";
}
return 1;
2019-07-05 14:30:31 +00:00
}
1;