class DMAO::API::Entity

Constants

ENDPOINT
INVALID_ENTITY_CLASS
INVALID_ENTITY_ERROR_MESSAGE
INVALID_ID_ERROR
NOT_FOUND_ERROR
VALID_ATTRIBUTES

Public Class Methods

all() click to toggle source
# File lib/dmao/api/entity.rb, line 36
def self.all

  begin
    response = self.api[self::ENDPOINT].get
  rescue RestClient::NotFound
    raise DMAO::API::Errors::InstitutionNotFound.new
  end

  entities = []

  response_data = JSON.parse(response)["data"]

  return entities if response_data.length == 0

  response_data.each do |data|

    entities.push instance_from_api_data(data)

  end

  entities

end
create(attributes) click to toggle source
# File lib/dmao/api/entity.rb, line 74
def self.create attributes

  validate_attributes attributes

  begin
    response = self.api[self::ENDPOINT].post attributes.to_json
  rescue RestClient::NotFound
    raise DMAO::API::Errors::InstitutionNotFound.new
  rescue RestClient::UnprocessableEntity => e
    handle_unprocessable_entity e
  end

  instance_from_response response

end
delete(id) click to toggle source
# File lib/dmao/api/entity.rb, line 108
def self.delete id

  validate_id id

  begin
    self.api["#{self::ENDPOINT}/#{id}"].delete
  rescue RestClient::NotFound
    raise self::NOT_FOUND_ERROR.new
  rescue RestClient::UnprocessableEntity => e
    handle_unprocessable_entity e
  end

  true

end
find_by_system_uuid(system_uuid) click to toggle source
# File lib/dmao/api/entity.rb, line 19
def self.find_by_system_uuid system_uuid

  validate_system_uuid system_uuid

  response = self.api["#{self::ENDPOINT}?system_uuid=#{system_uuid}"].get

  response_data = JSON.parse(response)["data"]

  raise self::NOT_FOUND_ERROR.new if response_data.length == 0
  raise DMAO::API::Errors::InvalidResponseLength.new("Expected 1 element in response there were #{response_data.length}") if response_data.length != 1

  data = response_data[0]

  instance_from_api_data data

end
get(id) click to toggle source
# File lib/dmao/api/entity.rb, line 60
def self.get id

  validate_id id

  begin
    response = self.api["#{self::ENDPOINT}/#{id}"].get
  rescue RestClient::NotFound
    raise self::NOT_FOUND_ERROR.new
  end

  instance_from_response response

end
handle_unprocessable_entity(error_response) click to toggle source
# File lib/dmao/api/entity.rb, line 132
def self.handle_unprocessable_entity error_response

  errors = JSON.parse(error_response.response.body)["errors"]

  raise self::INVALID_ENTITY_CLASS.new(self::INVALID_ENTITY_ERROR_MESSAGE, errors)

end
instance_from_api_data(_data) click to toggle source
# File lib/dmao/api/entity.rb, line 156
def self.instance_from_api_data _data
  raise "Should be overridden in extending class"
end
instance_from_response(response_body) click to toggle source
# File lib/dmao/api/entity.rb, line 124
def self.instance_from_response response_body

  data = JSON.parse(response_body)["data"]

  instance_from_api_data data

end
parse_error_response(error_response) click to toggle source
# File lib/dmao/api/entity.rb, line 164
def self.parse_error_response error_response
  JSON.parse(error_response.response.body)["errors"]
end
raise_error_if_key(error_class, errors, key) click to toggle source
# File lib/dmao/api/entity.rb, line 168
def self.raise_error_if_key error_class, errors, key
  raise error_class.new if errors.keys.include? key
end
set_id_attribute_if_not_nil(attribute_value) click to toggle source
# File lib/dmao/api/entity.rb, line 160
def self.set_id_attribute_if_not_nil attribute_value
attribute_value.nil? ? nil : attribute_value["id"]
end
update(id, attributes) click to toggle source
# File lib/dmao/api/entity.rb, line 90
def self.update id, attributes

  validate_id id

  validate_attributes attributes

  begin
    response = self.api["#{self::ENDPOINT}/#{id}"].patch attributes.to_json
  rescue RestClient::NotFound
    raise self::NOT_FOUND_ERROR.new
  rescue RestClient::UnprocessableEntity => e
    handle_unprocessable_entity e
  end

  instance_from_response response

end
validate_attributes(attributes) click to toggle source
# File lib/dmao/api/entity.rb, line 152
def self.validate_attributes attributes
  attributes.keep_if { |k, _v| self::VALID_ATTRIBUTES.include? k }
end
validate_id(id) click to toggle source
# File lib/dmao/api/entity.rb, line 140
def self.validate_id id
  if id.nil? || id.to_s.empty?
    raise self::INVALID_ID_ERROR.new
  end
end
validate_system_uuid(system_uuid) click to toggle source
# File lib/dmao/api/entity.rb, line 146
def self.validate_system_uuid system_uuid
  if system_uuid.nil? || system_uuid.to_s.empty?
    raise DMAO::API::Errors::InvalidSystemUUID.new
  end
end