class Gared::Googlebooks

Public Class Methods

new(api_key, page_size = '40') click to toggle source
# File lib/gared/googlebooks.rb, line 5
def initialize(api_key, page_size = '40')
  @options = {api_key: api_key, maxResults: page_size}
end

Public Instance Methods

query_publications_by_person(person, ctx = nil) click to toggle source
# File lib/gared/googlebooks.rb, line 9
def query_publications_by_person(person, ctx = nil)
  url = "https://www.googleapis.com/books/v1/volumes?q=inauthor:#{URI.escape(person)}&filter=full&key=#{@options[:api_key]}&maxResults=#{@options[:maxResults]}"
  resp = JSON.parse(RestClient.get(url))

  total = resp['totalItems']
  ret = []
  if total > 0
    start_at = 0
    recs = resp['items']
    while recs.length < total
      start_at += @options[:maxResults]
      resp = JSON.parse(RestClient.get(url+"&startIndex=#{start_at}"))
      recs += resp['items']
      sleep 2 # respect the server and avoid flood-blocking
    end
    recs.each do |r|
      next unless r['accessInfo']['pdf']['isAvailable']
      p = Publication.new(ctx)
      p.source_id = r['id']
      p.scanned = true
      p.title = r['volumeInfo']['title']
      h = Holding.new
      h.source_id = r['id']
      h.source_name = 'Google Books'
      p.add_holding(h)
      ret << p
    end
  end
  return ret
end