Class: DMAO::API::Entity

Inherits:
Base
  • Object
show all
Defined in:
lib/dmao/api/entity.rb

Constant Summary collapse

ENDPOINT =
''
NOT_FOUND_ERROR =
nil
INVALID_ID_ERROR =
nil
INVALID_ENTITY_CLASS =
nil
INVALID_ENTITY_ERROR_MESSAGE =
""
VALID_ATTRIBUTES =
[]

Class Method Summary collapse

Methods inherited from Base

api

Class Method Details

.allObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# 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) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# 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) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# 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) ⇒ Object

Raises:

  • (self::NOT_FOUND_ERROR)


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 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) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
# 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) ⇒ Object

Raises:

  • (self::INVALID_ENTITY_CLASS)


132
133
134
135
136
137
138
# 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) ⇒ Object



156
157
158
# 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) ⇒ Object



124
125
126
127
128
129
130
# 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) ⇒ Object



164
165
166
# 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) ⇒ Object

Raises:

  • (error_class)


168
169
170
# 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) ⇒ Object



160
161
162
# 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) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# 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) ⇒ Object



152
153
154
# 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) ⇒ Object



140
141
142
143
144
# 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) ⇒ Object



146
147
148
149
150
# 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