class Alma::AvailabilityResponse

Attributes

availability[RW]

Public Class Methods

new(response) click to toggle source
# File lib/alma/availability_response.rb, line 9
def initialize(response)
  @availability = parse_bibs_data(response.each)
end

Public Instance Methods

build_holdings_for(bib) click to toggle source
# File lib/alma/availability_response.rb, line 22
def build_holdings_for(bib)
  get_inventory_fields_for(bib).map do |inventory_field|
    # Use the mapping for this inventory type
    subfield_codes = Alma::INVENTORY_SUBFIELD_MAPPING[inventory_field["tag"]]

    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"] }.
      # Transform the array of subfields into a hash with mapped code as key
      reduce(Hash.new) { |acc, subfield|
         acc.merge({ "#{subfield_codes[subfield['code']]}" => subfield["content"] })
       }.
      # Include the inventory type
      merge({ "inventory_type" => subfield_codes["INVENTORY_TYPE"] })
  end
end
get_inventory_fields_for(bib) click to toggle source
# File lib/alma/availability_response.rb, line 41
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) click to toggle source

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

# 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