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

.included(base) ⇒ Object



294
295
296
# File 'common/jsonmodel_client.rb', line 294

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

Instance Method Details

#add_error(field, message) ⇒ Object



405
406
407
408
409
# File 'common/jsonmodel_client.rb', line 405

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

#deleteObject



364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# File 'common/jsonmodel_client.rb', line 364

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

#refetchObject



353
354
355
356
357
358
359
360
361
# File 'common/jsonmodel_client.rb', line 353

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

#save(opts = {}, whole_body = false) ⇒ Object

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



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
342
343
344
345
346
347
348
349
350
# File 'common/jsonmodel_client.rb', line 301

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

#set_suppressed(val) ⇒ Object

Mark the suppression status of this record



382
383
384
385
386
387
388
389
390
391
392
# File 'common/jsonmodel_client.rb', line 382

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

#suppressObject



395
396
397
# File 'common/jsonmodel_client.rb', line 395

def suppress
  set_suppressed(true)
end

#unsuppressObject



400
401
402
# File 'common/jsonmodel_client.rb', line 400

def unsuppress
  set_suppressed(false)
end