<commit_before># Service that searches MBooks from the University of Michigan
# Currently limited since it only searches by OCLCnum 
# 
# Two possibilities are available for sdr rights "full" or "searchonly".
# The third possibility is that sdr will be null.
# 
# MBooks sometimes shows fulltext or search sometimes when GBS has lesser access
# FIXME Eventually this service will be able to offer search inside

class MBooks < Service
  require 'open-uri'
  require 'json'
  include MetadataHelper
  
  attr_reader :url, :display_name, :note
  
  # FIXME add search_inside later, which ought to be _very_ easy to do with MBooks
  def service_types_generated
    return[ 
      ServiceTypeValue[:fulltext],
      ServiceTypeValue[:highlighted_link]  ]
  end
  
  def initialize(config)
    @url = 'http://mirlyn.lib.umich.edu/cgi-bin/sdrsmd?'
    @display_name = 'MBooks'
    @num_full_views = 1
    super(config)
  end
  
  def handle(request)
    get_viewability(request)
    return request.dispatched(self, true)
  end
  
  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)
    # FIXME once we can search for more than one identifier at a time we'll
    #   need to dedupe our resultset
    full_views_shown = create_fulltext_service_response(request, c_response)
    
    unless full_views_shown
      do_web_links(request, c_response)
    end
    
  end
  
  # just a wrapper around get_bibkey_parameters
  def get_parameters(rft)
    # FIXME currently the API only supports oclcnum
    get_bibkey_parameters(rft) do |isbn, lccn, oclcnum|      
      return nil if oclcnum.nil?
      'oclc=' << oclcnum          
    end
  end
  
  # 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
  def get_bibkey_parameters(rft)
    isbn = get_identifier(:urn, "isbn", rft)
    oclcnum = get_identifier(:info, "oclcnum", rft)
    lccn = get_identifier(:info, "lccn", rft)
        
    yield(isbn, lccn, oclcnum)    
  end
  
  # conducts query and parses the JSON
  def do_query(params)
    link = @url + params
    return JSON.parse( open(link).read )
  end
  
  # We're only interested in the 'sdr's and only those that have some rights
  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.
    require 'pp'; pp resp
    resp['result'].each_value do |id_value|
      id_value.each do |hit|
        cleaned_response << hit['sdr'] unless hit['sdr'].nil?
      end
    end
    cleaned_response
  end
  
  # FIXME abstract this out for use with both GBS and MBooks
  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?<commit_msg>removed stupid puts debugging<commit_after># Service that searches MBooks from the University of Michigan
# Currently limited since it only searches by OCLCnum 
# 
# Two possibilities are available for sdr rights "full" or "searchonly".
# The third possibility is that sdr will be null.
# 
# MBooks sometimes shows fulltext or search sometimes when GBS has lesser access
# FIXME Eventually this service will be able to offer search inside

class MBooks < Service
  require 'open-uri'
  require 'json'
  include MetadataHelper
  
  attr_reader :url, :display_name, :note
  
  # FIXME add search_inside later, which ought to be _very_ easy to do with MBooks
  def service_types_generated
    return[ 
      ServiceTypeValue[:fulltext],
      ServiceTypeValue[:highlighted_link]  ]
  end
  
  def initialize(config)
    @url = 'http://mirlyn.lib.umich.edu/cgi-bin/sdrsmd?'
    @display_name = 'MBooks'
    @num_full_views = 1
    super(config)
  end
  
  def handle(request)
    get_viewability(request)
    return request.dispatched(self, true)
  end
  
  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)
    # FIXME once we can search for more than one identifier at a time we'll
    #   need to dedupe our resultset
    full_views_shown = create_fulltext_service_response(request, c_response)
    
    unless full_views_shown
      do_web_links(request, c_response)
    end
    
  end
  
  # just a wrapper around get_bibkey_parameters
  def get_parameters(rft)
    # FIXME currently the API only supports oclcnum
    get_bibkey_parameters(rft) do |isbn, lccn, oclcnum|      
      return nil if oclcnum.nil?
      'oclc=' << oclcnum          
    end
  end
  
  # 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
  def get_bibkey_parameters(rft)
    isbn = get_identifier(:urn, "isbn", rft)
    oclcnum = get_identifier(:info, "oclcnum", rft)
    lccn = get_identifier(:info, "lccn", rft)
        
    yield(isbn, lccn, oclcnum)    
  end
  
  # conducts query and parses the JSON
  def do_query(params)
    link = @url + params
    return JSON.parse( open(link).read )
  end
  
  # We're only interested in the 'sdr's and only those that have some rights
  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|
      id_value.each do |hit|
        cleaned_response << hit['sdr'] unless hit['sdr'].nil?
      end
    end
    cleaned_response
  end
  
  # FIXME abstract this out for use with both GBS and MBooks
  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?
{'fraction_non_alphanumeric': 0.0697857592942659, 'fraction_numerical': 0.000315059861373661, 'mean_word_length': 3.2898648648648647, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '222111', 'n_tokens_mistral': 1999, 'n_tokens_neox': 1958, 'n_words': 668}