Sha256: 3ee62a0477f0783cf793432774c9655238f4433fecc5d8458d2a92ee1d2478a5

Contents?: true

Size: 867 Bytes

Versions: 8

Compression:

Stored size: 867 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

8 entries across 8 versions & 1 rubygems

Version Path
alma-0.6.2 lib/alma/result_set.rb
alma-0.6.1 lib/alma/result_set.rb
alma-0.6.0 lib/alma/result_set.rb
alma-0.5.1 lib/alma/result_set.rb
alma-0.5.0 lib/alma/result_set.rb
alma-0.4.2 lib/alma/result_set.rb
alma-0.4.1 lib/alma/result_set.rb
alma-0.4.0 lib/alma/result_set.rb