Class: Arvados::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/arvados.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.api_exec(method, parameters = {}) ⇒ Object



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/arvados.rb', line 214

def self.api_exec(method, parameters={})
  api_method = arvados_api.send(api_models_sym).send(method.name.to_sym)
  parameters.each do |k,v|
    parameters[k] = v.to_json if v.is_a? Array or v.is_a? Hash
  end
  # Look for objects expected by request.properties.(key).$ref and
  # move them from parameters (query string) to request body.
  body = nil
  method.discovery_document['request'].
    andand['properties'].
    andand.each do |k,v|
    if v.is_a? Hash and v['$ref']
      body ||= {}
      body[k] = parameters.delete k.to_sym
    end
  end
  result = client.
    execute(:api_method => api_method,
            :authenticated => false,
            :parameters => parameters,
            :body_object => body,
            :headers => {
              :authorization => 'Bearer '+arvados.config['ARVADOS_API_TOKEN']
            })
  resp = JSON.parse result.body, :symbolize_names => true
  if resp[:errors]
    if !resp[:errors][0].match(/.*req-[0-9a-zA-Z]{20}.*/)
      resp[:errors][0] += " (#{result.headers['X-Request-Id'] or client.request_id})"
    end
    raise Arvados::TransactionFailedError.new(resp[:errors])
  elsif resp[:uuid] and resp[:etag]
    self.new(resp)
  elsif resp[:items].is_a? Array
    resp.merge(:items => resp[:items].collect do |i|
                 self.new(i)
               end)
  else
    resp
  end
end

.arvados_apiObject



202
203
204
# File 'lib/arvados.rb', line 202

def self.arvados_api
  arvados.arvados_api
end

.clientObject



205
206
207
# File 'lib/arvados.rb', line 205

def self.client
  arvados.client
end

.debuglog(*args) ⇒ Object



208
209
210
# File 'lib/arvados.rb', line 208

def self.debuglog(*args)
  arvados.class.debuglog(*args)
end

Instance Method Details

#[](x) ⇒ Object



259
260
261
262
263
264
265
266
267
# File 'lib/arvados.rb', line 259

def [](x)
  if @attributes[x].is_a? Hash or @attributes[x].is_a? Array
    # We won't be notified via []= if these change, so we'll just
    # assume they are going to get changed, and submit them if
    # save() is called.
    @attributes_to_update[x] = @attributes[x]
  end
  @attributes[x]
end

#[]=(x, y) ⇒ Object



255
256
257
258
# File 'lib/arvados.rb', line 255

def []=(x,y)
  @attributes_to_update[x] = y
  @attributes[x] = y
end

#debuglog(*args) ⇒ Object



211
212
213
# File 'lib/arvados.rb', line 211

def debuglog(*args)
  self.class.arvados.class.debuglog(*args)
end

#saveObject



268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/arvados.rb', line 268

def save
  @attributes_to_update.keys.each do |k|
    @attributes_to_update[k] = @attributes[k]
  end
  j = self.class.api_exec :update, {
    :uuid => @attributes[:uuid],
    self.class.api_model_sym => @attributes_to_update.to_json
  }
  unless j.respond_to? :[] and j[:uuid]
    debuglog "Failed to save #{self.to_s}: #{j[:errors] rescue nil}", 0
    nil
  else
    @attributes_to_update = {}
    @attributes = j
  end
end