• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pulibrary / allsearch_api / 0400cc50-b3f0-4794-8616-b4382e0acdd0

14 Oct 2025 03:57AM UTC coverage: 94.558% (-5.0%) from 99.514%
0400cc50-b3f0-4794-8616-b4382e0acdd0

Pull #395

circleci

sandbergja
Migrate Library Staff Controller away from ActionController
Pull Request #395: Migrate a few more controllers away from Rails

8 of 8 new or added lines in 4 files covered. (100.0%)

93 existing lines in 21 files now uncovered.

973 of 1029 relevant lines covered (94.56%)

71.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.74
/app/services/csv_loading_service.rb
1
# frozen_string_literal: true
2

3
require 'csv'
2✔
4
require 'open-uri'
2✔
5

6
# A general class that can be subclassed
7
# to load data from a remote CSV file
8
# into the database
9
class CSVLoadingService
2✔
10
  def run
2✔
11
    fetch_data
54✔
12
    process_data if data_is_valid?
54✔
13
  end
14

15
  private
2✔
16

17
  attr_reader :csv
2✔
18

19
  def fetch_data
2✔
20
    contents = uri.open
54✔
21
    @csv = CSV.new(contents)
54✔
22
  end
23

24
  def process_data
2✔
25
    class_to_load.destroy_all
52✔
26
    csv.each { |row| class_to_load.new_from_csv(row) }
472✔
27
  end
28

29
  # Data is valid if the CSV has not shrunk significantly, and the header row matches the expected headers
30
  def data_is_valid?
2✔
31
    return false if csv_is_much_smaller?
54✔
32

33
    header_row_matches?
54✔
34
  end
35

36
  # If the CSV shrinks by 25% or more, assume something is wrong
37
  def csv_is_much_smaller?
2✔
38
    much_smaller = csv_shrinkage > (existing_records * 0.25)
54✔
39
    return false unless much_smaller
54✔
40

UNCOV
41
    Rails.logger.error("The #{self.class} had a much shorter CSV. " \
×
42
                       "The original length was #{existing_records} rows, " \
43
                       "the new length is #{new_csv_length} rows.")
UNCOV
44
    much_smaller
×
45
  end
46

47
  def csv_shrinkage
2✔
48
    existing_records - new_csv_length
54✔
49
  end
50

51
  def existing_records
2✔
52
    @existing_records ||= class_to_load.count
108✔
53
  end
54

55
  def new_csv_length
2✔
56
    @new_csv_length ||= begin
54✔
57
      length = csv.readlines.size
54✔
58
      csv.rewind
54✔
59
      length
54✔
60
    end
61
  end
62

63
  # Expect certain headers
64
  def header_row_matches?
2✔
65
    new_headers = csv.readline
54✔
66
    return true if (new_headers & expected_headers) == expected_headers
54✔
67

68
    Rails.logger.error("The #{self.class} did not load the CSV " \
2✔
69
                       "because the headers didn't match. The expected headers are: " \
70
                       "#{expected_headers.to_sentence}. " \
71
                       "The new CSV headers are #{new_headers&.to_sentence}.")
72
    false
2✔
73
  end
74

75
  def uri; end
2✔
76

77
  def class_to_load; end
2✔
78
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc