Skip to content

Instantly share code, notes, and snippets.

@shenzhaoyan
Forked from mtodd/sample.rb
Created January 7, 2011 02:58
Show Gist options
  • Save shenzhaoyan/769042 to your computer and use it in GitHub Desktop.
Save shenzhaoyan/769042 to your computer and use it in GitHub Desktop.
Sinatra helpers
helpers do
def format
(params[:format] || :json).to_sym
end
def render_to(format, results)
case format
when :json
results.to_json
when :xml
results.to_xml
end
end
end
before do
not_found unless params.key?(:query)
end
not_found do
render_to(format, {:error => "Not Found", :status => 404})
end
get '/api/v1/noun.:format' do
render_to(format, params[:query])
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment