Module: JSONModel::Client::ClassMethods

Defined in:
common/jsonmodel_client.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



410
411
412
413
414
415
416
417
418
419
420
421
422
423
# File 'common/jsonmodel_client.rb', line 410

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

#all(params = {}, opts = {}) ⇒ Object

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



470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
# File 'common/jsonmodel_client.rb', line 470

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

#find(id, opts = {}) ⇒ Object

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



446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
# File 'common/jsonmodel_client.rb', line 446

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

#find_by_uri(uri, opts = {}) ⇒ Object



464
465
466
# File 'common/jsonmodel_client.rb', line 464

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

#my_url(id = nil, opts = {}) ⇒ Object

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



428
429
430
431
432
433
434
435
436
437
438
439
440
441
# File 'common/jsonmodel_client.rb', line 428

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