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

pulibrary / bibdata / 373ad6ff-fad2-405a-ab26-3d30fb5ceecf

24 Dec 2024 08:24PM UTC coverage: 91.938% (+0.08%) from 91.859%
373ad6ff-fad2-405a-ab26-3d30fb5ceecf

Pull #2563

circleci

maxkadel
Put attaching xml files in their own batch
Pull Request #2563: I2321 Shift SCSB full index tasks into separate background jobs

152 of 156 new or added lines in 10 files covered. (97.44%)

65 existing lines in 17 files now uncovered.

3478 of 3783 relevant lines covered (91.94%)

366.14 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 { |r| r["itemAvailabilityStatus"] = "Unavailable" if r["itemAvailabilityStatus"] == "Not Available" }
12✔
21
  end
22

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

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

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

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

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

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

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

70
  private
1✔
71

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

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

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