This is a bare-bones example of a Sinatra application providing a REST API to a DataMapper-backed model.
The entire application is contained within the app.rb
file.
config.ru
is a minimal Rack configuration for unicorn.
run-tests.sh
runs a simplistic test and generates the API
documentation below.
It uses run-curl-tests.rb
which runs each command defined in
commands.yml
.
bundle install
unicorn -p 7000
./run-tests.sh
The REST API to the example app is described below.
GET /thing/
curl -i -H 'Accept: application/json' http://localhost:7000/thing/
HTTP/1.1 200 OK
Date: Thu, 24 Feb 2011 12:36:30 GMT
Status: 200 OK
Connection: close
Content-Type: application/json
Content-Length: 2
[]
POST /thing/
curl -i -H 'Accept: application/json' -d 'name=Foo&status=new' http://localhost:7000/thing
HTTP/1.1 201 Created
Date: Thu, 24 Feb 2011 12:36:30 GMT
Status: 201 Created
Connection: close
Content-Type: application/json
Location: /thing/1
Content-Length: 36
{"id":1,"name":"Foo","status":"new"}
GET /thing/id
curl -i -H 'Accept: application/json' http://localhost:7000/thing/1
HTTP/1.1 200 OK
Date: Thu, 24 Feb 2011 12:36:30 GMT
Status: 200 OK
Connection: close
Content-Type: application/json
Content-Length: 36
{"id":1,"name":"Foo","status":"new"}
GET /thing/id
curl -i -H 'Accept: application/json' http://localhost:7000/thing/9999
HTTP/1.1 404 Not Found
Date: Thu, 24 Feb 2011 12:36:30 GMT
Status: 404 Not Found
Connection: close
Content-Type: application/json
Content-Length: 35
{"status":404,"reason":"Not found"}
POST /thing/
curl -i -H 'Accept: application/json' -d 'name=Bar&junk=rubbish' http://localhost:7000/thing
HTTP/1.1 201 Created
Date: Thu, 24 Feb 2011 12:36:31 GMT
Status: 201 Created
Connection: close
Content-Type: application/json
Location: /thing/2
Content-Length: 35
{"id":2,"name":"Bar","status":null}
GET /thing/
curl -i -H 'Accept: application/json' http://localhost:7000/thing/
HTTP/1.1 200 OK
Date: Thu, 24 Feb 2011 12:36:31 GMT
Status: 200 OK
Connection: close
Content-Type: application/json
Content-Length: 74
[{"id":1,"name":"Foo","status":"new"},{"id":2,"name":"Bar","status":null}]
PUT /thing/:id/status/changed
curl -i -H 'Accept: application/json' -X PUT http://localhost:7000/thing/1/status/changed
HTTP/1.1 200 OK
Date: Thu, 24 Feb 2011 12:36:31 GMT
Status: 200 OK
Connection: close
Content-Type: application/json
Content-Length: 40
{"id":1,"name":"Foo","status":"changed"}
GET /thing/id
curl -i -H 'Accept: application/json' http://localhost:7000/thing/1
HTTP/1.1 200 OK
Date: Thu, 24 Feb 2011 12:36:31 GMT
Status: 200 OK
Connection: close
Content-Type: application/json
Content-Length: 40
{"id":1,"name":"Foo","status":"changed"}
PUT /thing/:id
curl -i -H 'Accept: application/json' -X PUT -d 'name=Foo&status=changed2' http://localhost:7000/thing/1
HTTP/1.1 200 OK
Date: Thu, 24 Feb 2011 12:36:31 GMT
Status: 200 OK
Connection: close
Content-Type: application/json
Content-Length: 41
{"id":1,"name":"Foo","status":"changed2"}
PUT /thing/:id
curl -i -H 'Accept: application/json' -X PUT -d 'status=changed3' http://localhost:7000/thing/1
HTTP/1.1 200 OK
Date: Thu, 24 Feb 2011 12:36:32 GMT
Status: 200 OK
Connection: close
Content-Type: application/json
Content-Length: 41
{"id":1,"name":"Foo","status":"changed3"}
PUT /thing/:id
curl -i -H 'Accept: application/json' -X PUT -d 'id=99&status=changed4' http://localhost:7000/thing/1
HTTP/1.1 200 OK
Date: Thu, 24 Feb 2011 12:36:32 GMT
Status: 200 OK
Connection: close
Content-Type: application/json
Content-Length: 41
{"id":1,"name":"Foo","status":"changed4"}
POST /thing/:id?_method=POST
curl -i -H 'Accept: application/json' -X POST -d 'name=Baz&_method=PUT' http://localhost:7000/thing/1
HTTP/1.1 200 OK
Date: Thu, 24 Feb 2011 12:36:32 GMT
Status: 200 OK
Connection: close
Content-Type: application/json
Content-Length: 41
{"id":1,"name":"Baz","status":"changed4"}
POST /thing/:id?_method=POST
curl -i -H 'Accept: application/json' -X POST -d 'name=Qux' http://localhost:7000/thing/1?_method=PUT
HTTP/1.1 404 Not Found
Date: Thu, 24 Feb 2011 12:36:32 GMT
Status: 404 Not Found
Connection: close
Content-Type: text/html;charset=utf-8
Content-Length: 35
{"status":404,"reason":"Not found"}
DELETE /thing/id
curl -i -H 'Accept: application/json' -X DELETE http://localhost:7000/thing/1/
HTTP/1.1 204 No Content
Date: Thu, 24 Feb 2011 12:36:32 GMT
Status: 204 No Content
Connection: close
DELETE /thing/id
curl -i -H 'Accept: application/json' -X DELETE http://localhost:7000/thing/1/
HTTP/1.1 404 Not Found
Date: Thu, 24 Feb 2011 12:36:32 GMT
Status: 404 Not Found
Connection: close
Content-Type: application/json
Content-Length: 35
{"status":404,"reason":"Not found"}
GET /thing/1
curl -i -H 'Accept: application/json' http://localhost:7000/thing/1
HTTP/1.1 404 Not Found
Date: Thu, 24 Feb 2011 12:36:33 GMT
Status: 404 Not Found
Connection: close
Content-Type: application/json
Content-Length: 35
{"status":404,"reason":"Not found"}
DELETE /thing/id
curl -i -H 'Accept: application/json' -X POST -d'_method=DELETE' http://localhost:7000/thing/2/
HTTP/1.1 204 No Content
Date: Thu, 24 Feb 2011 12:36:33 GMT
Status: 204 No Content
Connection: close