lib/vundabar/controller.rb in vundabar-0.1.0 vs lib/vundabar/controller.rb in vundabar-0.2.0
- old
+ new
@@ -13,11 +13,15 @@
def redirect_to(address, status: 301)
response([], status, "Location" => address)
end
def render(*args)
- response(render_template(*args))
+ if args[0].instance_of? Hash
+ return process_hash(args[0])
+ else
+ response(render_template(*args))
+ end
end
def response(body, status = 200, header = {})
@response = Rack::Response.new(body, status, header)
end
@@ -62,8 +66,19 @@
end
def controller_name
klass = self.class.to_s.gsub(/Controller$/, "")
klass.to_snake_case
+ end
+
+ def process_hash(hash)
+ status = hash.fetch(:status, 200)
+ body = hash[:json].to_hsh
+ suuplied_header = hash.fetch(:headers, {})
+ headers = suuplied_header.merge!("Content-Type" => "application/json")
+ if hash.key? :json
+ [:headers, :json, :status].each {|key| hash.delete(key) }
+ response(body.merge!(hash).to_json, status, headers)
+ end
end
end
end