Class: Alma::RequestSet

Inherits:
ResultSet show all
Defined in:
lib/alma/request_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) ⇒ RequestSet

Returns a new instance of RequestSet.



13
14
15
16
17
18
19
# File 'lib/alma/request_set.rb', line 13

def initialize(raw_response)
  @raw_response = raw_response
  @response = raw_response.parsed_response
  validate(raw_response)
  @results = @response.fetch(key, []) || []
  @results.map! { |item| single_record_class.new(item) }
end

Instance Attribute Details

#raw_responseObject (readonly)

Returns the value of attribute raw_response.



10
11
12
# File 'lib/alma/request_set.rb', line 10

def raw_response
  @raw_response
end

#resultsObject (readonly)

Returns the value of attribute results.



10
11
12
# File 'lib/alma/request_set.rb', line 10

def results
  @results
end

Instance Method Details

#allObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/alma/request_set.rb', line 34

def all
  Enumerator.new do |yielder|
    offset = 0
    limit = 100

    loop do
      r = (offset == 0) ? self : single_record_class.where_user(user_id, { limit:, offset: })
      unless r.empty?
        r.map { |item| yielder << item }
        offset += 100
      end

      if r.empty? || r.count < limit
        raise StopIteration
      end
    end
  end
end

#each(&block) ⇒ Object



53
54
55
# File 'lib/alma/request_set.rb', line 53

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

#keyObject



61
62
63
# File 'lib/alma/request_set.rb', line 61

def key
  "user_request"
end

#loggableObject



21
22
23
24
# File 'lib/alma/request_set.rb', line 21

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

#single_record_classObject



65
66
67
# File 'lib/alma/request_set.rb', line 65

def single_record_class
  Alma::UserRequest
end

#success?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/alma/request_set.rb', line 57

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

#validate(response) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/alma/request_set.rb', line 26

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