lib/nephos-server/server/responder.rb in nephos-server-0.5.4 vs lib/nephos-server/server/responder.rb in nephos-server-0.6.1
- old
+ new
@@ -1,18 +1,19 @@
module Nephos
- module Responder
+ class Responder
+
class InvalidContentType < StandardError; end
CT_CHARSET_PREFIX = '; charset='
- def self.content_type(kind, type, charset='UTF-8')
+ def content_type(kind, type, charset='UTF-8')
"#{kind}/#{type}" + CT_CHARSET_PREFIX + charset
end
# @param params [Hash] containing :type => "kind/type", example: "text/html"
- def self.ct_specific(params)
+ def ct_specific(params)
kind, type = params[:type].to_s.match(/^(\w+)\/(\w+)$/) && Regexp.last_match[1..2]
if kind.nil? or type.nil?
raise InvalidContentType, "params[:type] must match with \"kind/type\""
end
content_type(kind, type)
@@ -22,24 +23,23 @@
plain: "text/plain",
html: "text/html",
json: "application/json",
}
- private
# if not :status entry, set to 200
- def self.set_default_params_status params
+ def set_default_params_status params
params[:status] ||= 200
end
- def self.set_default_params_type params
+ def set_default_params_type params
if params[:type].nil?
params[:type] = PRESET_CT[(params.keys & [:plain, :html, :json]).first || :plain]
end
params[:type] = ct_specific(params)
end
- def self.set_default_params_content params
+ def set_default_params_content params
type = (params.keys & [:plain, :html, :json, :content]).first
if type.nil?
if params[:status].to_s.match(/^[45]\d\d$/)
params[:content] = "Error: #{params[:status]} code"
elsif params[:status].to_s.match(/^[3]\d\d$/)
@@ -50,46 +50,54 @@
else
params[type] = params[type].to_json if type == :json
params[:content] = params[type]
end
end
- public
# Fill params with default parameters (status, plain errors)
- def self.set_default_params params
+ def set_default_params params
set_default_params_status(params)
set_default_params_type(params)
set_default_params_content(params)
- params
+ return params
end
- def self.empty_resp
+ def empty_resp
resp = Rack::Response.new
resp.status = 204
resp["Content-Type"] = ct_specific({type: PRESET_CT[:plain]})
- resp
+ return resp
end
- def self.render_from_controller controller, method_to_call
- params = controller.send method_to_call
- resp = Responder.render(params)
+ def render_from_controller req, call
+ controller = Module.const_get(call[:controller]).new(req, call)
+ method_to_call = call[:method]
+
+ controller.execute_before_action(method_to_call)
+ # puts "Call #{controller} # #{method_to_call}"
+ params = controller.send(method_to_call)
+ controller.execute_after_action(method_to_call)
+
+ # puts "--- Params #{params.class} ---", "<<", params, ">>"
+ resp = render(params)
controller.cookies.each do |k, v|
resp.set_cookie k, v
end
return resp
end
- # @param controller [Controller]
- # @param method_to_call [Symbol]
- def self.render params
- return Responder.empty_resp if params == :empty
+ def render params
+ return empty_resp if params == :empty
return render(status: params) if params.is_a? Integer
+ raise "Not a valid params argument" unless params.is_a? Hash
+
resp = Rack::Response.new
params = set_default_params(params)
resp.status = params[:status]
resp["Content-Type"] = params[:type]
resp.body = [params[:content]]
return resp
end
end
+
end