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

pulibrary / orangelight / 00dbc10b-d747-4ad7-b211-7b26d753abbb

14 Aug 2025 01:25PM UTC coverage: 0.483% (-94.9%) from 95.343%
00dbc10b-d747-4ad7-b211-7b26d753abbb

push

circleci

web-flow
Merge pull request #5181 from pulibrary/dependabot/bundler/activestorage-7.2.2.2

Bump activestorage from 7.2.2.1 to 7.2.2.2

47 of 9721 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

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

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

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

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

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

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

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

54
    private
×
55

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

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

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

76
        api_response
×
77
      end
×
78

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

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

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

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