Skip to content

Commit

Permalink
Use start methof with block
Browse files Browse the repository at this point in the history
  • Loading branch information
eliotjordan committed Dec 19, 2024
1 parent cb6c047 commit c8c96c3
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 13 deletions.
6 changes: 1 addition & 5 deletions app/jobs/pdf_ocr_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,8 @@ def transfer_file(path)
port = Figgy.config["illiad_sftp_port"]
out_path = File.join(Figgy.config["illiad_sftp_path"], "pdf", resource.filename)

begin
sftp = Net::SFTP.start(host, user, { password: pass, port: port })
Net::SFTP.start(host, user, { password: pass, port: port }) do |sftp|
sftp.upload!(path, out_path)
ensure
sftp.close_channel
sftp.session.close
end
end
end
10 changes: 2 additions & 8 deletions spec/jobs/pdf_ocr_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,20 @@

RSpec.describe PdfOcrJob do
describe "#perform" do
let(:ssh_session) { instance_double(Net::SSH::Connection::Session) }
let(:sftp_session) { instance_double(Net::SFTP::Session) }
let(:resource) { FactoryBot.create(:ocr_request, file: fixture_path) }

before do
allow(Net::SFTP).to receive(:start).and_return(sftp_session)
allow(Net::SFTP).to receive(:start).and_yield(sftp_session)
allow(sftp_session).to receive(:upload!)
allow(sftp_session).to receive(:close_channel)
allow(sftp_session).to receive(:session).and_return(ssh_session)
allow(ssh_session).to receive(:close)
end

context "with a valid PDF" do
let(:fixture_path) { Rails.root.join("spec", "fixtures", "files", "sample.pdf") }

it "creates on OCRed PDF, uploads the file to the Illiad SFTP server, and deletes the attached PDF" do
described_class.perform_now(resource: resource)
expect(sftp_session).to have_received(:upload!)
expect(sftp_session).to have_received(:close_channel)
expect(ssh_session).to have_received(:close)
expect(sftp_session).to have_received(:upload!).with(/.*/, /pdf\/sample\.pdf/)
ocr_request = OcrRequest.all.first
expect(ocr_request.state).to eq "Complete"
expect(ocr_request.pdf.attached?).to be false
Expand Down

0 comments on commit c8c96c3

Please sign in to comment.