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

pulibrary / lsp-data / 94f33f5c-d95a-41e3-8645-afab5401ec91

11 Aug 2025 02:51PM UTC coverage: 99.648% (-0.4%) from 100.0%
94f33f5c-d95a-41e3-8645-afab5401ec91

Pull #111

circleci

mzelesky
fix electronic reproduction match test
Pull Request #111: create oclc match class

51 of 54 new or added lines in 3 files covered. (94.44%)

850 of 853 relevant lines covered (99.65%)

5.05 hits per line

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

93.88
/lib/lsp-data/oclc_record_match.rb
1
# frozen_string_literal: true
2

3
module LspData
1✔
4
  ### Return all matched records by a standard number identifier
5
  ###   and have a separate method to return records deemed acceptable
6
  ###   for copy cataloging
7
  ### Must provide a Z3950Connection object for OCLC
8
  ### identifier_type is either `isbn` or 'oclc'
9
  class OCLCRecordMatch
1✔
10
    attr_reader :identifier, :identifier_type, :conn
1✔
11

12
    def initialize(identifier:, identifier_type:, conn:)
1✔
13
      @identifier = identifier
3✔
14
      @identifier_type = identifier_type
3✔
15
      @conn = conn
3✔
16
    end
17

18
    def records
1✔
19
      search_index = case identifier_type
3✔
20
                     when 'isbn'
21
                       7
2✔
22
                     when 'oclc'
23
                       12
1✔
24
                     end
25
      return unless search_index
3✔
26

27
      result = conn.search_by_id(index: search_index, identifier: identifier)
3✔
28
      result.reject(&:nil?)
3✔
29
    end
30

31
    def filtered_records(title)
1✔
32
      all_records = records
2✔
33
      all_records&.select { |record| acceptable_record?(record, title) }
20✔
34
    end
35

36
    private
1✔
37

38
    def acceptable_leader?(record)
1✔
39
      [' ', '1', '4', 'I', 'M', 'L'].include?(record.leader[17]) &&
18✔
40
        record.leader[6..7] == 'am'
41
    end
42

43
    def belle_lettres?(record)
1✔
44
      %w[1 d f j m p].include?(record['008'].value[33])
6✔
45
    end
46

47
    def acceptable_subject?(record)
1✔
48
      record.fields('600'..'655').any? { |field| field.indicator2 == '0' }
12✔
49
    end
50

51
    def pcc050?(record)
1✔
NEW
52
      record.fields('042').any? { |f| f['a'] == 'pcc' } && record['050']
×
53
    end
54

55
    def call_num050?(record)
1✔
56
      record.fields('050').any? { |f| f['a'] =~ /\d/ }
11✔
57
    end
58

59
    def dlc040?(record)
1✔
NEW
60
      record['040'] =~ /DLC/
×
61
    end
62

63
    def electronic_reproduction?(record)
1✔
64
      record.fields('006').any? { |field| field.value[0] == 'm' } ||
7✔
NEW
65
        record.fields('007').any? { |field| field.value[0] == 'c' } ||
×
66
        record.fields('245').any? { |field| field['h'] } ||
3✔
67
        record.fields.any? { |field| field.tag == '533' }
187✔
68
    end
69

70
    def normalize_string(string)
1✔
71
      strip_punctuation(string).unicode_normalize(:nfd)
10✔
72
                               .gsub(/\p{InCombiningDiacriticalMarks}/, '')
73
                               .downcase
74
    end
75

76
    def strip_punctuation(string)
1✔
77
      string.gsub('%22', '')
10✔
78
            .gsub(/^\s*[aA]\s+/, '')
79
            .gsub(/^\s*[aA]n\s+/, '')
80
            .gsub(/^\s*[Tt]he\s+/, '')
81
            .gsub(/['{}]/, '')
82
            .gsub('&', 'and')
83
            .gsub(/[\u0020-\u0025\u0028-\u002f]/, '')
84
            .gsub(/[\u003a-\u0040\u005b-\u0060]/, '')
85
            .gsub(/[\u007c\u007e\u00a9]/, '')
86
    end
87

88
    def process_title(title)
1✔
89
      normalized = normalize_string(title)
10✔
90
      "#{normalized[5..9]}#{normalized[14..18]}"
10✔
91
    end
92

93
    def title_match?(record, title)
1✔
94
      process_title(title) == process_title(record['245']['a'])
5✔
95
    end
96

97
    def acceptable_record?(record, title)
1✔
98
      acceptable_leader?(record) &&
18✔
99
        acceptable_f040b?(record) &&
100
        (belle_lettres?(record) || acceptable_subject?(record)) &&
6✔
101
        call_num050?(record) &&
102
        title_match?(record, title) &&
103
        electronic_reproduction?(record) == false
104
    end
105

106
    def acceptable_f040b?(record)
1✔
107
      record['040']['b'].nil? || record['040']['b'] == 'eng'
11✔
108
    end
109
  end
110
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