Class: Alma::LoanSet
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) }
end
|
Instance Attribute Details
#raw_response ⇒ Object
Returns the value of attribute raw_response.
11
12
13
|
# File 'lib/alma/loan_set.rb', line 11
def raw_response
@raw_response
end
|
#results ⇒ Object
Returns the value of attribute results.
11
12
13
|
# File 'lib/alma/loan_set.rb', line 11
def results
@results
end
|
Instance Method Details
#all ⇒ Object
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
= @search_args.merge({ limit:, offset: })
r = (offset == 0) ? self : single_record_class.where_user(user_id, )
unless r.empty?
r.map { |item| yielder << item }
offset += 100
end
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
|
#key ⇒ Object
71
72
73
|
# File 'lib/alma/loan_set.rb', line 71
def key
"item_loan"
end
|
#loggable ⇒ Object
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_class ⇒ Object
75
76
77
|
# File 'lib/alma/loan_set.rb', line 75
def single_record_class
Alma::Loan
end
|
#success? ⇒ 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
|