Skip to content

Instantly share code, notes, and snippets.

@jptix
Created April 8, 2009 12:59
Show Gist options
  • Save jptix/91757 to your computer and use it in GitHub Desktop.
Save jptix/91757 to your computer and use it in GitHub Desktop.
require "rubygems"
require "rack/contrib/accept_format" # sudo gem install rack-rack-contrib (http://github.com/rack/rack-contrib/tree/master)
require "sinatra"
require "yaml"
require "json"
use Rack::AcceptFormat
get "/foo.*" do
@data = %w[foo bar baz]
pass
end
get "/foo.html" do
"<ul>" + @data.map { |n| "<li>#{n}</li>"}.join("\n") + "</ul>"
end
get "/foo.yaml" do
@data.to_yaml
end
get "/foo.json" do
@data.to_json
end
=begin
$ curl localhost:4567/foo
<ul><li>foo</li>
<li>bar</li>
<li>baz</li></ul>
$ curl localhost:4567/foo --header "Accept: application/json"
["foo","bar","baz"]
$ curl localhost:4567/foo --header "Accept: text/yaml"
---
- foo
- bar
- baz
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment