Module: Median::Aleph::Cash

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

Instance Method Summary collapse

Instance Method Details

#get_cash(patron_id) ⇒ 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
# File 'lib/median/aleph/cash.rb', line 4

def get_cash(patron_id)
  xml = get_url("#{Median.config.aleph_rest_service_base_url}/patron/#{patron_id}/circulationActions/cash",
    view: 'brief'
  )

  cash = OpenStruct.new
  cash.balance = 0.0
  cash.charges = []

  # balance
  cash.balance = xml.at_xpath('//open-sum').try(:content).gsub!(/\s/, '').to_f
  # charges
  xml.xpath('//cash').each do |charge_xml|
    charge             = OpenStruct.new
    sum                = charge_xml.at_xpath('z31-sum').try(:content).try(:gsub, '(', '').try(:gsub, ')', '').to_f
    sum                = sum * -1 if sum != 0 and charge_xml.at_xpath('z31-credit-debit').try(:content).try(:strip).try(:downcase) == 'debit'
    charge.sum         = sum
    date               = charge_xml.at_xpath('z31-date').try(:content)
    charge.date        = date.present? ? Date.strptime(date, '%Y%m%d') : nil
    charge.author      = charge_xml.at_xpath('z13-author').try(:content)
    charge.title       = charge_xml.at_xpath('z13-title').try(:content)
    charge.year        = charge_xml.at_xpath('z13-year').try(:content)
    description        = charge_xml.at_xpath('z31-description').try(:content)
    charge.description = description if description.present? and description.downcase.strip != 'alle'
    cash.charges << charge
  end

  return cash
end