Class: OpenLibraryCover

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

#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

- (OpenLibraryCover) initialize(config)



13
14
15
16
17
18
19
20
21
22
# File 'app/service_adaptors/open_library_cover.rb', line 13

def initialize(config)
  @base_url = "http://covers.openlibrary.org/b/"    
  @size = "medium" # "small", "medium" or "large"
  
  @credits = {
    "OpenLibrary" => "http://openlibrary.org/"
  }
  
  super(config)
end

Instance Method Details

- (Object) cover_uri(type, id)



68
69
70
# File 'app/service_adaptors/open_library_cover.rb', line 68

def cover_uri(type, id)
  @base_url + type.to_s + "/" + id.to_s + "-" + @size[0,1].upcase + ".jpg?default=false"
end

- (Object) handle(request)



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
61
62
63
64
65
66
# File 'app/service_adaptors/open_library_cover.rb', line 24

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.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.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,
      :service_type_value => :cover_image
    )
    break
  end

  return request.dispatched(self, true)
end

- (Object) service_types_generated



9
10
11
# File 'app/service_adaptors/open_library_cover.rb', line 9

def service_types_generated
  return [ServiceTypeValue[:cover_image]]
end