module JSONModel::Client::ClassMethods

Public Class Methods

extended(base) click to toggle source
# File lib/aspace_client/jsonmodel_client.rb, line 404
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

Public Instance Methods

all(params = {}, opts = {}) click to toggle source

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

# File lib/aspace_client/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
find(id, opts = {}) click to toggle source

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

# File lib/aspace_client/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
find_by_uri(uri, opts = {}) click to toggle source
# File lib/aspace_client/jsonmodel_client.rb, line 459
def find_by_uri(uri, opts = {})
  self.find(self.id_for(uri), opts)
end
my_url(id = nil, opts = {}) click to toggle source

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

# File lib/aspace_client/jsonmodel_client.rb, line 422
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