Module: JSONModel::Client

Defined in:
common/jsonmodel_client.rb

Defined Under Namespace

Modules: ClassMethods Classes: EnumSource

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Object) included(base)



284
285
286
# File 'common/jsonmodel_client.rb', line 284

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

- (Object) add_error(field, message)



396
397
398
399
400
# File 'common/jsonmodel_client.rb', line 396

def add_error(field, message)
  @errors ||= {}
  @errors[field.to_s] ||= []
  @errors[field.to_s] << message
end

- (Object) delete



355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
# File 'common/jsonmodel_client.rb', line 355

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

- (Object) refetch



344
345
346
347
348
349
350
351
352
# File 'common/jsonmodel_client.rb', line 344

def refetch
  # if a new object, nothing to fetch
  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

- (Object) save(opts = {}, whole_body = false)

Validate this JSONModel instance, produce a JSON string, and send an update to the backend.



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
341
# File 'common/jsonmodel_client.rb', line 291

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)

    # If we were able to save successfully, increment our local version
    # number to match the version on the server.
    self.lock_version = response["lock_version"]

    # Ensure object is up to date
    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

- (Object) set_suppressed(val)

Mark the suppression status of this record



373
374
375
376
377
378
379
380
381
382
383
# File 'common/jsonmodel_client.rb', line 373

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

- (Object) suppress



386
387
388
# File 'common/jsonmodel_client.rb', line 386

def suppress
  set_suppressed(true)
end

- (Object) unsuppress



391
392
393
# File 'common/jsonmodel_client.rb', line 391

def unsuppress
  set_suppressed(false)
end