class Alma::RequestSet
Attributes
raw_response[R]
results[R]
Public Class Methods
new(raw_response)
click to toggle source
# 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, []) .map { |item| single_record_class.new(item) } end
Public Instance Methods
all()
click to toggle source
# File lib/alma/request_set.rb, line 34 def all Enumerator.new do |yielder| offset = 0 loop do r = (offset == 0) ? self : single_record_class.where_user(user_id, { limit: 100, offset: offset }) unless r.empty? r.map { |item| yielder << item } offset += 100 else raise StopIteration end end end end
each(&block)
click to toggle source
# File lib/alma/request_set.rb, line 49 def each(&block) @results.each(&block) end
key()
click to toggle source
# File lib/alma/request_set.rb, line 57 def key "user_request" end
loggable()
click to toggle source
# 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_class()
click to toggle source
# File lib/alma/request_set.rb, line 61 def single_record_class Alma::UserRequest end
success?()
click to toggle source
# File lib/alma/request_set.rb, line 53 def success? raw_response.response.code.to_s == "200" end
validate(response)
click to toggle source
# 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
Private Instance Methods
get_user_id_from_path(path)
click to toggle source
# File lib/alma/request_set.rb, line 70 def get_user_id_from_path(path) # Path in user api calls starts with "/almaws/v1/users/123/maybe_something/else" split_path = path.split("/") # the part immediately following the "users" is going to be the user_id user_id_index = split_path.index("users") + 1 split_path[user_id_index] end
user_id()
click to toggle source
# File lib/alma/request_set.rb, line 66 def user_id @user_id ||= get_user_id_from_path(raw_response.request.uri.path) end