Class: Alma::LoanSet

Inherits:
ResultSet show all
Defined in:
lib/alma/loan_set.rb

Defined Under Namespace

Classes: ResponseError

Instance Attribute Summary collapse

Attributes inherited from ResultSet

#response

Instance Method Summary collapse

Methods inherited from ResultSet

#total_record_count

Methods included from Error

#error, #error_message, #has_error?

Constructor Details

#initialize(raw_response, search_args = {}) ⇒ LoanSet

Returns a new instance of LoanSet.



14
15
16
17
18
19
20
21
22
23
# File 'lib/alma/loan_set.rb', line 14

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, []) || []
  @results.map! { |item| single_record_class.new(item) }
  # args passed to the search that returned this set
  # such as limit, expand, order_by, etc
end

Instance Attribute Details

#raw_responseObject (readonly)

Returns the value of attribute raw_response.



11
12
13
# File 'lib/alma/loan_set.rb', line 11

def raw_response
  @raw_response
end

#resultsObject (readonly)

Returns the value of attribute results.



11
12
13
# File 'lib/alma/loan_set.rb', line 11

def results
  @results
end

Instance Method Details

#allObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/alma/loan_set.rb', line 39

def all
  Enumerator.new do |yielder|
    offset = 0
    limit = 100
    loop do
      extra_args = @search_args.merge({ limit:, offset: })
      r = (offset == 0) ? self : single_record_class.where_user(user_id, extra_args)

      unless r.empty?
        r.map { |item| yielder << item }
        offset += 100
      end

      # TODO: r.count greater than "limit" doesn't make any sense unless the ALMA User/Loan API is broken.
      # We should remove this qualification in October once Alma fixes the bug they introduced in their
      # September release.
      # @see https://developers.exlibrisgroup.com/forums/topic/limit-and-offset-not-being-applied-to-retrieve-user-loans-api/
      if r.empty? || r.count < limit || r.count > limit
        raise StopIteration
      end
    end
  end
end

#each(&block) ⇒ Object



63
64
65
# File 'lib/alma/loan_set.rb', line 63

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

#keyObject



71
72
73
# File 'lib/alma/loan_set.rb', line 71

def key
  "item_loan"
end

#loggableObject



25
26
27
28
29
# File 'lib/alma/loan_set.rb', line 25

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

#single_record_classObject



75
76
77
# File 'lib/alma/loan_set.rb', line 75

def single_record_class
  Alma::Loan
end

#success?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/alma/loan_set.rb', line 67

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

#validate(response) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/alma/loan_set.rb', line 31

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