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

pulibrary / allsearch_api / 29cc47ba-38a5-447a-9a89-fa55bc36f401

14 Oct 2025 01:59PM UTC coverage: 99.32% (-0.2%) from 99.514%
29cc47ba-38a5-447a-9a89-fa55bc36f401

Pull #395

circleci

sandbergja
add missing require_relative
Pull Request #395: Migrate a few more controllers away from Rails

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

90 existing lines in 18 files now uncovered.

1022 of 1029 relevant lines covered (99.32%)

96.72 hits per line

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

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

3
require 'csv'
3✔
4
require 'open-uri'
3✔
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
3✔
10
  def run
3✔
11
    fetch_data
75✔
12
    process_data if data_is_valid?
75✔
13
  end
14

15
  private
3✔
16

17
  attr_reader :csv
3✔
18

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

24
  def process_data
3✔
25
    class_to_load.destroy_all
68✔
26
    csv.each { |row| class_to_load.new_from_csv(row) }
623✔
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?
3✔
31
    return false if csv_is_much_smaller?
75✔
32

33
    header_row_matches?
72✔
34
  end
35

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

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

47
  def csv_shrinkage
3✔
48
    existing_records - new_csv_length
75✔
49
  end
50

51
  def existing_records
3✔
52
    @existing_records ||= class_to_load.count
153✔
53
  end
54

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

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

UNCOV
68
    Rails.logger.error("The #{self.class} did not load the CSV " \
4✔
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}.")
UNCOV
72
    false
4✔
73
  end
74

75
  def uri; end
3✔
76

77
  def class_to_load; end
3✔
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