Created
June 7, 2010 13:50
-
-
Save leejarvis/428682 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Ramaze::Helper | |
module RespondTo | |
Formats = { | |
'json' => 'application/json', | |
'xml' => 'application/xml', | |
'yaml' => 'application/yaml', | |
# others .. | |
} | |
# Hack to remove format from params | |
Ramaze::Controller.before_all do | |
action.params.map! {|x| x.split('.').first } | |
end | |
def req_format | |
s = action.path.split('.') | |
s.size > 1 ? s.last : nil | |
end | |
def respond_to(format, resp=nil, &blk) | |
type = Formats[format.to_s] || 'text/html' | |
if req_format == format.to_s | |
respond(resp || blk.call, 200, 'Content-Type' => type) | |
end | |
end | |
end | |
end | |
# /view/10 | |
# or | |
# /view/10.json | |
# def view(id) | |
# id #=> 10 | |
# not_found unless post = Post[id] | |
# respond_to(:json, post.to_json) | |
# respond_to(:xml, post.to_xml) | |
# "default response.." | |
# end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment