Class: Worldcat

Inherits:
Service show all
Includes:
MetadataHelper, UmlautHttp
Defined in:
app/service_adaptors/worldcat.rb

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 UmlautHttp

#http_fetch, #proxy_like_headers

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) ⇒ Worldcat

Returns a new instance of Worldcat.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/service_adaptors/worldcat.rb', line 14

def initialize(config)
  # defaults
  @suppress_precheck = false # it seems unneccesary to pre-check worldcat, it's mostly ALWAYS a positive hit. And pre-checking against worldcat is running into Worldcat's rate limiting defenses. If neccesary, you can turn this off. Really, we should be using the Worldcat API anyway. 
  @base_url = 'http://www.worldcat.org/'
  @display_text = 'Find in other libraries'
  @display_text_i18n = 'display_text'
  @display_name = 'OCLC WorldCat.org'
  
  @credits = {
    "OCLC WorldCat.org" => "http://www.worldcat.org/"
  }
  
  super(config)
end

Instance Method Details

#handle(request) ⇒ Object



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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'app/service_adaptors/worldcat.rb', line 33

def handle(request)
  isbn = get_identifier(:urn, "isbn", request.referent)
  issn = get_identifier(:urn, "issn", request.referent)
  oclcnum = get_identifier(:info, "oclcnum", request.referent)
  
  
  isxn_key = nil
  isxn_value = nil
  if (! oclcnum.blank?)
    isxn_key = 'oclc'
    isxn_value = oclcnum    
  elsif (! issn.blank?)
    isxn_key = 'issn'
    #isxn_value = ref_metadata['issn'] + '+dt:ser'
    isxn_value = issn
  elsif (! isbn.blank?)
    isxn_key = 'isbn'
    isxn_value = isbn
  else
    # We have no useful identifiers
    return request.dispatched(self, true)
  end

  # Do some cleanup of the value. Sometimes spaces or other
  # weird chars get in there, why not strip out everything that
  # isn't a number or X?
  isxn_value = isxn_value.gsub( /[^\dX]/, '')
  # and URL escape just to be safe, although really shouldn't be neccesary
  isxn_value = URI.escape( isxn_value )
  
  # We do a pre-emptive lookup to worldcat to try and see if worldcat
  # has a hit or not, before adding the link.
  isxn_key = URI.escape( isxn_key )
  uri_str = @base_url+isxn_key+'/'+isxn_value
  uri_str +=  "&loc=#{URI.escape(@search_zip_code.to_s)}" if @search_zip_code

  
  begin
    worldcat_uri = URI.parse(uri_str)
  rescue Exception => e
    Rails.logger.error("Bad worldcat uri string constructed?")
    Rails.logger.error(e)
    return request.dispatched(self, DispatchedService::FailedFatal)
  end

  unless ( @suppress_precheck )
  
    http = Net::HTTP.new worldcat_uri.host
    http.open_timeout = 7
    http.read_timeout = 7

    
    begin
      # Fake being a proxy to send info on actual end-user client to worldcat,
      # to lessen chance of worldcat traffic limiters. 
      headers = proxy_like_headers( request, worldcat_uri.host )
      wc_response = http.get(worldcat_uri.path, headers)
    rescue  Timeout::Error => exception
      return request.dispatched(self, DispatchedService::FailedTemporary, exception)
    end

    # Bad response code?
    unless wc_response.code == "200"
      # Could be temporary, could be fatal. Let's say temporary. 
      return request.dispatched(self, DispatchedService::FailedTemporary, Exception.new("oclc returned error http status code: #{wc_response.code}"))
    end

    # Sadly, worldcat returns a 200 even if there are no matches.
    # We need to screen-scrape to discover if there are matches.
    if (wc_response.body =~ /The page you tried was not found\./)
      # Not found in worldcat, we won't add a link.
      return request.dispatched(self, true)
    end
  end
  
  request.add_service_response(
    :service=>self, 
    :url=>worldcat_uri.to_s,
    :display_text=>@display_text,
    :display_text_i18n => @display_text_i18n,
    :service_type_value => :highlighted_link
    )
  
  return request.dispatched(self, true)
end

#service_types_generatedObject



29
30
31
# File 'app/service_adaptors/worldcat.rb', line 29

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