Class: IsbnDb

Inherits:
Service show all
Defined in:
app/service_adaptors/isbn_db.rb

Overview

Talks to the ISBNDb (isbndb.com) to get pricing info and links to pricing info for online sellers. There are potentially other services we could make use of there in the future too.

By default makes an API request in advance to check if book is available, and thus requires an API key. However, ISBNdb seems no longer as reliable as it once was there. You may not want to use ISBNdb at all, see AllBooksDotCom as an alternative (although with no pre-check for hits.)

jrochkind talked to the operators of isbndb and got a very high traffic limit (instead of the default free 500 requests/day), for free. You could try the same.

config params in services.yml:

access_key:  Your API access key from isbnDB.
display_text: (Optional) name of link.
timeout:  (Optional) seconds to wait for response
display_name: (Optional) what to call the service in display

Constant Summary

Constants inherited from Service

Service::LinkOutFilterTask, Service::StandardTask

Instance Attribute Summary

Attributes inherited from Service

#group, #name, #priority, #request, #service_id, #status, #task, #url

Instance Method Summary collapse

Methods inherited from Service

#credits, #display_name, #handle_wrapper, #link_out_filter, #preempted_by, required_config_params, #response_to_view_data, #response_url, #view_data_from_service_type

Constructor Details

#initialize(config) ⇒ IsbnDb

Returns a new instance of IsbnDb.



29
30
31
32
33
34
35
36
37
38
39
# File 'app/service_adaptors/isbn_db.rb', line 29

def initialize(config)
  @timeout = 3
  @display_text = "Compare online prices"
  @display_name = "ISBNdb.com"
  
  @credits = {
    "ISBNdb" => "http://isbndb.com/"
  }
  
  super(config)
end

Instance Method Details

#do_request(isbn) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/service_adaptors/isbn_db.rb', line 67

def do_request(isbn)
  host = "isbndb.com"
  # including results=details will prime isbndb to refresh pricing, Andrew tells me. 
  path = "/api/books.xml?access_key=#{@access_key}&results=details,prices&index1=isbn&value1=#{isbn}"

  http = Net::HTTP.new( host )
  http.open_timeout = @timeout
  http.read_timeout = @timeout

  response =  http.get( path )
  # raise if not 200 OK response
  response.value
  
  return response  
end

Pass in nokogiri object representing the <BookData> element. passes back string url of isbndb prices/availability page



85
86
87
88
89
# File 'app/service_adaptors/isbn_db.rb', line 85

def get_prices_link( book_data )
  book_id = book_data.attributes['book_id']

  return (book_id) ? "http://isbndb.com/d/book/#{book_id}/prices.html" : nil 
end

#handle(umlaut_request) ⇒ Object



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
# File 'app/service_adaptors/isbn_db.rb', line 41

def handle(umlaut_request)
  
  isbn = umlaut_request.referent.['isbn']
  
  # No isbn, nothing we can do. 
  return umlaut_request.dispatched(self, true) if isbn.blank?
  
  response = do_request(isbn)
  xml = Nokogiri::XML( response.body )
  book_xml = xml.at('ISBNdb/BookList/BookData')

  # No hits?
  return umlaut_request.dispatched(self, true) if book_xml.blank?
  
  prices_link = get_prices_link( book_xml )
  
  umlaut_request.add_service_response(
    :service=>self, 
    :url=> prices_link, 
    :display_text=> @display_text,
    :service_type_value => ServiceTypeValue[:highlighted_link]
    )

  return umlaut_request.dispatched(self, true)
end

#service_types_generatedObject



25
26
27
# File 'app/service_adaptors/isbn_db.rb', line 25

def service_types_generated
  return [ServiceTypeValue['highlighted_link']]
end