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

pulibrary / bibdata / 2ee2c4fc-5ef0-4806-b86e-01bf70aa67a0

24 Dec 2024 04:55PM UTC coverage: 91.859% (-0.04%) from 91.902%
2ee2c4fc-5ef0-4806-b86e-01bf70aa67a0

Pull #2569

circleci

christinach
Generate new .rubocop_todo.yml
rubocop fix
Pull Request #2569: Rubocop gems

335 of 378 new or added lines in 57 files covered. (88.62%)

2 existing lines in 2 files now uncovered.

3385 of 3685 relevant lines covered (91.86%)

377.92 hits per line

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

94.64
/app/models/concerns/scsb.rb
1
module Scsb
1✔
2
  extend ActiveSupport::Concern
1✔
3

4
  # if id comes from 001 source is scsb
5
  # if id comes from 009 source is the owning library
6
  def items_by_id(id, source = 'scsb')
1✔
7
    request_body = scsb_bib_id_request(id, source)
5✔
8
    request_body_json = request_body.to_json
5✔
9
    scsb_request_response_customize(scsb_request_response: scsb_request('/sharedCollection/bibAvailabilityStatus', request_body_json))
5✔
10
  end
11

12
  # Retrieves items from the SCSB endpoint using a barcode
13
  def items_by_barcode(barcodes)
1✔
14
    request_body = scsb_barcode_request(barcodes)
5✔
15
    request_body_json = request_body.to_json
5✔
16
    scsb_request_response_customize(scsb_request_response: scsb_request('/sharedCollection/itemAvailabilityStatus', request_body_json))
5✔
17
  end
18

19
  def scsb_request_response_customize(scsb_request_response:)
1✔
20
    scsb_request_response.each do |r|
6✔
21
      r['itemAvailabilityStatus'] = 'Unavailable' if r['itemAvailabilityStatus'] == 'Not Available'
6✔
22
    end
23
  end
24

25
  def scsb_barcode_request(barcodes)
1✔
26
    {
27
      barcodes:
5✔
28
    }
29
  end
30

31
  def scsb_bib_id_request(id, source)
1✔
32
    {
33
      bibliographicId: id,
5✔
34
      institutionId: source
35
    }
36
  end
37

38
  def scsb_conn
1✔
39
    Faraday.new(url: scsb_server) do |faraday|
10✔
40
      faraday.request  :url_encoded # form-encode POST params
10✔
41
      faraday.response :logger unless Rails.env.test? # log requests to STDOUT
10✔
42
      faraday.adapter  Faraday.default_adapter # make requests with Net::HTTP
10✔
43
    end
44
  end
45

46
  def parse_scsb_message(message)
1✔
47
    parsed = JSON.parse(message)
1✔
NEW
48
    parsed.instance_of?(Hash) ? parsed.with_indifferent_access : parsed
×
49
  rescue JSON::ParserError
50
    Rails.logger.error("Failed to parse a message from the SCSB server: #{message}")
1✔
51
    {}
1✔
52
  end
53

54
  def scsb_response_json(response)
1✔
55
    if response.status != 200
8✔
56
      Rails.logger.error("The request to the SCSB server failed: #{response.body}")
3✔
57
      return {}
3✔
58
    end
59

60
    JSON.parse(response.body)
5✔
61
  rescue JSON::ParserError
62
    Rails.logger.error("Failed to parse the response from the SCSB server: #{response.body}")
1✔
63
    {}
1✔
64
  end
65

66
  def parse_scsb_response(response)
1✔
67
    parsed = scsb_response_json(response)
6✔
68
    parsed.instance_of?(Hash) ? parsed.with_indifferent_access : parsed
6✔
69
  end
70

71
  private
1✔
72

73
    def scsb_auth_key
1✔
74
      if Rails.env.test?
8✔
75
        'TESTME'
8✔
76
      else
NEW
77
        ENV['SCSB_AUTH_KEY']
×
78
      end
79
    end
80

81
    def scsb_server
1✔
82
      if Rails.env.test?
14✔
83
        'https://test.api.com/'
14✔
84
      else
NEW
85
        ENV['SCSB_SERVER']
×
86
      end
87
    end
88

89
    def scsb_request(request_path, request_body)
1✔
90
      response = self.scsb_conn.post do |req|
10✔
91
        req.url request_path
8✔
92
        req.headers['Content-Type'] = 'application/json'
8✔
93
        req.headers['Accept'] = 'application/json'
8✔
94
        req.headers['api_key'] = scsb_auth_key
8✔
95
        req.body = request_body
8✔
96
      end
97
      parse_scsb_response(response)
6✔
98
    rescue Faraday::ConnectionFailed, Faraday::TimeoutError => e
99
      Rails.logger.warn("#{self.class}: Connection error for #{scsb_server}")
4✔
100
      raise e
4✔
101
    end
102
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