From 1c357005e97487b86ecbc8bb5230da069d433488 Mon Sep 17 00:00:00 2001 From: Beck Davis Date: Thu, 18 Apr 2024 10:21:37 -0400 Subject: [PATCH] Add RenewFile class Co-authored-by: Max Kadel --- app/models/alma_renew/renew_file.rb | 21 ++++++++++++ spec/models/alma_renew/renew_file_spec.rb | 39 +++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 app/models/alma_renew/renew_file.rb create mode 100644 spec/models/alma_renew/renew_file_spec.rb diff --git a/app/models/alma_renew/renew_file.rb b/app/models/alma_renew/renew_file.rb new file mode 100644 index 00000000..eec3626e --- /dev/null +++ b/app/models/alma_renew/renew_file.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +require 'csv' + +module AlmaRenew + class RenewFile + attr_reader :temp_file, :renew_item_list + def initialize(temp_file:) + @temp_file = temp_file + @renew_item_list = [] + end + + def process + CSVValidator.new(csv_filename: temp_file.path).require_headers ['Barcode', 'Patron Group', 'Expiry Date', 'Primary Identifier'] + CSV.foreach(temp_file, headers: true, encoding: 'bom|utf-8') do |row| + renew_item_list << Item.new(row.to_h) + end + renew_item_list + end + end +end diff --git a/spec/models/alma_renew/renew_file_spec.rb b/spec/models/alma_renew/renew_file_spec.rb new file mode 100644 index 00000000..da80893f --- /dev/null +++ b/spec/models/alma_renew/renew_file_spec.rb @@ -0,0 +1,39 @@ +# frozen_string_literal: true +require 'rails_helper' + +RSpec.describe AlmaRenew::RenewFile, type: :model, file_download: true do + let(:temp_file) { Tempfile.new(encoding: 'ascii-8bit') } + let(:renew_file) { described_class.new(temp_file:) } + + it "can be instantiated" do + expect(described_class.new(temp_file:)).to be + end + + it "can access the temp_file" do + expect(renew_file.temp_file).to eq(temp_file) + end + + describe "#process" do + before do + allow(Tempfile).to receive(:new).and_return(temp_file) + end + + around do |example| + temp_file.write(File.open(File.join('spec', 'fixtures', 'renew.csv')).read) + temp_file.rewind + example.run + end + + it "can be processed" do + expect(described_class.new(temp_file:).process).to be + end + + it "returns an array" do + expect(renew_file.process).to be_instance_of(Array) + end + + it "has Items in the array" do + expect(renew_file.process.first).to be_instance_of(AlmaRenew::Item) + end + end +end