Module: JSONModel::Client::ClassMethods

Defined in:
common/jsonmodel_client.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



414
415
416
417
418
419
420
421
422
423
424
425
426
427
# File 'common/jsonmodel_client.rb', line 414

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.



474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
# File 'common/jsonmodel_client.rb', line 474

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

#fetch(uri) ⇒ Object



498
499
500
501
# File 'common/jsonmodel_client.rb', line 498

def fetch(uri)
  hash = JSONModel::HTTP.get_json(uri)
  self.new(hash)
end

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

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



450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
# File 'common/jsonmodel_client.rb', line 450

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



468
469
470
# File 'common/jsonmodel_client.rb', line 468

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)



432
433
434
435
436
437
438
439
440
441
442
443
444
445
# File 'common/jsonmodel_client.rb', line 432

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