Class: CoverThing

Inherits:
Service show all
Includes:
MetadataHelper
Defined in:
app/service_adaptors/cover_thing.rb

Overview

Find book covers from LibraryThing's CoverThing service. Only fetches “medium” size image. Fetches only by ISBN.

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 included from MetadataHelper

#get_doi, #get_epage, #get_gpo_item_nums, #get_identifier, #get_isbn, #get_issn, #get_lccn, #get_month, #get_oclcnum, #get_pmid, #get_search_creator, #get_search_terms, #get_search_title, #get_spage, #get_sudoc, #get_top_level_creator, #get_year, #normalize_lccn, #normalize_title, #raw_search_title, title_is_serial?

Methods included from MarcHelper

#add_856_links, #edition_statement, #get_title, #get_years, #gmd_values, #service_type_for_856, #should_skip_856_link?, #strip_gmd

Methods inherited from Service

#credits, #display_name, #handle_wrapper, #link_out_filter, #preempted_by, required_config_params, #response_url, #translate

Constructor Details

#initialize(config) ⇒ CoverThing


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

def initialize(config)
  @display_name = "LibraryThing"
  # http://covers.librarything.com/devkey/KEY/medium/isbn/0545010225
  @base_url = 'http://covers.librarything.com/devkey/';
  @lt404url = 'http://www.librarything.com/coverthing404.php'
  
  @credits = {
    "LibraryThing" => "http://www.librarything.com/"
  }
  
  super(config)
end

Instance Method Details

#handle(request) ⇒ Object


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

def handle(request)
  image_url = image_url(request.referent)
  return request.dispatched(self, true) unless image_url

  uri = URI.parse(image_url)
  response = nil
  # All we need is a HEAD request to check content-length. 
  Net::HTTP.start(uri.host, uri.port) {|http|
    response = http.head(uri.path)
  }
  
  # Only way to know if we got an image or a transparent placeholder
  # is to check the content-length. Currently the transparent placeholder
  # is 43 bytes. -- not true anymore, now we can check for a redirect,
  # I guess.

  # Not sure why response is ever nil, but sometimes it is, let's log
  # some info.
  if ( response.kind_of?(Net::HTTPRedirection) && response["location"] == @lt404url)
    # no cover found.
    return request.dispatched(self, true)
  elsif ( response.nil? || response.content_length.nil? )
    Rails.logger.debug("CoverThing: Null response for #{uri}, status #{response.class}")
  end
  unless (response.nil? || response.content_length.nil? || response.content_length < 50)
    request.add_service_response(
      :service=>self, 
      :display_text => 'Cover Image',
      :key=> 'medium', 
      :url => image_url, 
      :size => 'medium',
      :service_type_value => :cover_image
      )
  end

  return request.dispatched(self, true)    
end

#image_url(referent) ⇒ Object


64
65
66
67
68
69
70
# File 'app/service_adaptors/cover_thing.rb', line 64

def image_url(referent)
  isbn = get_identifier(:urn, "isbn", referent)
  isbn.gsub!(/[^\d]/, '') if isbn # just numeric isbn
  return nil if isbn.blank? # need an isbn to make the request
  
  return @base_url + @developer_key + '/medium/isbn/' + isbn
end

#service_types_generatedObject


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

def service_types_generated
  return [ ServiceTypeValue[:cover_image] ]       
end