Class: Gared::Googlebooks

Inherits:
Object
  • Object
show all
Defined in:
lib/gared/googlebooks.rb

Instance Method Summary collapse

Constructor Details

#initialize(api_key, page_size = '40') ⇒ Googlebooks

Returns a new instance of Googlebooks.



5
6
7
# File 'lib/gared/googlebooks.rb', line 5

def initialize(api_key, page_size = '40')
  @options = {api_key: api_key, maxResults: page_size}
end

Instance Method Details

#query_publications_by_person(person, ctx = nil) ⇒ Object



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
# File 'lib/gared/googlebooks.rb', line 13

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

#uri_escape(s) ⇒ Object



8
9
10
11
# File 'lib/gared/googlebooks.rb', line 8

def uri_escape(s)
  p = URI::Parser.new
  return p.escape(s)
end