Class: MBooks

Inherits:
Service show all
Includes:
MetadataHelper
Defined in:
lib/service_adaptors/m_books.rb

Constant Summary

Constant Summary

Constants inherited from Service

Service::LinkOutFilterTask, Service::StandardTask

Instance Attribute Summary (collapse)

Attributes inherited from Service

#name, #priority, #request, #service_id, #session_id, #status, #task

Instance Method Summary (collapse)

Methods inherited from Service

#handle_wrapper, #link_out_filter, #preempted_by, required_config_params, #response_to_view_data, #session, #update_session, #view_data_from_service_type

Constructor Details

- (MBooks) initialize(config)

A new instance of MBooks



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/service_adaptors/m_books.rb', line 39

def initialize(config)
  @url = 'http://mirlyn-classic.lib.umich.edu/cgi-bin/sdrsmd?'
  @hathi_search_url = 'https://babel.hathitrust.org/shcgi/ptsearch'
  # HT links are handle.net, but we can't really use Shib login with
  # handle.net, so we hard-code the particular host that the handle.net
  # resolves to, sorry! 
  @hathi_link_url = "https://babel.hathitrust.org/shcgi/pt"
  @display_name = 'HathiTrust'
  @num_full_views = 1
  @note =  '' #'Fulltext books from the University of Michigan'
  @show_search_inside = true
  super(config)
end

Instance Attribute Details

- (Object) display_name (readonly)

Returns the value of attribute display_name



30
31
32
# File 'lib/service_adaptors/m_books.rb', line 30

def display_name
  @display_name
end

- (Object) note (readonly)

Returns the value of attribute note



30
31
32
# File 'lib/service_adaptors/m_books.rb', line 30

def note
  @note
end

- (Object) url (readonly)

Returns the value of attribute url



30
31
32
# File 'lib/service_adaptors/m_books.rb', line 30

def url
  @url
end

Instance Method Details

- (Object) clean_response(resp)

We're only interested in the 'sdr's and only those that have some rights



115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/service_adaptors/m_books.rb', line 115

def clean_response(resp)
  cleaned_response = []
  # because of the structure of the response we recurse through it to get
  # what we're after. OK, this is a bit of premature optimization since we
  # only have one response returned right now.
  resp['result'].each_value do |id_value|
    return nil if id_value.nil?
    id_value.each do |hit|
      cleaned_response << hit['sdr'] unless hit['sdr'].nil?
    end
  end
  cleaned_response
end

- (Object) create_fulltext_service_response(request, data)

FIXME abstract this out for use with both GBS and MBooks



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/service_adaptors/m_books.rb', line 130

def create_fulltext_service_response(request, data)
  display_name = @display_name
  
  full_views = data.select{|d| d['rights'] == 'full'}
  return nil if full_views.empty?
  count = 0
  full_views.each do |fv|
    request.add_service_response(
      {:service=>self, 
        :display_text=>display_name, 
        :url=>@hathi_link_url + '?id=' + fv['handle'], 
        :notes=> @note}, 
      [ :fulltext ]) 
    count += 1
    break if count == @num_full_views
  end   
  return true
end

- (Object) do_query(params)

conducts query and parses the JSON



109
110
111
112
# File 'lib/service_adaptors/m_books.rb', line 109

def do_query(params)
  link = @url + params
  return JSON.parse( open(link).read )
end

- (Object) do_search_inside(request, data)



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/service_adaptors/m_books.rb', line 149

def do_search_inside(request, data)

  
  search_views = data.select{|d| d['rights'] == 'searchonly' || d['rights'] == 'full'}

  return if search_views.blank?
  
  search_view = search_views.first

  request.add_service_response( 
      {:service => self,
      :display_text=>@display_name,
      :url=> @hathi_search_url + '?id=' +  
        search_view["handle"]},
      [:search_inside]
     )
end

- (Object) get_bibkey_parameters(rft) {|isbn, lccn, oclcnum| ... }

method that takes a referent and a block for parameter creation The block receives isbn, lccn, oclcnum and is responsible for formatting the parameters for the particular service FIXME consider moving this into metadata_helper

Yields:

  • (isbn, lccn, oclcnum)


100
101
102
103
104
105
106
# File 'lib/service_adaptors/m_books.rb', line 100

def get_bibkey_parameters(rft)
  isbn = get_identifier(:urn, "isbn", rft)
  oclcnum = get_identifier(:info, "oclcnum", rft)
  lccn = get_lccn(rft)
      
  yield(isbn, lccn, oclcnum)    
end

- (Object) get_parameters(rft)

just a wrapper around get_bibkey_parameters



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/service_adaptors/m_books.rb', line 78

def get_parameters(rft)
  # API supports oclcnum, isbn, or lccn, and can provide more than one of each. 
  get_bibkey_parameters(rft) do |isbn, lccn, oclcnum|
    # Prefer ISBN, best chance of a hit. Else oclc, else lccn.   
    keys = Array.new
    
    keys << "isbn=" + CGI.escape(isbn) unless isbn.blank?      
    keys << "oclc=" + CGI.escape(oclcnum) unless oclcnum.blank?    
    keys <<  "lccn=" + CGI.escape(lccn) unless lccn.blank?

    if keys.length > 0
      return keys.join("&")
    else
      return nil
    end
  end
end

- (Object) get_viewability(request)



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/service_adaptors/m_books.rb', line 58

def get_viewability(request)    
  params = get_parameters(request.referent)
  return nil if params.nil?
  mb_response = do_query(params)
  c_response = clean_response(mb_response)
  return nil if c_response.nil?
  
  # Only add fulltext if we're not skipping due to GBS
  if ( preempted_by(request, "fulltext"))
       RAILS_DEFAULT_LOGGER.debug("MBooks service: Skipping due to pre-emption")
  else
       full_views_shown = create_fulltext_service_response(request, c_response)
  end
  

  do_search_inside(request, c_response)
      
end

- (Object) handle(request)



53
54
55
56
# File 'lib/service_adaptors/m_books.rb', line 53

def handle(request)
  get_viewability(request)
  return request.dispatched(self, true)
end

- (Object) response_url(service_type, submitted_params)

Handle search_inside



169
170
171
172
173
174
175
176
177
178
179
# File 'lib/service_adaptors/m_books.rb', line 169

def response_url(service_type, )
  if ( ! (service_type.service_type_value.name == "search_inside" ))
    return super(service_type, )
  else
    base = service_type.service_response[:url]      
    query = CGI.escape(["query"] || "")
    url = base + "&q1=#{query}"

    return url
  end
end

- (Object) service_types_generated



32
33
34
35
36
37
# File 'lib/service_adaptors/m_books.rb', line 32

def service_types_generated
  types = [ ServiceTypeValue[:fulltext] ]
  types << ServiceTypeValue[:search_inside] if @show_search_inside

  return types
end