Class: IsbnDb

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

Overview

NOT recommended anymore, jrochkind has found IsbnDb to be pretty flaky, and hard to deal with. See book_finder.rb or all_books_dot_com.rb as alternatives.

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_url, #translate

Constructor Details

#initialize(config) ⇒ IsbnDb

Returns a new instance of IsbnDb.



32
33
34
35
36
37
38
39
40
41
42
# File 'app/service_adaptors/isbn_db.rb', line 32

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



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/service_adaptors/isbn_db.rb', line 70

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



88
89
90
91
92
# File 'app/service_adaptors/isbn_db.rb', line 88

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



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/service_adaptors/isbn_db.rb', line 44

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



28
29
30
# File 'app/service_adaptors/isbn_db.rb', line 28

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