-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3b0f09d
commit 151f116
Showing
5 changed files
with
536 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# frozen_string_literal: true | ||
|
||
class ServiceTokenError < StandardError; end | ||
|
||
class HoldingsRequestError < StandardError; end | ||
|
||
class ItemRequestError < StandardError; end | ||
|
||
class RequestDetailsError < StandardError; end | ||
|
||
class UserDetailsError < StandardError; end | ||
|
||
class CatalogueServicesClient | ||
MAX_TOKEN_RETRIES = 3 | ||
|
||
def get_holdings(instance_id:) | ||
conn = setup_connection | ||
|
||
res = conn.get("/catalogue-services/folio/instance/#{instance_id}") | ||
if res.status == 200 | ||
if res.body.present? | ||
res.body["holdingsRecords"] | ||
end | ||
else | ||
Rails.logger.error "Failed to retrieve holdings for #{instance_id}" | ||
raise HoldingsRequestError.new("Failed to retrieve holdings for #{instance_id}") | ||
end | ||
rescue => e | ||
Rails.logger.error "get_holdings - Failed to connect catalogue-service: #{e.message}" | ||
raise HoldingsRequestError.new("Failed to retrieve holdings for #{instance_id}") | ||
end | ||
|
||
def get_holding(instance_id:, holdings_id:, item_id:) | ||
all_holdings = get_holdings(instance_id: instance_id) | ||
|
||
# find holdings record | ||
holding = all_holdings.find { |h| h["id"] == holdings_id } | ||
|
||
# find item record | ||
item = holding["itemRecords"].find { |i| i["id"] == item_id } | ||
|
||
[holding, item] | ||
end | ||
|
||
def get_requestable(instance_id:, holdings_id:, item_id:) | ||
all_holdings = get_holdings(instance_id: instance_id) | ||
|
||
# find holdings record | ||
holding = all_holdings.find { |h| h["id"] == holdings_id } | ||
|
||
# find item record | ||
item = holding["itemRecords"].find { |i| i["id"] == item_id } | ||
|
||
item["requestable"] ? item["displayStatus"] : "Not for loan" | ||
end | ||
|
||
def get_item_ids(instance_id:) | ||
all_holdings = get_holdings(instance_id: instance_id) | ||
|
||
item_id = all_holdings.first["itemRecords"].first["holdingsRecordId"] if all_holdings.first["itemRecords"].any? | ||
|
||
holding_id = all_holdings.first["itemRecords"].first["id"] if all_holdings.first["itemRecords"].any? | ||
p "catservices" | ||
pp [item_id, holding_id] | ||
[item_id, holding_id] | ||
end | ||
|
||
private | ||
|
||
def setup_connection | ||
Faraday.new(url: ENV["CATALOGUE_SERVICES_API_BASE_URL"]) do |f| | ||
f.response :json | ||
end | ||
end | ||
end |
Oops, something went wrong.