module Voyager class Status attr_reader :statuses def initialize(holdings) @statuses = parse_holdings_status(holdings) end def to_hash(args = {}) @statuses end private def parse_holdings_status(holdings) status_hash = combine_statuses(holdings) holdings_hash = {} status_hash.each_pair { |mfhdid,items| holdings_hash[mfhdid.to_s] = determine_status(items) } holdings_hash end def combine_statuses(holdings) statuses = {} # for each holding, for each item create an array of status codes # we don't need bibid becasue each Collection call processes one bib holdings.each do |holding| bibid, mfhdid, itemid, status = holding.values unless itemid.nil? statuses[mfhdid] ||= {} statuses[mfhdid][itemid] ||= [] statuses[mfhdid][itemid] << status else statuses[mfhdid] = {'no_items' => []} end end statuses end # 1 Not Charged # 2 Charged # 3 Renewed # 4 Overdue # 5 Recall Request # 6 Hold Request # 7 On Hold # 8 In Transit # 9 In Transit Discharged no longer used but appear in records # 10 In Transit On Hold no longer used but appear in records # 11 Discharged # 12 Missing # 13 Lost--Library Applied # 14 Lost--System Applied # 15 Claims Returned x # 16 Damaged x # 17 Withdrawn skip item # 18 At Bindery # 19 Cataloging Review x # 20 Circulation Review x # 21 Scheduled # 22 In Process # 23 Call Slip Request # 24 Short Loan Request # 25 Remote Storage Request def determine_status(items) return 'none' if items.has_key?('no_items') icnt = 0 # count of items ucnt = 0 # count of unavailable items items.each_pair do |itemid,statuses| # skip withdrawn unless statuses.include?(17) icnt += 1 # the overwhelming majority of items have only one status if statuses.length == 1 ucnt += 1 unless [1,9,11].any? {|code| statuses.include?(code)} else # some unavalable statuses can be in combination with 1, 9 and 11 so we test for them first if [2,3,4,5,6,7,8,10,12,13,14,18,21,22,23,24,25].any? {|code| statuses.include?(code)} ucnt += 1 end end end end return 'none' if icnt == 0 return 'available' if ucnt == 0 return 'unavailable' if icnt == ucnt return 'some_available' end end end