Skip to content

Instantly share code, notes, and snippets.

@leejarvis
Created June 7, 2010 13:50
Show Gist options
  • Save leejarvis/428682 to your computer and use it in GitHub Desktop.
Save leejarvis/428682 to your computer and use it in GitHub Desktop.
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