Class: OpenLibraryCover
- Inherits:
-
Service
- Object
- Service
- OpenLibraryCover
- Defined in:
- lib/service_adaptors/open_library_cover.rb
Overview
Looks for cover images from OpenLibrary Cover API. Lookig up covers in OL can require multiple HTTP requests, one for each identifier, which can sometimes lead to slowness. OL also doesn't have great cover image coverage. So if you have access to Amazon or Google covers, you probably don't need this service.
Constant Summary
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)
- - (Object) cover_uri(type, id)
- - (Object) handle(request)
-
- (OpenLibraryCover) initialize(config)
constructor
A new instance of OpenLibraryCover.
- - (Object) service_types_generated
Methods inherited from Service
#display_name, #handle_wrapper, #link_out_filter, #preempted_by, required_config_params, #response_to_view_data, #response_url, #session, #update_session, #view_data_from_service_type
Constructor Details
- (OpenLibraryCover) initialize(config)
A new instance of OpenLibraryCover
13 14 15 16 17 |
# File 'lib/service_adaptors/open_library_cover.rb', line 13 def initialize(config) @base_url = "http://covers.openlibrary.org/b/" @size = "medium" # "small", "medium" or "large" super(config) end |
Instance Method Details
- (Object) cover_uri(type, id)
62 63 64 |
# File 'lib/service_adaptors/open_library_cover.rb', line 62 def cover_uri(type, id) @base_url + type.to_s + "/" + id.to_s + "-" + @size[0,1].upcase + ".jpg?default=false" end |
- (Object) handle(request)
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 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/open_library_cover.rb', line 19 def handle(request) ids = { :isbn => request.referent.isbn, :oclc => request.referent.oclcnum, :lccn => request.referent.lccn } ids.delete_if {|k,v| v.blank?} # Return if we don't have any identifiers return request.dispatched(self, true) unless ids.size > 0 # What order is best for trying first? [:isbn, :oclc, :lccn].each do |type| next unless ids[type] uri = cover_uri(type, ids[type] ) s_time = Time.now response = Net::HTTP.get_response(URI.parse(uri)) RAILS_DEFAULT_LOGGER.debug("#{@id}: #{Time.now - s_time}s to lookup #{uri}") if response.kind_of?( Net::HTTPNotFound ) # OL has no cover next end unless response.kind_of?( Net::HTTPSuccess ) # unexpected response RAILS_DEFAULT_LOGGER.error("#{@id}: Error in HTTP response when requesting #{uri}, #{response.inspect}") end # Got this far, we've got a response. request.add_service_response( :service => self, :display_text => "Cover Image", :size => "medium", :url => uri ) break end return request.dispatched(self, true) end |
- (Object) service_types_generated
9 10 11 |
# File 'lib/service_adaptors/open_library_cover.rb', line 9 def service_types_generated return [ServiceTypeValue[:cover_image]] end |