Module: JSONModel::Client
- Defined in:
- lib/aspace_client/jsonmodel_client.rb
Defined Under Namespace
Modules: ClassMethods
Classes: EnumSource
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.included(base) ⇒ Object
283
284
285
|
# File 'lib/aspace_client/jsonmodel_client.rb', line 283
def self.included(base)
base.extend(ClassMethods)
end
|
Instance Method Details
#add_error(field, message) ⇒ Object
395
396
397
398
399
|
# File 'lib/aspace_client/jsonmodel_client.rb', line 395
def add_error(field, message)
@errors ||= {}
@errors[field.to_s] ||= []
@errors[field.to_s] << message
end
|
#delete ⇒ Object
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
|
# File 'lib/aspace_client/jsonmodel_client.rb', line 354
def delete
response = JSONModel::HTTP.delete_request(self.class.my_url(self.id))
if response.code == '200'
true
elsif response.code == '403'
raise AccessDeniedException.new
elsif response.code == '404'
nil
elsif response.code == '409'
err = ASUtils.json_parse(response.body)
raise ConflictException.new(err["error"])
else
raise Exception.new("Unknown response: #{response}")
end
end
|
#refetch ⇒ Object
343
344
345
346
347
348
349
350
351
|
# File 'lib/aspace_client/jsonmodel_client.rb', line 343
def refetch
return self if self.id.nil?
obj = (self.instance_data.has_key? :find_opts) ?
self.class.find(self.id, self.instance_data[:find_opts]) : self.class.find(self.id)
self.reset_from(obj) if not obj.nil?
end
|
#save(opts = {}, whole_body = false) ⇒ Object
Validate this JSONModel instance, produce a JSON string, and send an update to the backend.
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
|
# File 'lib/aspace_client/jsonmodel_client.rb', line 290
def save(opts = {}, whole_body = false)
clear_errors
type = self.class.record_type
response = JSONModel::HTTP.post_json(self.class.my_url(self.id, opts),
self.to_json)
if response.code == '200'
response = ASUtils.json_parse(response.body)
self.uri = self.class.uri_for(response["id"], opts)
self.lock_version = response["lock_version"]
if response["stale"]
self.refetch
end
return whole_body ? response : response["id"]
elsif response.code == '403'
raise AccessDeniedException.new
elsif response.code == '409'
err = ASUtils.json_parse(response.body)
raise ConflictException.new(err["error"])
elsif response.code == '404'
raise RecordNotFound.new
elsif response.code =~ /^4/
err = ASUtils.json_parse(response.body)
if err["error"].is_a?(Hash)
err["error"].each do |field, errors|
errors.each do |msg|
add_error(field, msg)
end
end
end
raise ValidationException.new(:invalid_object => self,
:errors => err["error"])
else
raise Exception.new("Unknown response: #{response.body} (code: #{response.code})")
end
end
|
#set_suppressed(val) ⇒ Object
Mark the suppression status of this record
372
373
374
375
376
377
378
379
380
381
382
|
# File 'lib/aspace_client/jsonmodel_client.rb', line 372
def set_suppressed(val)
response = JSONModel::HTTP.post_form("#{self.uri}/suppressed", :suppressed => val)
if response.code == '403'
raise AccessDeniedException.new("Permission denied when setting suppression status")
elsif response.code != '200'
raise "Error when setting suppression status for #{self}: #{response.code} -- #{response.body}"
end
self["suppressed"] = ASUtils.json_parse(response.body)["suppressed_state"]
end
|
#suppress ⇒ Object
385
386
387
|
# File 'lib/aspace_client/jsonmodel_client.rb', line 385
def suppress
set_suppressed(true)
end
|
#unsuppress ⇒ Object
390
391
392
|
# File 'lib/aspace_client/jsonmodel_client.rb', line 390
def unsuppress
set_suppressed(false)
end
|