Class: Gared::Nli_Api
- Inherits:
-
Object
- Object
- Gared::Nli_Api
- Defined in:
- lib/gared/nli_api.rb
Instance Method Summary collapse
- #fetch_value_by_dc_key(record, key) ⇒ Object
-
#initialize(url, api_key) ⇒ Nli_Api
constructor
A new instance of Nli_Api.
- #query_person(person) ⇒ Object
- #query_persons(q) ⇒ Object
- #query_publication(publication) ⇒ Object
- #query_publications(q) ⇒ Object
-
#query_publications_by_person(person, ctx = nil) ⇒ Object
return in-memory Publication instances with associated Holdings.
Constructor Details
#initialize(url, api_key) ⇒ Nli_Api
Returns a new instance of Nli_Api.
5 6 7 |
# File 'lib/gared/nli_api.rb', line 5 def initialize(url, api_key) @options = {url: url, api_key: api_key} end |
Instance Method Details
#fetch_value_by_dc_key(record, key) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/gared/nli_api.rb', line 20 def fetch_value_by_dc_key(record, key) ret = '' fullkey = key[0] == '@' ? key : 'http://purl.org/dc/elements/1.1/' + key unless record.nil? unless record[fullkey].nil? if record[fullkey].class == String ret = record[fullkey ] elsif record[fullkey].class == Array ret = record[fullkey].map{|x| x['@value'] }.join('; ') end end end ret end |
#query_person(person) ⇒ Object
12 13 |
# File 'lib/gared/nli_api.rb', line 12 def query_person(person) end |
#query_persons(q) ⇒ Object
9 10 |
# File 'lib/gared/nli_api.rb', line 9 def query_persons(q) end |
#query_publication(publication) ⇒ Object
18 19 |
# File 'lib/gared/nli_api.rb', line 18 def query_publication(publication) end |
#query_publications(q) ⇒ Object
15 16 |
# File 'lib/gared/nli_api.rb', line 15 def query_publications(q) end |
#query_publications_by_person(person, ctx = nil) ⇒ Object
return in-memory Publication instances with associated Holdings
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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/gared/nli_api.rb', line 35 def query_publications_by_person(person, ctx = nil) ret = [] begin # first run obtain counts for the query escaped_person = URI.escape(person) url = @options[:url]+"?api_key=#{@options[:api_key]}&query=creator,contains,#{escaped_person},AND;language,exact,heb&sort_field=title&material_type=books&count_mode=true" json = JSON.parse(RestClient::Resource.new(url,verify_ssl: OpenSSL::SSL::VERIFY_NONE).get) total = json['countInfos']['total'] # then start loading the results result_page = 1 recs = [] while recs.length < total url = @options[:url]+"?api_key=#{@options[:api_key]}&query=creator,contains,#{escaped_person},AND;language,exact,heb&sort_field=title&material_type=books&result_page=#{result_page}" puts "DBG: retrieving results page #{result_page}" json = JSON.parse(RestClient::Resource.new(url,verify_ssl: OpenSSL::SSL::VERIFY_NONE).get) recs += json result_page += 1 # sleep 1 # respect the server and avoid flood-blocking end recs.each do |r| begin p = Publication.new(ctx) p.title = fetch_value_by_dc_key(r, 'title') p. = fetch_value_by_dc_key(r, 'creator') p.language = fetch_value_by_dc_key(r, 'language') p.notes = "#{fetch_value_by_dc_key(r, 'format')}\n#{fetch_value_by_dc_key(r, 'subject')}" p.publisher_line = fetch_value_by_dc_key(r,'publisher') p.pub_year = fetch_value_by_dc_key(r, 'non_standard_date') p.source_id = fetch_value_by_dc_key(r, '@id') # collect additional URLS from record, for clients to be able to determine whether a scanned object exists additional_urls = [] r.keys.each do |key| val = fetch_value_by_dc_key(r, key) additional_urls << val if val =~ /https?:[^\s]\/\// end p.additional_urls = additional_urls if additional_urls.length > 0 h = Holding.new h.source_id = p.source_id h.source_name = 'NLI API' h.location = fetch_value_by_dc_key(r, 'recordid') p.add_holding(h) ret << p rescue Exception puts $! end end # TODO: also collect IIIF links for the *subset* of titles that have them, using the availability_type param. No way to get that in the above query -- the fields are not emitted. # the URL is like https://api.nli.org.il/openlibrary/search?api_key=(((KEY)))&query=title,contains,querystring&availability_type=online_and_api_access&material_type=books rescue Exception puts $! end return ret end |