Sha256: 34aa7825e37a8586cf2ede15079149862b4428f7db9de380bca849c2419f2f40

Contents?: true

Size: 1.67 KB

Versions: 2

Compression:

Stored size: 1.67 KB

Contents

require 'nokogiri'
require 'berkeley_library/util'
require 'berkeley_library/holdings/oclc_number'
require 'berkeley_library/holdings/world_cat/symbols'

module BerkeleyLibrary
  module Holdings
    module WorldCat
      # @see https://developer.api.oclc.org/wcv1#/Holdings
      class LibrariesRequest
        include BerkeleyLibrary::Util

        XPATH_INST_ID_VALS = '/holdings/holding/institutionIdentifier/value'.freeze

        attr_reader :oclc_number, :symbols

        def initialize(oclc_number, symbols: Symbols::ALL)
          @oclc_number = OCLCNumber.ensure_oclc_number!(oclc_number)
          @symbols = Symbols.ensure_valid!(symbols)
        end

        def uri
          @uri ||= URIs.append(holdings_base_uri, URIs.path_escape(oclc_number))
        end

        # TODO: Check that this works w/more than 10 results
        #       See https://developer.api.oclc.org/wcv1#/Holdings
        def params
          @params ||= {
            'oclcsymbol' => symbols.join(','),
            'servicelevel' => 'full',
            'frbrGrouping' => 'off',
            'wskey' => Config.api_key
          }
        end

        def execute
          response_body = URIs.get(uri, params:, log: false)
          holdings_syms = holdings_from(response_body)
          holdings_syms.select { |sym| symbols.include?(sym) } # just in case
        end

        private

        def holdings_base_uri
          URIs.append(Config.base_uri, 'catalog', 'content', 'libraries')
        end

        def holdings_from(xml)
          xml_doc = Nokogiri::XML(xml)
          id_vals = xml_doc.xpath(XPATH_INST_ID_VALS)
          id_vals.filter_map { |value| value.text.strip }
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
berkeley_library-holdings-1.0.5 lib/berkeley_library/holdings/world_cat/libraries_request.rb
berkeley_library-holdings-1.0.4 lib/berkeley_library/holdings/world_cat/libraries_request.rb