Class: Alma::AvailabilityResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/alma/availability_response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ AvailabilityResponse

Returns a new instance of AvailabilityResponse.



9
10
11
# File 'lib/alma/availability_response.rb', line 9

def initialize(response)
  @availability = parse_bibs_data(response.each)
end

Instance Attribute Details

#availabilityObject

Returns the value of attribute availability.



7
8
9
# File 'lib/alma/availability_response.rb', line 7

def availability
  @availability
end

Instance Method Details

#build_holdings_for(bib) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/alma/availability_response.rb', line 22

def build_holdings_for(bib)
  holdings = []

  get_inventory_fields_for(bib).map do |inventory_field|
    h = {}

    # Use the mapping for this inventory type
    subfield_codes = Alma::INVENTORY_SUBFIELD_MAPPING[inventory_field["tag"]]
    h["inventory_type"] = subfield_codes["INVENTORY_TYPE"]

    inventory_field.
      # Get all the subfields for this inventory field
      fetch("subfield", []).
      # Limit to only subfields codes for which we have a mapping
      select { |sf| subfield_codes.key? sf["code"] }.each { |f|
      key = subfield_codes[f["code"]]
      unless key.blank? || f["content"].blank?
        if h.key? key
          h[key] << " " + f["content"]
        else
          h[key] = f["content"]
        end
      end
    }
    holdings << h
  end
  holdings
end

#get_inventory_fields_for(bib) ⇒ Object



51
52
53
54
55
56
# File 'lib/alma/availability_response.rb', line 51

def get_inventory_fields_for(bib)
  # Return only the datafields with tags AVA, AVD, or AVE
  bib.record
    .fetch("datafield", [])
    .select { |df| Alma::INVENTORY_SUBFIELD_MAPPING.key?(df["tag"]) }
end

#parse_bibs_data(bibs) ⇒ Object

Data structure for holdings information of bib records. A hash with mms ids as keys, with values of an array of one or more hashes of holdings info



16
17
18
19
20
# File 'lib/alma/availability_response.rb', line 16

def parse_bibs_data(bibs)
  bibs.reduce(Hash.new) { |acc, bib|
    acc.merge({ "#{bib.id}" => { holdings: build_holdings_for(bib) } })
  }
end