class Skala::AlephAdapter::GetUserLoans

Public Instance Methods

call(username, options = {}) click to toggle source
# File lib/skala/aleph_adapter/get_user_loans.rb, line 9
def call(username, options = {})
  resolved_user_id = resolve_user(username)
  loans_type = options[:type] == :history ? :history : nil
  aleph_options = { type: loans_type, view: :full }.compact
  raw_aleph_response = adapter.restful_api.patron(resolved_user_id).circulationActions.loans.get(aleph_options)

  if raw_aleph_response.include?("<error>")
    return nil
  else
    self.class::Result.new(
      loans: Nokogiri::XML(raw_aleph_response).xpath("//loan").map do |_loan|
        {
          id: id(_loan),
          due_date: due_date(_loan),
          fine: fine(_loan),
          loan_date: loan_date(_loan),
          record: {
            id: ils_record_id(_loan),
            title: title(_loan)
          },
          renewable: renewable(_loan),
          returned_date: returned_date(_loan),
          signature: signature(_loan)
        }
      end,
      source: raw_aleph_response
    )
  end
end

Private Instance Methods

due_date(loan) click to toggle source
# File lib/skala/aleph_adapter/get_user_loans.rb, line 45
def due_date(loan)
  if _due_date = loan.at_xpath(".//z36-due-date|.//z36h-due-date").try(:content)
    Date.strptime(_due_date, "%Y%m%d")
  end
end
fine(loan) click to toggle source
# File lib/skala/aleph_adapter/get_user_loans.rb, line 57
def fine(loan)
  loan.at_xpath(".//fine").try(:content).presence.try(:to_f)
end
id(loan) click to toggle source
# File lib/skala/aleph_adapter/get_user_loans.rb, line 41
def id(loan)
  loan.attr("href")[/[^\/]+\Z/][/\A[^?]+/]
end
ils_record_id(element) click to toggle source
# File lib/skala/aleph_adapter/get_user_loans.rb, line 61
def ils_record_id(element)
  element.at_xpath("./z13/z13-doc-number").try(:content).presence
end
loan_date(loan) click to toggle source
# File lib/skala/aleph_adapter/get_user_loans.rb, line 51
def loan_date(loan) # only available in full view
  if _loan_date = loan.at_xpath(".//z36-loan-date|.//z36h-loan-date").try(:content)
    Date.strptime(_loan_date, "%Y%m%d")
  end
end
renewable(loan) click to toggle source
# File lib/skala/aleph_adapter/get_user_loans.rb, line 65
def renewable(loan)
  z36_last_renew_date = loan.at_xpath("./z36/z36-last-renew-date").try(:content).presence

  # maybe its present but "00000000" or something else non valid
  last_renew_date = begin
    z36_last_renew_date.try do |_date|
      Date.strptime(_date, "%Y%m%d")
    end
  rescue ArgumentError
    nil
  end

  loan.attr("renew") == "Y" && (!last_renew_date || last_renew_date < Date.today)
end
returned_date(loan) click to toggle source
# File lib/skala/aleph_adapter/get_user_loans.rb, line 80
def returned_date(loan)
  if _returned_date = loan.at_xpath(".//z36h-returned-date").try(:content)
    Date.strptime(_returned_date, "%Y%m%d")
  end
end
signature(element) click to toggle source
# File lib/skala/aleph_adapter/get_user_loans.rb, line 90
def signature(element)
  element.at_xpath("./z13/z13-call-no").try(:content).presence
end
title(element) click to toggle source
# File lib/skala/aleph_adapter/get_user_loans.rb, line 86
def title(element)
  element.at_xpath("./z13/z13-title").try(:content).presence
end