If a full view is available it returns a fulltext service response. If
partial view is available, return as “limited experts”. If no view at all,
still includes a link in highlighted_links, to pay

lip service to google branding requirements.

Unfortunately there is no way tell which of the noview books provide
search, although some do – search is advertised if full or partial view is
available.

If a thumbnail_url is returned in the responses, a cover image is
displayed.

Can also enhances with an abstract, if available. – off by default, set
`abstract: true` to turn on.

And fleshes out bibliographic details from an identifier – if all you had
was an ISBN, will fill in title, author, etc in referent from GBS response.

Google API Key

Setting an api key in :api_key STRONGLY recommended, or you'll probably
get rate limited (not clear what the limit is with no api key supplied).
You may have to ask for higher rate limit for your api key than the default
1000/day, which you can do through the google api console: code.google.com/apis/console

I requested 50k with this message, and was quickly approved with no
questions “Services for academic library (Johns Hopkins Libraries) web
applications to match Google Books availability to items presented by our
catalog, OpenURL link resolver, and other software. ”

Recommend setting your 'per user limit' to something crazy high, as
well as requesting more quota.

Constructor Details

# File 'app/service_adaptors/google_book_search.rb', line 73definitialize(config)@url='https://www.googleapis.com/books/v1/volumes?q='@display_name='Google Books'# number of full views to show
@num_full_views=1# default on, to enhance our metadata with stuff from google
@referent_enhance=true# default OFF, add description/abstract from GBS
@abstract=false# Other responses on by default but can be turned off
@cover_image=true@fulltext=true@search_inside=true@web_links=true# to partial view :excerpts or :fulltext
# google api key strongly recommended, otherwise you'll
# probably get rate limited.
@api_key=nil@credits={"Google Books"=>"http://books.google.com/"}# While you can theoretically look up by LCCN on Google Books,
# we have found FREQUENT false positives. There's no longer any
# way to even report these to Google. By default, don't lookup
# by LCCN.
@lookup_by_lccn=falsesuper(config)end

#add_cover_image(request, url) ⇒ Object

# File 'app/service_adaptors/google_book_search.rb', line 442defadd_cover_image(request,url)zoom_url=url.clone# if we're sent to a page other than the frontcover then strip out the
# page number and insert front cover
zoom_url.sub!(/&pg=.*?&/,'&printsec=frontcover&')# hack out the 'curl' if we can
zoom_url.sub!('&edge=curl','')request.add_service_response(:service=>self,:display_text=>'Cover Image',:url=>zoom_url,:size=>"medium",:service_type_value=>:cover_image)end

#add_search_inside(request, data) ⇒ Object

365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381

# File 'app/service_adaptors/google_book_search.rb', line 365defadd_search_inside(request,data)# Just take the first one we find, if multiple
searchable_view=find_entries(data,[ViewFullValue,ViewPartialValue])[0]if(searchable_view)url=searchable_view["volumeInfo"]["infoLink"]request.add_service_response(:service=>self,:display_text=>@display_name,:display_text_i18n=>"display_name",:url=>remove_query_context(url),:service_type_value=>:search_inside)endend

#build_headers(request) ⇒ Object

We don't need to fake a proxy request anymore, but we still include
X-Forwarded-For so google can return location-appropriate availability. If
there's an existing X-Forwarded-For, we respect it and add on to it.

# File 'app/service_adaptors/google_book_search.rb', line 291defbuild_headers(request)original_forwarded_for=nilif(request.http_env&&request.http_env['HTTP_X_FORWARDED_FOR'])original_forwarded_for=request.http_env['HTTP_X_FORWARDED_FOR']end# we used to prepare a comma seperated list in x-forwarded-for if
# we had multiple requests, as per the x-forwarded-for spec, but I
# think Google doesn't like it.
ip_address=(original_forwarded_for?original_forwarded_for:request.client_ip_addr.to_s)return{}ifip_address.blank?# If we've got a comma-seperated list from an X-Forwarded-For, we
# can't send it on to google, google won't accept that, just take
# the first one in the list, which is actually the ultimate client
# IP. split returns the whole string if seperator isn't found, convenient.
ip_address=ip_address.split(",").first# If all we have is an internal/private IP from the internal network,
# do NOT send that to Google, or Google will give you a 503 error
# and refuse to process your request, as of 7 sep 2011. sigh.
# Also if it doesn't look like an IP at all, forget it, don't send it.
if((!ip_address=~/^\d+\.\d+\.\d+\/\d$/)||ip_address.start_with?("10.")||ip_address.start_with?("172.16")||ip_address.start_with?("192.168"))return{}elsereturn{'X-Forwarded-For'=>ip_address}endend

#create_fulltext_service_response(request, data) ⇒ Object

We only create a fulltext service response if we have a full view. We
create only as many full views as are specified in config.

# File 'app/service_adaptors/google_book_search.rb', line 386defdo_web_links(request,data)# some noview items will have a snippet view, but we have no way to tell
info_views=find_entries(data,ViewPartialValue)viewability=ViewPartialValueifinfo_views.blank?info_views=find_entries(data,ViewNoneValue)viewability=ViewNoneValueend# Shouldn't ever get to this point, but just in case
returnnilifinfo_views.blank?url=''iv=info_views.firsttype=nilif(viewability==ViewPartialValue&&url=iv["volumeInfo"]["previewLink"])display_text=@display_namedisplay_text_i18n="display_name"type=ServiceTypeValue[:excerpts]elseurl=url=iv["volumeInfo"]["infoLink"]display_text="Book Information"display_text_i18n="book_information"type=ServiceTypeValue[:highlighted_link]endrequest.add_service_response(:service=>self,:url=>remove_query_context(url),:display_text=>display_text,:display_text_i18n=>display_text_i18n,:service_type_value=>type)end

#find_thumbnail_url(data) ⇒ Object

Not all responses have a thumbnail_url. We look for them and return the
1st.

429
430
431
432
433
434
435
436
437
438
439

# File 'app/service_adaptors/google_book_search.rb', line 429deffind_thumbnail_url(data)entries=data["items"].collectdo|entry|entry["volumeInfo"]["imageLinks"]["thumbnail"]ifentry["volumeInfo"]&&entry["volumeInfo"]["imageLinks"]end# removenill values
entries.compact!# pick the first of the available thumbnails, or nil
returnentries[0]end

#get_bibkeys(rft) ⇒ Object

returns nil or escaped string of bibkeys to increase the chances of good
hit, we send all available bibkeys and later dedupe by id. FIXME Assumes
we only have one of each kind of identifier.

# File 'app/service_adaptors/google_book_search.rb', line 228defget_bibkeys(rft)isbn=get_identifier(:urn,"isbn",rft)oclcnum=get_identifier(:info,"oclcnum",rft)lccn=get_lccn(rft)# Google doesn't officially support oclc/lccn search, but does
# index as token with prefix smashed up right with identifier
# eg http://books.google.com/books/feeds/volumes?q=OCLC32012617
#
# Except turns out doing it as a phrase search is important! Or
# google's normalization/tokenization does odd things.
keys=[]keys<<('isbn:'+isbn)ifisbnkeys<<('"'+"OCLC"+oclcnum+'"')ifoclcnum# Only use LCCN if we've got nothing else, and we're allowing it.
# it returns many false positives.
if@lookup_by_lccn&&lccn&&keys.length==0keys<<('"'+'LCCN'+lccn+'"')endreturnnilifkeys.empty?keys=CGI.escape(keys.join(' OR '))returnkeysend

#response_url(service_response, submitted_params) ⇒ Object

Catch url_for call for search_inside, because we're going to redirect

469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485

# File 'app/service_adaptors/google_book_search.rb', line 469defresponse_url(service_response,submitted_params)if(!(service_response.service_type_value.name=="search_inside"))returnsuper(service_response,submitted_params)else# search inside!
base=service_response[:url]query=CGI.escape(submitted_params["query"]||"")# attempting to reverse engineer a bit to get 'snippet'
# style results instead of 'onepage' style results.
# snippet seem more user friendly, and are what google's own
# interface seems to give you by default. but 'onepage' is the
# default from our deep link, but if we copy the JS hash data,
# it looks like we can get Google to 'snippet'.
url=base+"&q=#{query}#v=snippet&q=#{query}&f=false"returnurlendend