Module: JSONModel::Client::ClassMethods

Defined in:
common/jsonmodel_client.rb

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Object) extended(base)



405
406
407
408
409
410
411
412
413
414
415
416
417
418
# File 'common/jsonmodel_client.rb', line 405

def self.extended(base)
  class << base
    alias :_substitute_parameters :substitute_parameters

    def substitute_parameters(uri, opts = {})
      opts = ASUtils.keys_as_strings(opts)
      if JSONModel::repository
        opts = {'repo_id' => JSONModel::repository}.merge(opts)
      end

      _substitute_parameters(uri, opts)
    end
  end
end

Instance Method Details

- (Object) all(params = {}, opts = {})

Return all instances of the current JSONModel’s record type.



465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
# File 'common/jsonmodel_client.rb', line 465

def all(params = {}, opts = {})
  uri = my_url(nil, opts)

  uri.query = URI.encode_www_form(params)

  response = JSONModel::HTTP.get_response(uri)

  if response.code == '200'
    json_list = ASUtils.json_parse(response.body)

    if json_list.is_a?(Hash)
      json_list["results"] = json_list["results"].map {|h| self.new(h)}
    else
      json_list = json_list.map {|h| self.new(h)}
    end

    json_list
  elsif response.code == '403'
    raise AccessDeniedException.new
  else
    raise response.body
  end
end

- (Object) find(id, opts = {})

Given an ID, retrieve an instance of the current JSONModel from the backend.



441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
# File 'common/jsonmodel_client.rb', line 441

def find(id, opts = {})
  response = JSONModel::HTTP.get_response(my_url(id, opts))

  if response.code == '200'
    obj = self.new(ASUtils.json_parse(response.body))
    # store find params on instance to support #refetch
    obj.instance_data[:find_opts] = opts
    obj
  elsif response.code == '403'
    raise AccessDeniedException.new
  elsif response.code == '404'
    raise RecordNotFound.new
  else
    raise response.body
  end
end

- (Object) find_by_uri(uri, opts = {})



459
460
461
# File 'common/jsonmodel_client.rb', line 459

def find_by_uri(uri, opts = {})
  self.find(self.id_for(uri), opts)
end

- (Object) my_url(id = nil, opts = {})

Given the ID of a JSONModel instance, return its full URL (including the URL of the backend)



423
424
425
426
427
428
429
430
431
432
433
434
435
436
# File 'common/jsonmodel_client.rb', line 423

def my_url(id = nil, opts = {})
  uri, remaining_opts = self.uri_and_remaining_options_for(id, opts)
  url = URI("#{JSONModel::HTTP.backend_url}#{uri}")

  # Don't need to pass this as a URL parameter if it wasn't picked up by
  # the URI template substitution.
  remaining_opts.delete(:repo_id)

  if not remaining_opts.empty?
    url.query = URI.encode_www_form(remaining_opts)
  end

  url
end