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

pulibrary / orangelight / b613140a-7a61-4859-a386-22fd24debf51

pending completion
b613140a-7a61-4859-a386-22fd24debf51

Pull #3348

circleci

GitHub
Update app/views/catalog/_show_harmful_content_feedback.html.erb
Pull Request #3348: Add beginning harmful content feedback bar

5256 of 5561 relevant lines covered (94.52%)

1502.66 hits per line

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

78.87
/app/models/concerns/blacklight/solr/document/marc.rb
1
# frozen_string_literal: true
2

3
require 'marc'
3✔
4
require 'openurl/context_object'
3✔
5

6
module Blacklight
3✔
7
  module Solr
3✔
8
    module Document
3✔
9
      module Marc
3✔
10
        include Blacklight::Solr::Document::MarcExport
3✔
11
        include OpenURL
3✔
12

13
        # Prepend our overloaded method to bypass bug in Blacklight
14
        # See https://stackoverflow.com/questions/5944278/overriding-method-by-another-defined-in-module
15
        prepend Blacklight::Solr::Document::MarcExportOverride
3✔
16

17
        class UnsupportedMarcFormatType < RuntimeError; end
3✔
18

19
        def self.extended(document)
3✔
20
          Blacklight::Solr::Document::MarcExport.register_export_formats(document)
1,762✔
21
        end
22

23
        # Accesses the MARC::Record constructed from data retrieved over the HTTP
24
        # @return [MARC::Record]
25
        def to_marc
3✔
26
          @_ruby_marc_obj ||= load_marc
19✔
27
        end
28

29
        # These are registered by default
30
        # @see Blacklight::Solr::Document::MarcExport.register_export_formats
31

32
        # Generate the string-serialized XML from the remote MARC record
33
        # @see Blacklight::Solr::Document::MarcExport#export_as_marcxml
34
        # @return [String]
35
        def export_as_marcxml
3✔
36
          return '' unless to_marc
3✔
37
          super
2✔
38
        end
39

40
        # @see Blacklight::Solr::Document::MarcExport#export_as_marc
41
        # @return [String]
42
        def export_as_marc
3✔
43
          return '' unless to_marc
×
44
          super
×
45
        end
46

47
        # @see Blacklight::Solr::Document::MarcExport#export_as_openurl_ctx_kev
48
        # @return [String]
49
        def export_as_openurl_ctx_kev(format = nil)
3✔
50
          ctx = to_ctx(format)
57✔
51
          # send back the encoded string
52
          ctx.kev
57✔
53
        end
54

55
        # Generate the refworks citation format from the remote MARC record
56
        # @see Blacklight::Solr::Document::MarcExport#export_as_refworks_marc_txt
57
        # @return [String]
58
        def export_as_refworks_marc_txt
3✔
59
          return '' unless to_marc
2✔
60
          super
1✔
61
        end
62

63
        # These are not registered by default, but still provided as public methods
64

65
        # @see Blacklight::Solr::Document::MarcExport#export_as_apa_citation_txt
66
        # @return [String]
67
        def export_as_apa_citation_txt
3✔
68
          return '' unless to_marc
1✔
69
          super
×
70
        end
71

72
        # @see Blacklight::Solr::Document::MarcExport#export_as_mla_citation_txt
73
        # @return [String]
74
        def export_as_mla_citation_txt
3✔
75
          return '' unless to_marc
1✔
76
          super
×
77
        end
78

79
        # @see Blacklight::Solr::Document::MarcExport#export_as_chicago_citation_txt
80
        # @return [String]
81
        def export_as_chicago_citation_txt
3✔
82
          return '' unless to_marc
1✔
83
          super
×
84
        end
85

86
        # @see Blacklight::Solr::Document::MarcExport#export_as_endnote
87
        # @return [String]
88
        def export_as_endnote
3✔
89
          return '' unless to_marc
×
90
          super
×
91
        end
92

93
        # return openurl ctx object
94
        def to_ctx(format)
3✔
95
          @_ctx || build_ctx(format)
62✔
96
        end
97

98
        # returns true if doc originated from alma
99
        def alma_record?
3✔
100
          if /^[0-9]+/.match?(self['id'])
417✔
101
            true
276✔
102
          else
103
            false
141✔
104
          end
105
        end
106

107
        # does we have any standard numbers that can be used by other services
108
        def standard_numbers?
3✔
109
          std_numbers.any? { |v| key? v }
7✔
110
        end
111

112
        def std_numbers
3✔
113
          %w[lccn_s isbn_s issn_s oclc_s]
2✔
114
        end
115

116
        def format_to_openurl_genre(format)
3✔
117
          return 'book' if format == 'book'
62✔
118
          return 'bookitem' if format == 'book'
25✔
119
          return 'journal' if format == 'serial'
25✔
120
          return 'conference' if format == 'conference'
25✔
121
          'unknown'
25✔
122
        end
123

124
        # We allow the user to retry in very specific scenarios.
125
        def can_retry?
3✔
126
          @can_retry
1✔
127
        end
128

129
        protected
3✔
130

131
          def build_ctx(format = nil)
3✔
132
            ctx = ContextObject.new
62✔
133
            id = self['id']
62✔
134
            # title = self['title_citation_display'].first unless self['title_citation_display'].nil?
135
            date = self['pub_date_display'].first unless self['pub_date_display'].nil?
62✔
136
            author = self['author_citation_display'].first unless self['author_citation_display'].nil?
62✔
137
            corp_author = self['pub_citation_display'].first unless self['pub_citation_display'].nil?
62✔
138
            publisher_info = self['pub_citation_display'].first unless self['pub_citation_display'].nil?
62✔
139
            edition = self['edition_display'].first unless self['edition_display'].nil?
62✔
140
            unless format.nil?
62✔
141
              format = format.is_a?(Array) ? format[0].downcase.strip : format.downcase.strip
62✔
142
              genre = format_to_openurl_genre(format)
62✔
143
            end
144
            if format == 'book'
62✔
145
              ctx.referent.set_format('book')
37✔
146
              ctx.referent.set_metadata('genre', 'book')
37✔
147
              # ctx.referent.set_metadata('btitle', title)
148
              # ctx.referent.set_metadata('title', title)
149
              ctx.referent.set_metadata('au', author)
37✔
150
              ctx.referent.set_metadata('aucorp', corp_author)
37✔
151
              # Place not easilty discernable in solr doc
152
              # ctx.referent.set_metadata('place', publisher_info)
153
              ctx.referent.set_metadata('pub', publisher_info)
37✔
154
              ctx.referent.set_metadata('edition', edition)
37✔
155
              ctx.referent.set_metadata('isbn', self['isbn_s'].first) unless self['isbn_s'].nil?
37✔
156
              ctx.referent.set_metadata('date', date)
37✔
157
            elsif /journal/i.match?(format) # checking using include because institutions may use formats like Journal or Journal/Magazine
25✔
158
              ctx.referent.set_format('journal')
13✔
159
              ctx.referent.set_metadata('genre', 'serial')
13✔
160
              # ctx.referent.set_metadata('atitle', title)
161
              # ctx.referent.set_metadata('title', title)
162
              # use author display as corp author for journals
163
              ctx.referent.set_metadata('aucorp', author)
13✔
164
              ctx.referent.set_metadata('issn', self['issn_s'].first) unless self['issn_s'].nil?
13✔
165
            else
166
              ctx.referent.set_format(genre) # do we need to do this?
12✔
167
              ctx.referent.set_metadata('genre', genre)
12✔
168
              # ctx.referent.set_metadata('title', title)
169
              ctx.referent.set_metadata('creator', author)
12✔
170
              ctx.referent.set_metadata('aucorp', corp_author)
12✔
171
              # place not discernable in solr doc
172
              # ctx.referent.set_metadata('place', publisher_info)
173
              ctx.referent.set_metadata('pub', publisher_info)
12✔
174
              ctx.referent.set_metadata('format', format)
12✔
175
              ctx.referent.set_metadata('issn', self['issn_s'].first) unless self['issn_s'].nil?
12✔
176
              ctx.referent.set_metadata('isbn', self['isbn_s'].first) unless self['isbn_s'].nil?
12✔
177
              ctx.referent.set_metadata('date', date)
12✔
178
            end
179
            ## common metadata for all formats
180
            # canonical identifier for the citation?
181
            ctx.referent.add_identifier("https://bibdata.princeton.edu/bibliographic/#{id}")
62✔
182
            # add pulsearch refererrer
183
            ctx.referrer.add_identifier('info:sid/catalog.princeton.edu:generator')
62✔
184
            ctx.referent.add_identifier("info:oclcnum/#{self['oclc_s'].first}") unless self['oclc_s'].nil?
62✔
185
            ctx.referent.add_identifier("info:lccn/#{self['lccn_s'].first}") unless self['lccn_s'].nil?
62✔
186
            ctx
62✔
187
          end
188

189
          # Retrieves the bib. ID from the Solr Document
190
          # @return [String]
191
          def marc_source
3✔
192
            @_marc_source ||= fetch(_marc_source_field)
×
193
          end
194

195
          # Retrieve the MARC 21 bitstream over the HTTP
196
          # @return [MARC::Record]
197
          def marc_record_from_marc21
3✔
198
            return if marc_source.blank?
×
199
            MARC::Record.new_from_marc marc_source
×
200
          end
201

202
          # Retrieve the MARC JSON over the HTTP
203
          # @return [MARC::Record]
204
          def marc_record_from_json
3✔
205
            return if marc_source.blank?
×
206

207
            begin
208
              marc_json = JSON.parse(marc_source)
×
209
            rescue JSON::ParserError => json_error
210
              Rails.logger.error "#{self.class}: Failed to parse the MARC JSON: #{json_error}"
×
211
              return
×
212
            end
213
            MARC::Record.new_from_hash marc_json
×
214
          end
215

216
          # Construct a MARC::Record using MARC record data retrieved over the HTTP
217
          # @return [MARC::Record]
218
          def load_marc
3✔
219
            marc_format = _marc_format_type.to_s
12✔
220

221
            case marc_format
12✔
222
            when 'marcxml'
223
              marc_record_from_marcxml
12✔
224
            when 'marc21'
225
              marc_record_from_marc21
×
226
            when 'json'
227
              marc_record_from_json
×
228
            else
229
              raise UnsupportedMarcFormatType, "Only marcxml, marc21, and json are supported, this documents format is #{_marc_format_type} and the current extension parameters are #{self.class.extension_parameters.inspect}"
×
230
            end
231
          rescue StandardError => e
232
            Rails.logger.error("Blacklight failed to parse MARC record. Exception was: #{e}")
3✔
233
            nil
3✔
234
          end
235

236
          # Construct a MARC::Record using MARC-XML data retrieved over the HTTP
237
          # @return [MARC::Record]
238
          def marc_record_from_marcxml
3✔
239
            id = fetch(_marc_source_field)
12✔
240

241
            response = Faraday.get("#{Requests.config['bibdata_base']}/bibliographic/#{id}")
12✔
242
            @can_retry = response.status == 429
9✔
243
            response_stream = StringIO.new(response.body)
9✔
244
            marc_reader = MARC::XMLReader.new(response_stream)
9✔
245
            marc_records = marc_reader.to_a
9✔
246
            marc_records.first
9✔
247
          end
248

249
          def _marc_helper
3✔
250
            @_marc_helper ||= Blacklight::Marc::Document.new fetch(_marc_source_field), _marc_format_type
×
251
          end
252

253
          def _marc_source_field
3✔
254
            self.class.extension_parameters[:marc_source_field]
12✔
255
          end
256

257
          def _marc_format_type
3✔
258
            # TODO: Raise if not present
259
            self.class.extension_parameters[:marc_format_type]
12✔
260
          end
261

262
          # Overwites the get_author_list(record) method from the module Blacklight::Solr::Document::MarcExport
263
          def get_author_list(record)
3✔
264
            author_list = []
×
265
            authors_primary = record.find { |f| f.tag == '100' }
×
266
            begin
267
              author_primary = authors_primary.find { |s| s.code == 'a' }.value unless authors_primary.nil?
×
268
            rescue StandardError
269
              ''
×
270
            end
271
            author_list.push(clean_end_punctuation(author_primary)) unless author_primary.nil?
×
272
            authors_secondary = record.find_all { |f| f.tag == '700' }
×
273
            authors_secondary&.each do |l|
×
274
              unless l.find { |s| s.code == 'a' }.nil?
×
275
                author_list.push(clean_end_punctuation(l.find { |s| s.code == 'a' }.value)) unless l.find { |s| s.code == 'a' }.value.nil?
×
276
              end
277
            end
278
            author_list.uniq!
×
279
            author_list
×
280
          end
281
      end
282
    end
283
  end
284
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