lib/nephos-server/controller.rb in nephos-server-0.6.4 vs lib/nephos-server/controller.rb in nephos-server-0.6.5

- old
+ new

@@ -4,15 +4,17 @@ # It contains a constructor (you should not rewrite it) # It contains some helpers too, like an access to the environment, # and the parameters. class Controller - attr_reader :req, :callpath, :params, :cookies + attr_reader :req, :callpath, :params, :cookies, :extension + alias :format :extension # @param env [Hash] env extracted from the http request # @param parsed [Hash] pre-parsed env with parameters, ... - def initialize req, callpath + # @param extension [String] extension ".json", ".html", ... + def initialize req, callpath, extension=nil raise ArgumentError, "req must be a Rack::Request" unless req.is_a? Rack::Request raise ArgumentError, "call must be a Hash" unless callpath.is_a? Hash @req= req @callpath= callpath @params= req.params rescue {} @@ -22,9 +24,21 @@ @params.merge! Hash[callpath[:params].zip(params)] @params.select!{|k,v| not k.to_s.empty?} @params = Params.new(@params) @cookies = Params.new(@req.cookies) + + @extension = extension.to_s.split(".").last + end + + def html? + %w(htm html xhtml).include? extension + end + def json? + %w(json).include? extension + end + def plain? + %w(txt raw).include? extension end @@before_action = {:'*' => []} @@after_action = {:'*' => []}