class EvergreenHoldings::Connection

Attributes

org_units[R]

Public Class Methods

new(evergreen_domain) click to toggle source

Create a new object with the evergreen_domain specified, e.g. libcat.linnbenton.edu

Usage: `conn = EvergreenHoldings::Connection.new 'gapines.org'`

# File lib/evergreen_holdings.rb, line 20
def initialize(evergreen_domain)
  @evergreen_domain = evergreen_domain
  @gateway = URI evergreen_domain + OSRF_PATH
  @acpl_cache = {}
  fetch_idl_order
  raise CouldNotConnectToEvergreenError unless fetch_statuses

  fetch_ou_tree
end

Public Instance Methods

get_holdings(tcn, options = {}) click to toggle source

Fetch holdings data from the Evergreen server Returns a Status object

Usage: `stat = conn.get_holdings 23405` If you just want holdings at a specific org_unit: `my_connection.get_holdings 23405, org_unit: 5`

# File lib/evergreen_holdings.rb, line 35
def get_holdings(tcn, options = {})
  if options.key?(:org_unit)
    if options[:descendants]
      params = "format=json&input_format=json&service=open-ils.cat&method=open-ils.cat.asset.copy_tree.retrieve&param=auth_token_not_needed_for_this_call&param=#{tcn}"
      @org_units[options[:org_unit]][:descendants]&.each do |ou|
        params << + "&param=#{ou}"
      end
    else
      params = "format=json&input_format=json&service=open-ils.cat&method=open-ils.cat.asset.copy_tree.retrieve&param=auth_token_not_needed_for_this_call&param=#{tcn}&param=#{options[:org_unit]}"
    end
  else
    params = "format=json&input_format=json&service=open-ils.cat&method=open-ils.cat.asset.copy_tree.global.retrieve&param=auth_token_not_needed_for_this_call&param=#{tcn}"
  end
  @gateway.query = params

  res = send_query
  return Status.new res.body, @idl_order, self if res
end
location_name(id) click to toggle source

Given an ID, returns a human-readable name

# File lib/evergreen_holdings.rb, line 55
def location_name(id)
  @acpl_cache.fetch(id) { |id| fetch_new_acpl(id) || id }
end
ou_name(id) click to toggle source
# File lib/evergreen_holdings.rb, line 63
def ou_name(id)
  @org_units[id][:name]
end
status_name(id) click to toggle source
# File lib/evergreen_holdings.rb, line 59
def status_name(id)
  @possible_item_statuses[id]
end

Private Instance Methods

add_ou_descendants(id, parent) click to toggle source
# File lib/evergreen_holdings.rb, line 69
def add_ou_descendants(id, parent)
  (@org_units[parent][:descendants] ||= []) << id
  add_ou_descendants id, @org_units[parent][:parent] if @org_units[parent][:parent]
end
fetch_idl_order() click to toggle source
# File lib/evergreen_holdings.rb, line 114
def fetch_idl_order
  begin
    idl = Nokogiri::XML(URI.parse("#{@evergreen_domain}/reports/fm_IDL.xml").open)
  rescue Errno::ECONNREFUSED, Net::ReadTimeout, OpenURI::HTTPError
    raise CouldNotConnectToEvergreenError
  end

  @idl_order = IDLParser.new(idl).field_order_by_class %i[acn acp acpl aou ccs circ]
end
fetch_new_acpl(id) click to toggle source

Given the ID of a shelving location, this method finds the name of the location, caches it, and returns it

# File lib/evergreen_holdings.rb, line 90
def fetch_new_acpl(id)
  params = "format=json&input_format=json&service=open-ils.circ&method=open-ils.circ.copy_location.retrieve&param=#{id}"
  @gateway.query = params
  res = send_query
  if res
    data = JSON.parse(res.body)['payload'][0]
    name = data['__p'][@idl_order[:acpl]['name']] unless data.key? 'stacktrace'
    @acpl_cache[id] = name
    return name if name
  end
  false
end
fetch_ou_tree() click to toggle source
# File lib/evergreen_holdings.rb, line 139
def fetch_ou_tree
  @org_units = {}
  params = 'format=json&input_format=json&service=open-ils.actor&method=open-ils.actor.org_tree.retrieve'
  @gateway.query = params
  res = send_query
  if res
    raw_orgs = JSON.parse(res.body)['payload'][0]['__p']
    take_info_from_ou_tree raw_orgs
    return true unless @org_units.empty?
  end
  false
end
fetch_statuses() click to toggle source
# File lib/evergreen_holdings.rb, line 124
def fetch_statuses
  @possible_item_statuses = []
  params = 'format=json&input_format=json&service=open-ils.search&method=open-ils.search.config.copy_status.retrieve.all'
  @gateway.query = params
  res = send_query
  if res
    stats = JSON.parse(res.body)['payload'][0]
    stats.each do |stat|
      @possible_item_statuses[stat['__p'][@idl_order[:ccs]['id']]] = stat['__p'][@idl_order[:ccs]['name']]
    end
    return true unless stats.empty?
  end
  false
end
send_query() click to toggle source
# File lib/evergreen_holdings.rb, line 103
def send_query
  begin
    res = Net::HTTP.get_response(@gateway)
  rescue Errno::ECONNREFUSED, Net::ReadTimeout
    return nil
  end
  return res if res.is_a?(Net::HTTPSuccess)

  nil
end
take_info_from_ou_tree(o) click to toggle source
# File lib/evergreen_holdings.rb, line 74
def take_info_from_ou_tree(o)
  id = o[@idl_order[:aou]['id']]
  @org_units[id] = {}
  @org_units[id][:name] = o[@idl_order[:aou]['name']]
  if o[@idl_order[:aou]['parent_ou']]
    @org_units[id][:parent] = o[@idl_order[:aou]['parent_ou']]
    add_ou_descendants id, o[@idl_order[:aou]['parent_ou']]
  end
  o[@idl_order[:aou]['children']].each do |p|
    take_info_from_ou_tree p['__p']
  end
end