Class: IsbnDb

Inherits:
Service show all
Defined in:
lib/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.

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

#name, #priority, #request, #service_id, #session_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.



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/service_adaptors/isbn_db.rb', line 24

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

Instance Method Details

#do_request(isbn) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/service_adaptors/isbn_db.rb', line 62

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



80
81
82
83
84
# File 'lib/service_adaptors/isbn_db.rb', line 80

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



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/service_adaptors/isbn_db.rb', line 36

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



20
21
22
# File 'lib/service_adaptors/isbn_db.rb', line 20

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