require "skala/adapter/get_user" require "skala/get_user_result" require_relative "../aleph_adapter" class Skala::AlephAdapter::GetUser < Skala::Adapter::GetUser def call(username, options = {}) user_library = options[:user_library] || @adapter.default_user_library username = username.upcase # ensure that username/id is always upcased for request/result raw_aleph_result = @adapter.x_services.post( op: :"bor-info", bor_id: username, library: user_library, cash: "B", hold: "P", loans: "P" ) if raw_aleph_result.include?("") return nil else fields = Hash.from_xml(raw_aleph_result) Skala::GetUserResult.new( cash_balance: cash_balance(fields), email_address: fields["bor_info"]["z304"]["z304_email_address"], expiry_date: Date.strptime(fields["bor_info"]["z305"]["z305_expiry_date"], "%d/%m/%Y"), id: username, first_name: fields["bor_info"]["z303"]["z303_name"].split(",").last.strip, last_name: fields["bor_info"]["z303"]["z303_name"].split(",").first.strip, number_of_hold_requests: [fields["bor_info"]["item_h"]].flatten(1).compact.length, number_of_loans: [fields["bor_info"]["item_l"]].flatten(1).compact.length, fields: fields ) end end private def cash_balance(fields) balance = fields["bor_info"]["balance"].to_f sign = fields["bor_info"]["sign"] loans = [fields["bor_info"]["item_l"]].flatten(1).compact current_fines = loans.map { |_loan| _loan["current_fine"].to_f } balance * (sign == "D" ? -1 : 1) - current_fines.inject(0.0) { |sum, fine| sum + (fine || 0.0) } end end