blib/lib/Parse/USDA/NNDB.pm

filter: Statementstmt 26.4% 32 / 121 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 55.5% 10 / 18 Podpod 100% 5 / 5 totaltotal 25.8% 47 / 182 CC 17 SCAR 29.0
CC
17
Cov
21%
get_line
42.0
open_file
30.0
_fetch_data
30.0
CRAP 18.1
↑ Summary
1 package Parse::USDA::NNDB;
2 {
3 1 $Parse::USDA::NNDB::VERSION = '0.1';
4 }
5  
6 # ABSTRACT: download and parse the latest USDA National Nutrient Database
7  
8 1 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use Text::CSV_XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Archive::Zip qw/ :ERROR_CODES :CONSTANTS /;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use File::HomeDir;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use File::Fetch;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Log::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 # XXX file encoding
21 # TODO use the updates rather than a whole new db
22 # XXX option to download old releases?
23 # TODO progress bars... hardcode lines per file as they will only change once per year...
24  
25 0 ▶sub new {
Subroutine new: not called CC=3 SCAR=24.8
Pod documented
26 0 my ( $this, $base_dir ) = @_;
27 0 ▶ my $class = ref( $this ) || $this;
Condition0%0/3
ref $this || $this
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table0%0/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
28  
29 # TODO better cross-platform defaults
30 0 ▶ if ( !defined $base_dir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $base_dir) 0 0 0
31 0 $base_dir = File::Spec->catdir( File::HomeDir->my_home, '.cache/usda_nndb' );
32 }
33  
34 # TODO set up base dir
35 0 my $self = {
36 base_dir => $base_dir,
37 data_dir => File::Spec->catdir( $base_dir, 'sr24' ),
38 data_uri => URI->new( 'http://www.ars.usda.gov/SP2UserFiles/Place/12354500/Data/SR24/dnload/sr24.ZIP' ),
39 zip_file => File::Spec->catfile( $base_dir, 'sr24.ZIP' ),
40 logger => Log::Any->get_logger( category => __PACKAGE__ ),
41 };
42  
43 0 bless $self, $class;
44 0 return $self;
45 }
46  
47 0 ▶sub _get_file_path_for {
Subroutine _get_file_path_for: not called CC=1 SCAR=6.9
48 0 my ( $self, $table ) = @_;
49  
50 0 my $file_path = File::Spec->catfile( $self->{data_dir}, $table . ".txt" );
51 0 $self->{logger}->debug( "Using path [$file_path] for '$table'" );
52 0 return $file_path;
53 }
54  
55 0 ▶sub tables {
Subroutine tables: not called CC=1 SCAR=6.9
Pod documented
56 0 return qw/DATSRCLN FD_GROUP LANGUAL LANGDESC FOOTNOTE NUTR_DEF SRC_CD DATA_SRC DERIV_CD FOOD_DES NUT_DATA WEIGHT/;
57 }
58  
59 0 ▶sub get_columns_for {
Subroutine get_columns_for: not called CC=1 SCAR=6.9
Pod documented
60 0 my ( $self, $table ) = @_;
61  
62 0 given ( $table ) {
63 0 when ( /^FOOD_DES$/i ) {
64 return [
65 0 qw/NDB_No FdGrp_Cd Long_Desc Shrt_Desc ComName ManufacName Survey Ref_desc Refuse SciName N_Factor Pro_Factor Fat_Factor CHO_Factor/
66 ];
67 }
68 0 when ( /^FD_GROUP$/i ) {
69 0 return [qw/FdGrp_Cd FdGrp_Desc/];
70 }
71 0 when ( /^LANGUAL$/i ) {
72 0 return [qw/NDB_No Factor_Code/];
73 }
74 0 when ( /^LANGDESC$/i ) {
75 0 return [qw/Factor_Code Description/];
76 }
77 0 when ( /^NUT_DATA$/i ) {
78 return [
79 0 qw/NDB_No Nutr_No Nutr_Val Num_Data_Pts Std_Error Src_Cd Deriv_Cd Ref_NDB_No Add_Nutr_Mark Num_Studies Min Max DF Low_EB Up_EB Stat_cmt CC/
80 ];
81  
82 }
83 0 when ( /^NUTR_DEF$/i ) {
84 0 return [qw(Nutr_No Units Tagname NutrDesc Num_Dec SR_Order)];
85 }
86 0 when ( /^SRC_CD$/i ) {
87 0 return [qw(Src_Cd SrcCd_Desc)];
88 }
89 0 when ( /^DERIV_CD$/i ) {
90 0 return [qw(Deriv_Cd Deriv_Desc)];
91 }
92 0 when ( /^WEIGHT$/i ) {
93 0 return [qw(NDB_No Seq Amount Msre_Desc Gm_Wgt Num_Data_Pts Std_Dev)];
94 }
95 0 when ( /^FOOTNOTE$/i ) {
96 0 return [qw(NDB_No Footnt_No Footnt_Typ Nutr_No Footnt_Txt)];
97 }
98 0 when ( /^DATSRCLN$/i ) {
99 0 return [qw(NDB_No Nutr_No DataSrc_ID)];
100 }
101 0 when ( /^DATA_SRC$/i ) {
102 0 return [qw(DataSrc_ID Authors Title Year Journal Vol_City Issue_State Start_Page End_Page)];
103 }
104  
105 #when ( /^ABBREV$/i ) {
106 # return [
107 # qw(NDB_No Shrt_Desc Water Energ_Kcal Protein Lipit_Tot Ash Carbonhydrt Fiber_TD Sugar_Tot Calcium Iron Magnesium Phosphorus Potassium Sodium Zinc Copper Manganese Selenium Vit_C Thiamin Riboflavin Niacin Panto_acid Vit_B6 Folate_Tot Folic_acid Food_Folate Folate_DFE Choline_total Vit_B12 Vit_A_IU Vit_A_RAE Retinol Alpha_Carot Beta_Carot Beta_Crypt Lycopene Lut_and_Zea Vit_E Vit_K FA_Sat FA_Mono FA_Poly Cholestrl GmWt_1 GmWt_Desc1 GmWt_2 GmWt_Desc2 Refuse_Pct)
108 # ];
109 #}
110 0 default {
111 0 warn "Unknown table [$table] requested\n";
112 0 return;
113 }
114 }
115 }
116  
117 0 ▶sub open_file {
Subroutine open_file: not called CC=5 SCAR=34.0
Pod documented
118 0 my ( $self, $table ) = @_;
119  
120 0 my $csv = Text::CSV_XS->new( {
121 quote_char => '~',
122 sep_char => '^',
123 allow_loose_escapes => 1,
124 empty_is_undef => 1,
125 binary => 1,
126 } );
127 0 my $file_path = $self->_get_file_path_for( $table );
128  
129 0 ▶ if ( !-e $file_path ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $file_path) 0 0 0
130 0 ▶ $self->_fetch_data
Branch: 1 branch
BranchTrueFalseTotal
$self->_fetch_data or return 0 0 0 0
131 or return 0;
132 }
133  
134 0 open my $fh, '<:encoding(iso-8859-1)', $file_path;
135  
136 # TODO better error handling!
137 0 ▶ if ( !$fh ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh) 0 0 0
138 0 my $err = $self->{logger}->crit( "Could not open [$file_path]: $!" );
139 0 die;
140 }
141  
142 0 ▶ my $column_names = $self->get_columns_for( $table )
Branch: 1 branch
BranchTrueFalseTotal
my $column_names = $self->get_columns_for($table) or return 0 0 0 0
143 or return 0;
144  
145 0 $csv->column_names( $column_names );
146 0 $self->{fh} = $fh;
147 0 $self->{csv} = $csv;
148  
149 0 return 1;
150 }
151  
152 0 ▶sub get_line {
Subroutine get_line: not called CC=6 SCAR=37.4
Pod documented
153 0 my $self = shift;
154  
155 0 ▶ if ( !defined $self->{fh} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"fh"}) 0 0 0
156 0 die "No active filehandle. Did you call open_file first?\n";
157 }
158 0 ▶ if ( !defined $self->{csv} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"csv"}) 0 0 0
159 0 die "No csv object. Did you call open_file first?\n";
160 }
161  
162 0 my $row = $self->{csv}->getline_hr( $self->{fh} );
163  
164 0 ▶ if ( $self->{csv}->eof ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"csv"}->eof) 0 0 0
165 0 $self->{logger}->debug( 'Closing file' );
166 0 $self->{fh}->close;
167 0 $self->{fh} = undef;
168 }
169  
170 0 my ( $code, $str, $pos ) = $self->{csv}->error_diag;
171 0 ▶ if ( $str && !$self->{csv}->eof ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str and not $self->{"csv"}->eof) 0 0 0
Condition0%0/3
$str and not $self->{"csv"}->eof
!ll&&!rl&&r
000
MC/DC0%0/2
$str and not $self->{"csv"}->eof
$str not $self->{"csv"}->eof
Truth table0%0/3
A and not(B)
A= $strB= not $self->{"csv"}->eof
ABresult
0X0
100
111
172 0 $self->{logger}->critf( "CSV parse error at pos %s: %s [%s]", $pos, $str, $self->{csv}->error_input );
173 0 return;
174 }
175  
176 0 return $row;
177 }
178  
179 0 ▶sub _fetch_data {
Subroutine _fetch_data: not called CC=5 SCAR=34.0
180 0 my $self = shift;
181  
182 # does zip already exist?
183 0 ▶ if ( -e $self->{zip_file} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $self->{"zip_file"}) 0 0 0
184 0 ▶ if ( !$self->_extract_data ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->_extract_data) { } 0 0 0
185  
186 # failed to extract, file is corrupt? User should try again
187 unlink $self->{zip_file}
188 0 ▶ or die sprintf "Failed to remove cached file '%s': %s\n", $self->{zip_file}, $!;
Branch: 1 branch
BranchTrueFalseTotal
unlink $self->{"zip_file"} or die sprintf("Failed to remove cached file '%s': %s\n", $self->{"zip_file"}, $!) 0 0 0
189 } else {
190 0 return 1;
191 }
192  
193 }
194  
195 0 my $ff = File::Fetch->new( uri => $self->{data_uri} );
196  
197 0 $self->{logger}->info( "Downloading " . $self->{data_uri} . " to " . $self->{base_dir} );
198 my $file = $ff->fetch( to => $self->{base_dir} )
199 0 ▶ or $self->{logger}->warn( $ff->error );
Branch: 1 branch
BranchTrueFalseTotal
my $file = $ff->fetch("to", $self->{"base_dir"}) or $self->{"logger"}->warn($ff->error) 0 0 0
200  
201 0 $self->{zip_file} = $file; # should have been the same anyway
202 0 $self->{logger}->info( "Saved data to $file" );
203 0 $self->_extract_data;
204  
205 0 return 1;
206 }
207  
208 0 ▶sub _extract_data {
Subroutine _extract_data: not called CC=2 SCAR=17.9
209 0 my $self = shift;
210  
211 0 my $zip = Archive::Zip->new;
212  
213 0 ▶ unless ( $zip->read( $self->{zip_file} ) == AZ_OK ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($zip->read($self->{"zip_file"}) == 0) 0 0 0
214 0 $self->{logger}->error( 'Read error' );
215 0 return 0;
216 }
217  
218 0 $zip->extractTree( undef, $self->{data_dir} . "/" );
219  
220 0 return 1;
221 }
222  
223 1 1;
224  
225 =pod
226  
227 =head1 NAME
228  
229 Parse::USDA::NNDB - download and parse the latest USDA National Nutrient Database
230  
231 =head1 VERSION
232  
233 version 0.1
234  
235 =head1 SYNOPSIS
236  
237 use Parse::USDA::NNDB;
238 my $usda = Parse::USDA::NNDB->new($optional_cache_dir);
239 $usda->open_file( 'FD_GROUP' );
240 while (my $fg = $usda->getline) {
241 printf "ID: %s DESC: %s\n", $fg->{NDB_No}, $fg->{Shrt_Desc};
242 }
243  
244 =head1 DESCRIPTION
245  
246 Parse::USDA::NNDB is for parsing the nutrient data files made available by the
247 USDA in ASCII format. If the files are not available, they will be automatically
248 retrieved and extracted for you.
249  
250 =head1 METHODS
251  
252 =over
253  
254 =item new($basedir)
255  
256 Creates a new Parse::USDA::NNDB object. Takes one optional argument, a path
257 to the dir which contains the datafiles to be parsed.
258  
259 =item B<open_file($table)>
260  
261 Call with the case-insensitive name of the file, without extension, to open.
262 You must call this before B<get_line>.
263  
264 Returns true on success.
265  
266 =item B<get_line>
267  
268 After B<open_file>, keep calling this to get the next line. Each line is a
269 hashref (see USDA docs for their meanings).
270  
271 Returns undef when the file is finished or if something goes wrong.
272  
273 =item B<tables>
274  
275 Returns a list of all the known tables/filenames.
276  
277 =item B<get_columns_for($table)>
278  
279 Returns a list of the keys used in this file.
280  
281 =back
282  
283 =head1 SEE ALSO
284  
285 L<USDA documentation|http://www.ars.usda.gov/Services/docs.htm?docid=8964>
286  
287 =head1 AUTHOR
288  
289 Ioan Rogers <ioan.rogers@gmail.com>
290  
291 =head1 COPYRIGHT AND LICENSE
292  
293 This software is Copyright (c) 2012 by Ioan Rogers.
294  
295 This is free software, licensed under:
296  
297 The Artistic License 2.0 (GPL Compatible)
298  
299 =cut
300  
301 __END__
↑ Summary