Skip to content

Commit

Permalink
Add RenewFile class
Browse files Browse the repository at this point in the history
Co-authored-by: Max Kadel <[email protected]>
  • Loading branch information
Beck-Davis and maxkadel committed Apr 18, 2024
1 parent dcc6dcf commit 1c35700
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
21 changes: 21 additions & 0 deletions app/models/alma_renew/renew_file.rb
Original file line number Diff line number Diff line change
@@ -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
39 changes: 39 additions & 0 deletions spec/models/alma_renew/renew_file_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 1c35700

Please sign in to comment.