Index: /trunk/cd2flac
===================================================================
--- /trunk/cd2flac	(revision 22)
+++ /trunk/cd2flac	(revision 23)
@@ -5,9 +5,7 @@
 use lib "$RealBin/lib";
 
-use Tracks;
+use Ripper;
 
-use File::Temp qw{tempdir};
 use File::Spec::Functions qw{catfile splitpath};
-use File::Copy;
 use File::Path qw{mkpath};
 use Getopt::Long qw{:config no_ignore_case no_auto_abbrev};
@@ -20,7 +18,6 @@
 );
 
+# output file
 die "Usage: $0 -o <output.flac> [-D <device>]\n" unless $OUTPUT_NAME;
-
-# output file
 my (undef, $out_dir, $out_file) = splitpath($OUTPUT_NAME);
 # automatically add ".flac"
@@ -37,33 +34,8 @@
 # get the CD info
 $CD_DEVICE ||= '/dev/cdrom';
-my $tracks = Tracks->new;
-$tracks->read_disc($CD_DEVICE);
 
-die "No tracks found; is there a CD in the drive?\n" unless @{ $tracks->get_tracks };
+my $ripper = Ripper->new({ device => $CD_DEVICE });
+$ripper->rip_to_flac($archive_flac);
 
-my $tempdir = tempdir(CLEANUP => 1);
-
-my $wav_file  = catfile($tempdir, 'cdda.wav');
-my $flac_file = catfile($tempdir, 'cdda.flac');
-my $cue_file  = catfile($tempdir, 'cdda.cue');
-
-# rip
-my $span = $tracks->get_cdparanoia_span;
-system 'cdparanoia', '-d', $CD_DEVICE, $span, $wav_file;
-die "\nRipping canceled\n" if ($? & 127);
-
-# encode + cuesheet
-open my $CUE, "> $cue_file";
-print $CUE $tracks->get_cuesheet;
-close $CUE;
-system 'flac', '-o', $flac_file, '--cuesheet', $cue_file, $wav_file;
-die "\nFLAC encoding canceled\n" if ($? & 127);
-
-# MusicBrainz discid metadata
-my $discid = $tracks->get_mbz_discid;
-
-# copy to permanent location
-copy($flac_file, $archive_flac);
-system 'metaflac', '--set-tag', "MUSICBRAINZ_DISCID=$discid", $archive_flac;
 print "Rip saved as $archive_flac\n";
 system 'eject', $CD_DEVICE;
Index: /trunk/lib/Ripper.pm
===================================================================
--- /trunk/lib/Ripper.pm	(revision 23)
+++ /trunk/lib/Ripper.pm	(revision 23)
@@ -0,0 +1,52 @@
+package Ripper;
+
+use Moose;
+
+use Tracks;
+
+use File::Temp qw{tempdir};
+use File::Spec::Functions qw{catfile};
+use File::Copy;
+
+has device => ( is => 'rw' );
+has tracks => ( 
+    is => 'rw',
+    handles => [qw{read_disc get_tracks get_cuesheet}],
+);
+
+sub rip_to_flac {
+    my ($self, $archive_flac) = @_;
+
+    $self->tracks(Tracks->new);
+    $self->read_disc($self->device);
+
+    die "No tracks found; is there a CD in the drive?\n" unless @{ $self->get_tracks };
+
+    my $tempdir = tempdir(CLEANUP => 1);
+
+    my $wav_file  = catfile($tempdir, 'cdda.wav');
+    my $flac_file = catfile($tempdir, 'cdda.flac');
+    my $cue_file  = catfile($tempdir, 'cdda.cue');
+
+    # rip
+    my $span = $self->tracks->get_cdparanoia_span;
+    system 'cdparanoia', '-d', $self->device, $span, $wav_file;
+    die "\nRipping canceled\n" if ($? & 127);
+
+    # encode + cuesheet
+    open my $CUE, "> $cue_file";
+    print $CUE $self->get_cuesheet;
+    close $CUE;
+    system 'flac', '-o', $flac_file, '--cuesheet', $cue_file, $wav_file;
+    die "\nFLAC encoding canceled\n" if ($? & 127);
+
+    # MusicBrainz discid metadata
+    my $discid = $self->tracks->get_mbz_discid;
+
+    # copy to permanent location
+    copy($flac_file, $archive_flac);
+    system 'metaflac', '--set-tag', "MUSICBRAINZ_DISCID=$discid", $archive_flac;
+}
+
+# module return
+1;
