Module: Median::Aleph::Loan

Included in:
Median::Aleph
Defined in:
lib/median/aleph/loan.rb

Instance Method Summary collapse

Instance Method Details

#get_loans(patron_id, options = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/median/aleph/loan.rb', line 4

def get_loans(patron_id, options = {})
  options = options.reverse_merge(history: false)
  loans = []

  # load loans
  url = "#{Median.config.aleph_rest_service_base_url}/patron/#{patron_id}/circulationActions/loans"
  xml = options[:history] ?
    get_url(url, view: 'brief', type: 'history', no_loans: 250) :
    get_url(url, view: 'brief')

  # parse result
  xml.xpath('//loan').each do |loan_xml|
    loan = OpenStruct.new
    loan.id        = loan_xml.attribute('href').try(:content).try(:split, '/').try(:last)
    loan.can_renew = (loan_xml.attribute('renew').try(:content).try(:upcase) == 'Y') ? true : false
    loan.author    = loan_xml.at_xpath('z13-author').try(:content)
    loan.title     = loan_xml.at_xpath('z13-title').try(:content)
    loan.year      = loan_xml.at_xpath('z13-year').try(:content)
    return_date    = loan_xml.at_xpath('z36h-returned-date').try(:content)
    due_date       = loan_xml.at_xpath('z36-due-date').try(:content)
    loan.returned  = return_date.present? ? Date.strptime(return_date, '%Y%m%d') : nil
    loan.due       = due_date.present? ? Date.strptime(due_date, '%Y%m%d') : nil
    loans << loan
  end

  # sort by due date ascending
  loans.sort{|x,y| x.due <=> y.due }

  # return
  loans
end

#renew_loan(patron_id, loan_id) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/median/aleph/loan.rb', line 36

def renew_loan(patron_id, loan_id)
  xml = post_url("#{Median.config.aleph_rest_service_base_url}/patron/#{patron_id}/circulationActions/loans/#{loan_id}")

  result_code = xml.at_xpath('//reply-code').try(:content).to_i
  if result_code == 0 # no error
    return true
  end

  return false
end

#renew_loans(patron_id) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# File 'lib/median/aleph/loan.rb', line 47

def renew_loans(patron_id)
  xml = post_url("#{Median.config.aleph_rest_service_base_url}/patron/#{patron_id}/circulationActions/loans")

  result_code = xml.at_xpath('//reply-code').try(:content).to_i
  if result_code == 0 # no error
    return true
  end

  return false
end