This repository has been archived by the owner on May 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollections.rb
66 lines (52 loc) · 2.29 KB
/
collections.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
module Harvard::LibraryCloud::Collections
require 'faraday'
require 'json'
include Harvard::LibraryCloud
# Logic for adding an item to a collection,
def add_to_collection_action (collection, item)
api = Harvard::LibraryCloud::API.new
path = 'collections/' + collection
connection = Faraday.new(:url => api.get_base_uri + path) do |faraday|
faraday.request :url_encoded
faraday.response :logger
faraday.adapter Faraday.default_adapter
faraday.headers['Content-Type'] = 'application/json'
faraday.headers['X-LibraryCloud-API-Key'] = ENV["LC_API_KEY"]
end
raw_response = begin
response = connection.post do |req|
req.body = '[{"item_id": "' + item + '"}]'
end
{ status: response.status.to_i, headers: response.headers, body: response.body.force_encoding('utf-8') }
rescue Errno::ECONNREFUSED, Faraday::Error => e
raise RSolr::Error::Http.new(connection, e.response)
end
end
# This is the action that displays the contents of the "Add to Collection" dialog
def add_to_collection
if request.post?
# Actually add the item to the collection
ids = request.params[:id].split(',')
ids.each do |id|
add_to_collection_action(request.params[:collection], id)
end
# Don't render the default "Add to Collection" dialog - render the "Success!" dialog contents
flash[:success] ||= "The item has been added to the collection"
render 'catalog/add_to_collection_success'
end
end
# There is probably a better way to do this. Blacklight is calling a function with this name to render
# the path for the "Add to Collection" action, but I'm not 100% sure why. According to the documentation
# we would expect this function to be named 'add_to_collection_catalog_path'.
def add_to_collection_solr_document_path (arg)
"/catalog/" << arg.id << "/add_to_collection"
end
# Helper method to get the collections that are available for adding an item to
# This does not currently do any filtering by ownership, so it shows ALL collections
def available_collections
api = API.new
params = {:limit => 9999, :preserve_original => true}
response = api.send_and_receive('collections', {:params => params})
response.map {|n| [n['title'], n['identifier']]}
end
end