Sha256: 47eb828966aeacaf5656d7e920e38fed092ad01bf672001185a074094866e9ca

Contents?: true

Size: 1.74 KB

Versions: 2

Compression:

Stored size: 1.74 KB

Contents

# frozen_string_literal: true

module Alma
  class LoanSet < ResultSet
    class ResponseError < Alma::StandardError
    end

    alias :total_records :total_record_count


    attr_reader :results, :raw_response
    def_delegators :results, :empty?

    def initialize(raw_response, search_args = {})
      @raw_response = raw_response
      @response = raw_response.parsed_response
      @search_args = search_args
      validate(raw_response)
      @results = @response.fetch(key, [])
        .map { |item| single_record_class.new(item) }
      # args passed to the search that returned this set
      # such as limit, expand, order_by, etc
    end

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

    def validate(response)
      if response.code != 200
        error = "Could not find loans info."
        log = loggable.merge(response.parsed_response)
        raise ResponseError.new(error, log)
      end
    end

    def all
      Enumerator.new do |yielder|
        offset = 0
        loop do
          extra_args = @search_args.merge({ limit: 100, offset: offset })
          r = (offset == 0) ? self : single_record_class.where_user(user_id, extra_args)
          unless r.empty?
            r.map { |item| yielder << item }
            offset += 100
          else
            raise StopIteration
          end
        end
      end
    end

    def each(&block)
      @results.each(&block)
    end

    def success?
      raw_response.response.code.to_s == "200"
    end

    def key
      "item_loan"
    end

    def single_record_class
      Alma::Loan
    end

    private
      def user_id
        @user_id ||= results.first.user_id
      end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
alma-0.3.3 lib/alma/loan_set.rb
alma-0.3.2 lib/alma/loan_set.rb