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

pulibrary / orangelight / 0e37073d-109c-440c-949f-49d2aa86311b

18 Aug 2025 09:05PM UTC coverage: 0.482% (-94.9%) from 95.343%
0e37073d-109c-440c-949f-49d2aa86311b

push

circleci

web-flow
Replace per_page_options_for_select with custom component to avoid deprecation issue (#5186)

* Start creating new component to address deprecaton warning

* Replace per_page_options_for_select with custom component to avoid deprecation issue

Co-authored-by: Jane Sandberg <sandbergja@users.noreply.github.com>

---------

Co-authored-by: Ryan Jensen <rj1044@princeton.edu>
Co-authored-by: Jane Sandberg <sandbergja@users.noreply.github.com>

0 of 33 new or added lines in 1 file covered. (0.0%)

9374 existing lines in 213 files now uncovered.

47 of 9753 relevant lines covered (0.48%)

0.01 hits per line

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

0.0
/app/services/bibdata.rb
1
# frozen_string_literal: true
2

UNCOV
3
class Bibdata
×
4
  # This might be better derived from Faraday::ServerError
UNCOV
5
  class ServerError < StandardError; end
×
UNCOV
6
  class PerSecondThresholdError < StandardError; end
×
UNCOV
7
  class ResourceNotFoundError < StandardError; end
×
UNCOV
8
  class ForbiddenError < StandardError; end
×
UNCOV
9
  class EmptyResponseError < StandardError; end
×
10

UNCOV
11
  class << self
×
12
    # ignore rubocop warnings; complexity and length step from error checking.
UNCOV
13
    def get_patron(user, ldap:)
×
UNCOV
14
      return unless user.uid
×
15

UNCOV
16
      patron_uri = patron_uri(id: user.uid, ldap:)
×
UNCOV
17
      api_response = api_request_patron(patron_uri:)
×
18

UNCOV
19
      build_api_patron(api_response:, user:)
×
UNCOV
20
    rescue ServerError
×
UNCOV
21
      Rails.logger.error('An error was encountered with the Patron Data Service.')
×
UNCOV
22
      {}
×
UNCOV
23
    rescue PerSecondThresholdError => per_second_error
×
UNCOV
24
      Rails.logger.error("The maximum number of HTTP requests per second for the Alma API has been exceeded.")
×
UNCOV
25
      raise(per_second_error)
×
UNCOV
26
    rescue ResourceNotFoundError
×
UNCOV
27
      Rails.logger.error("404 Patron #{user.uid} cannot be found in the Patron Data Service.")
×
UNCOV
28
      {}
×
UNCOV
29
    rescue ForbiddenError
×
UNCOV
30
      Rails.logger.error("403 Not Authorized to Connect to Patron Data Service at #{api_base_uri}/patron/#{user.uid}")
×
UNCOV
31
      {}
×
UNCOV
32
    rescue Faraday::ConnectionFailed
×
UNCOV
33
      Rails.logger.error("Unable to connect to #{api_base_uri}")
×
UNCOV
34
      {}
×
UNCOV
35
    rescue EmptyResponseError
×
UNCOV
36
      Rails.logger.error("#{patron_uri} returned an empty patron response")
×
UNCOV
37
      {}
×
UNCOV
38
    end
×
39

UNCOV
40
    def holding_locations
×
41
      # check cache; return unless nil
UNCOV
42
      locations = Rails.cache.fetch('holding_locations', expires_in: 24.hours)
×
UNCOV
43
      return locations unless locations.nil?
×
44

45
      # don't cache if we didn't get a success
UNCOV
46
      response = Faraday.get("#{Requests.config['bibdata_base']}/locations/holding_locations.json")
×
UNCOV
47
      return {} unless response.status == 200
×
48

UNCOV
49
      locations = sorted_locations(response)
×
UNCOV
50
      Rails.cache.write('holding_locations', locations, expires_in: 24.hours)
×
UNCOV
51
      locations
×
UNCOV
52
    end
×
53

UNCOV
54
    private
×
55

UNCOV
56
      def api_base_uri
×
UNCOV
57
        Requests.config['bibdata_base']
×
UNCOV
58
      end
×
59

UNCOV
60
      def api_request_patron(patron_uri:)
×
UNCOV
61
        api_response = Faraday.get(patron_uri)
×
62

UNCOV
63
        case api_response.status
×
UNCOV
64
        when 500
×
UNCOV
65
          raise(ServerError)
×
UNCOV
66
        when 429
×
UNCOV
67
          raise(PerSecondThresholdError)
×
UNCOV
68
        when 404
×
UNCOV
69
          raise(ResourceNotFoundError)
×
UNCOV
70
        when 403
×
UNCOV
71
          raise(ForbiddenError)
×
UNCOV
72
        else
×
UNCOV
73
          raise(EmptyResponseError) if api_response.body.empty?
×
UNCOV
74
        end
×
75

UNCOV
76
        api_response
×
UNCOV
77
      end
×
78

UNCOV
79
      def patron_uri(id:, ldap:)
×
UNCOV
80
        "#{api_base_uri}/patron/#{id}?ldap=#{ldap}"
×
UNCOV
81
      end
×
82

UNCOV
83
      def build_api_patron(api_response:, user:)
×
UNCOV
84
        response_body = api_response.body
×
UNCOV
85
        base_patron_json = JSON.parse(response_body)
×
UNCOV
86
        base_patron_json.merge(valid: user.valid?).with_indifferent_access
×
UNCOV
87
      rescue JSON::ParserError
×
UNCOV
88
        Rails.logger.error("#{api_response.env.url} returned an invalid patron response: #{response_body}")
×
UNCOV
89
        {}
×
UNCOV
90
      end
×
91

UNCOV
92
      def sorted_locations(response)
×
UNCOV
93
        locations_hash = {}.with_indifferent_access
×
UNCOV
94
        JSON.parse(response.body).each do |location|
×
UNCOV
95
          locations_hash[location['code']] = location.with_indifferent_access
×
UNCOV
96
        end
×
UNCOV
97
        sorted = locations_hash.sort_by do |_i, l|
×
UNCOV
98
          [l['library']['order'], l['library']['label'], l['label']]
×
UNCOV
99
        end
×
100

UNCOV
101
        sorted.to_h.with_indifferent_access
×
UNCOV
102
      end
×
103
    # rubocop:enable Metrics/MethodLength
104
    # rubocop:enable Metrics/AbcSize
UNCOV
105
  end
×
UNCOV
106
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