class Startback::Web::Api
Protected Instance Methods
_serve(body)
click to toggle source
# File lib/startback/web/api.rb, line 87 def _serve(body) case body when Path send_file(body) else body end end
context()
click to toggle source
Facade over context
# File lib/startback/web/api.rb, line 16 def context env[Startback::Context::Middleware::RACK_ENV_KEY] end
file_body(file, ctype)
click to toggle source
# File lib/startback/web/api.rb, line 58 def file_body(file, ctype) raise UnsupportedMediaTypeError, "Unable to use `#{ctype}` as input data" end
json_body(body = request.body.read)
click to toggle source
# File lib/startback/web/api.rb, line 54 def json_body(body = request.body.read) JSON.load(body) end
loaded_body()
click to toggle source
About the body / input
# File lib/startback/web/api.rb, line 42 def loaded_body @loaded_body ||= case ctype = request.content_type when /json/ json_body when /multipart\/form-data/ file = params[:file] file_body file, Path(file[:filename]).extname else unsupported_media_type_error!(ctype) end end
operation_world(op)
click to toggle source
# File lib/startback/web/api.rb, line 34 def operation_world(op) { context: context } end
serve(entity_description, entity, ct = nil)
click to toggle source
# File lib/startback/web/api.rb, line 70 def serve(entity_description, entity, ct = nil) if entity.nil? status 404 content_type :json { description: "#{entity_description} not found" }.to_json elsif entity.respond_to?(:to_dto) ct, body = entity.to_dto(context).to(env['HTTP_ACCEPT'], ct) content_type ct _serve(body) elsif entity.is_a?(Path) _serve(entity) else content_type ct || "application/json" entity.to_json end end
serve_nothing()
click to toggle source
Various reusable responses
# File lib/startback/web/api.rb, line 66 def serve_nothing [ 204, {}, [] ] end
with_context(ctx = nil) { |: yield(new_context)| ... }
click to toggle source
# File lib/startback/web/api.rb, line 20 def with_context(ctx = nil) old_context = self.context new_context = ctx || self.context.dup env[Startback::Context::Middleware::RACK_ENV_KEY] = new_context result = ctx ? yield : yield(new_context) env[Startback::Context::Middleware::RACK_ENV_KEY] = old_context result end