1 class ArvadosResourceList
2 include ArvadosApiClientHelper
5 def initialize resource_class=nil
6 @resource_class = resource_class
7 @fetch_multiple_pages = true
8 @arvados_api_token = Thread.current[:arvados_api_token]
9 @reader_tokens = Thread.current[:reader_tokens]
17 def limit(max_results)
18 if not max_results.nil? and not max_results.is_a? Integer
19 raise ArgumentError("argument to limit() must be an Integer or nil")
30 def order(orderby_spec)
31 @orderby_spec = orderby_spec
35 def select(columns=nil)
36 # If no column arguments were given, invoke Enumerable#select.
54 @cond.keys.each do |uuid_key|
55 if @cond[uuid_key] and (@cond[uuid_key].is_a? Array or
56 @cond[uuid_key].is_a? ArvadosBase)
57 # Coerce cond[uuid_key] to an array of uuid strings. This
58 # allows caller the convenience of passing an array of real
59 # objects and uuids in cond[uuid_key].
60 if !@cond[uuid_key].is_a? Array
61 @cond[uuid_key] = [@cond[uuid_key]]
63 @cond[uuid_key] = @cond[uuid_key].collect do |item|
64 if item.is_a? ArvadosBase
72 @cond.keys.select { |x| x.match /_kind$/ }.each do |kind_key|
73 if @cond[kind_key].is_a? Class
74 @cond = @cond.merge({ kind_key => 'arvados#' + arvados_api_client.class_kind(@cond[kind_key]) })
80 def fetch_multiple_pages(f)
81 @fetch_multiple_pages = f
97 @items_available = r.items_available if r.respond_to? :items_available
98 @result_limit = r.limit if r.respond_to? :limit
99 @result_offset = r.offset if r.respond_to? :offset
100 @result_links = r.links if r.respond_to? :links
112 self.each_page do |items|
130 results.send('[]', *x)
142 Hash[self.collect { |x| [x.uuid, x] }]
165 # Return links provided with API response that point to the
166 # specified object, and have the specified link_class. If link_class
167 # is false or omitted, return all links pointing to the specified
169 def links_for item_or_uuid, link_class=false
170 return [] if !result_links
171 unless @links_for_uuid
173 result_links.each do |link|
174 if link.respond_to? :head_uuid
175 @links_for_uuid[link.head_uuid] ||= []
176 @links_for_uuid[link.head_uuid] << link
180 if item_or_uuid.respond_to? :uuid
181 uuid = item_or_uuid.uuid
185 (@links_for_uuid[uuid] || []).select do |link|
186 link_class == false or link.link_class == link_class
196 api_params[:where] = @cond if @cond
197 api_params[:eager] = '1' if @eager
198 api_params[:limit] = @limit if @limit
199 api_params[:select] = @select if @select
200 api_params[:order] = @orderby_spec if @orderby_spec
201 api_params[:filters] = @filters if @filters
204 offset = @offset || 0
207 api_params[:offset] = offset
208 api_params[:limit] = (@limit - item_count) if @limit
210 res = arvados_api_client.api(@resource_class, '', api_params,
211 arvados_api_token: @arvados_api_token,
212 reader_tokens: @reader_tokens)
213 items = arvados_api_client.unpack_api_response res
215 break if items.nil? or not items.any?
217 @items_available = items.items_available if items.respond_to? :items_available
218 @result_limit = items.limit if items.respond_to? :limit
219 @result_offset = items.offset if items.respond_to? :offset
221 item_count += items.size
222 offset = @result_offset + items.size
226 break if @limit.is_a? Integer and item_count >= @limit
227 break if items.respond_to? :items_available and offset >= items.items_available
228 end while @fetch_multiple_pages