Sha256: 227088035cfa053c5386cd733787ecc0a762a7b13eba07a91297cfcb7af07678

Contents?: true

Size: 1.57 KB

Versions: 5

Compression:

Stored size: 1.57 KB

Contents

require 'berkeley_library/holdings/hathi_trust/record_url_request_base'

module BerkeleyLibrary
  module Holdings
    module HathiTrust
      class RecordUrlBatchRequest
        include RecordUrlRequestBase

        # Per HathiTrust API docs: https://www.hathitrust.org/bib_api
        MAX_BATCH_SIZE = 20

        attr_reader :oclc_numbers

        def initialize(oclc_numbers)
          @oclc_numbers = ensure_valid_oclc_numbers!(oclc_numbers)
        end

        def execute
          response_body = URIs.get(uri, log: false)
          record_urls_from(response_body)
        end

        def uri
          @uri ||= URIs.append(volumes_base_uri, 'json', URIs.path_escape(oclc_list))
        end

        private

        def ensure_valid_oclc_numbers!(oclc_numbers)
          raise ArgumentError, 'No OCLC numbers provided' if oclc_numbers.empty?
          raise ArgumentError, "Too many OCLC numbers; expected <= #{MAX_BATCH_SIZE}, was #{oclc_numbers.size}" if oclc_numbers.size > MAX_BATCH_SIZE

          OCLCNumber.ensure_oclc_numbers!(oclc_numbers)
        end

        def oclc_list
          @oclc_list = oclc_numbers.map(&method(:key_for)).join('|')
        end

        def key_for(oclc_number)
          "oclc:#{oclc_number}"
        end

        def record_urls_from(json_str)
          json = JSON.parse(json_str)
          oclc_numbers.filter_map do |oclc_num|
            next unless (entry = json[key_for(oclc_num)])

            record_url = find_record_url(entry, oclc_num)
            [oclc_num, record_url] if record_url
          end.to_h
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
berkeley_library-holdings-1.0.5 lib/berkeley_library/holdings/hathi_trust/record_url_batch_request.rb
berkeley_library-holdings-1.0.4 lib/berkeley_library/holdings/hathi_trust/record_url_batch_request.rb
berkeley_library-holdings-1.0.3 lib/berkeley_library/holdings/hathi_trust/record_url_batch_request.rb
berkeley_library-holdings-1.0.2 lib/berkeley_library/holdings/hathi_trust/record_url_batch_request.rb
berkeley_library-holdings-1.0.1 lib/berkeley_library/holdings/hathi_trust/record_url_batch_request.rb