class Gared::Nli_Api

Public Class Methods

new(url, api_key) click to toggle source
# File lib/gared/nli_api.rb, line 5
def initialize(url, api_key)
  @options = {url: url, api_key: api_key}
end

Public Instance Methods

fetch_value_by_dc_key(record, key) click to toggle source
# 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) click to toggle source
# File lib/gared/nli_api.rb, line 12
def query_person(person)
end
query_persons(q) click to toggle source
# File lib/gared/nli_api.rb, line 9
def query_persons(q)
end
query_publication(publication) click to toggle source
# File lib/gared/nli_api.rb, line 18
def query_publication(publication)
end
query_publications(q) click to toggle source
# File lib/gared/nli_api.rb, line 15
def query_publications(q)
end
query_publications_by_person(person, ctx = nil) click to toggle source

return in-memory Publication instances with associated Holdings

# 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.author_line = 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