Sha256: c7ba4b5f541f00351adcc4a794610940fd91481856543674c8e60fdae9fe3d5b

Contents?: true

Size: 859 Bytes

Versions: 3

Compression:

Stored size: 859 Bytes

Contents

# frozen_string_literal: true

require "forwardable"

class Alma::ResultSet
  extend ::Forwardable
  include Enumerable
  include Alma::Error

  attr_reader :response

  def_delegators :response, :[], :fetch
  def_delegators :each, :each_with_index, :size

  def initialize(response_body_hash)
    @response = response_body_hash
  end

  def loggable
    { uri: @response&.request&.uri&.to_s }
      .select { |k, v| !(v.nil? || v.empty?) }
  end

  def each
    @results ||= @response.fetch(key, [])
      .map { |item| single_record_class.new(item) }
  end

  def total_record_count
    fetch("total_record_count", 0).to_i
  end
  alias :total_records :total_record_count

  protected
    def key
      raise NotImplementedError "Subclasses of ResultSet need to define a response key"
    end

    def single_record_class
      Alma::AlmaRecord
    end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
alma-0.3.3 lib/alma/result_set.rb
alma-0.3.2 lib/alma/result_set.rb
alma-0.3.1 lib/alma/result_set.rb