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



290
291
292
# File 'common/jsonmodel_client.rb', line 290

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

Instance Method Details

#add_error(field, message) ⇒ Object



401
402
403
404
405
# File 'common/jsonmodel_client.rb', line 401

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

#deleteObject



360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
# File 'common/jsonmodel_client.rb', line 360

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



349
350
351
352
353
354
355
356
357
# File 'common/jsonmodel_client.rb', line 349

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.



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
342
343
344
345
346
# File 'common/jsonmodel_client.rb', line 297

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



378
379
380
381
382
383
384
385
386
387
388
# File 'common/jsonmodel_client.rb', line 378

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



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

def suppress
  set_suppressed(true)
end

#unsuppressObject



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

def unsuppress
  set_suppressed(false)
end