Module: Mountapi::Response::Base::InstanceMethods

Defined in:
lib/mountapi/response/base.rb

Instance Method Summary collapse

Instance Method Details

#default_schemaObject



45
46
47
# File 'lib/mountapi/response/base.rb', line 45

def default_schema
  Mountapi::Response.default_schema
end

#responseObject

Since we return a mountapi response they must have a custom rack response to enable customizing



29
30
31
# File 'lib/mountapi/response/base.rb', line 29

def response
  @response ||= Rack::Response.new([])
end

#statusObject



24
25
26
# File 'lib/mountapi/response/base.rb', line 24

def status
  self.class.get_status
end

#to_jsonObject



41
42
43
# File 'lib/mountapi/response/base.rb', line 41

def to_json
  payload.to_json
end

#to_rackObject

Use the existing (or default) rack response



34
35
36
37
38
39
# File 'lib/mountapi/response/base.rb', line 34

def to_rack
  response.status = status
  response.write(to_json)
  response.set_header("content-type", "application/json")
  response.finish
end

#validate(definitions) ⇒ Object



49
50
51
52
# File 'lib/mountapi/response/base.rb', line 49

def validate(definitions)
  errors = Schema::Validator.call(definitions[status].to_json_schema, payload)
  errors.empty? || raise(Error::InvalidResponse, errors)
end