Sha256: 8ab685685579307cd2defa72db035dfd7f6aed42308b1e08ca9a4dc789dbfca4

Contents?: true

Size: 1.65 KB

Versions: 1

Compression:

Stored size: 1.65 KB

Contents

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?("<error>")
      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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
skala-1.1.0 lib/skala/aleph_adapter/get_user.rb