Code Monkey home page Code Monkey logo

rest-api-example's Introduction

REST API example application

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.

Install

bundle install

Run the app

unicorn -p 7000

Run the tests

./run-tests.sh

REST API

The REST API to the example app is described below.

Get list of Things

Request

GET /thing/

curl -i -H 'Accept: application/json' http://localhost:7000/thing/

Response

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

[]

Create a new Thing

Request

POST /thing/

curl -i -H 'Accept: application/json' -d 'name=Foo&status=new' http://localhost:7000/thing

Response

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 a specific Thing

Request

GET /thing/id

curl -i -H 'Accept: application/json' http://localhost:7000/thing/1

Response

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 a non-existent Thing

Request

GET /thing/id

curl -i -H 'Accept: application/json' http://localhost:7000/thing/9999

Response

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"}

Create another new Thing

Request

POST /thing/

curl -i -H 'Accept: application/json' -d 'name=Bar&junk=rubbish' http://localhost:7000/thing

Response

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 list of Things again

Request

GET /thing/

curl -i -H 'Accept: application/json' http://localhost:7000/thing/

Response

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}]

Change a Thing's state

Request

PUT /thing/:id/status/changed

curl -i -H 'Accept: application/json' -X PUT http://localhost:7000/thing/1/status/changed

Response

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 changed Thing

Request

GET /thing/id

curl -i -H 'Accept: application/json' http://localhost:7000/thing/1

Response

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"}

Change a Thing

Request

PUT /thing/:id

curl -i -H 'Accept: application/json' -X PUT -d 'name=Foo&status=changed2' http://localhost:7000/thing/1

Response

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"}

Attempt to change a Thing using partial params

Request

PUT /thing/:id

curl -i -H 'Accept: application/json' -X PUT -d 'status=changed3' http://localhost:7000/thing/1

Response

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"}

Attempt to change a Thing using invalid params

Request

PUT /thing/:id

curl -i -H 'Accept: application/json' -X PUT -d 'id=99&status=changed4' http://localhost:7000/thing/1

Response

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"}

Change a Thing using the _method hack

Request

POST /thing/:id?_method=POST

curl -i -H 'Accept: application/json' -X POST -d 'name=Baz&_method=PUT' http://localhost:7000/thing/1

Response

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"}

Change a Thing using the _method hack in the url

Request

POST /thing/:id?_method=POST

curl -i -H 'Accept: application/json' -X POST -d 'name=Qux' http://localhost:7000/thing/1?_method=PUT

Response

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 a Thing

Request

DELETE /thing/id

curl -i -H 'Accept: application/json' -X DELETE http://localhost:7000/thing/1/

Response

HTTP/1.1 204 No Content
Date: Thu, 24 Feb 2011 12:36:32 GMT
Status: 204 No Content
Connection: close

Try to delete same Thing again

Request

DELETE /thing/id

curl -i -H 'Accept: application/json' -X DELETE http://localhost:7000/thing/1/

Response

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 deleted Thing

Request

GET /thing/1

curl -i -H 'Accept: application/json' http://localhost:7000/thing/1

Response

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 a Thing using the _method hack

Request

DELETE /thing/id

curl -i -H 'Accept: application/json' -X POST -d'_method=DELETE' http://localhost:7000/thing/2/

Response

HTTP/1.1 204 No Content
Date: Thu, 24 Feb 2011 12:36:33 GMT
Status: 204 No Content
Connection: close

rest-api-example's People

Contributors

chrislo avatar chrisn avatar seanohalpin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

rest-api-example's Issues

unable to start unicorn

When I try to start to unicorn server I get stuff like this

E, [2013-09-27T11:13:08.147770 #83109] ERROR -- : reaped #<Process::Status: pid 83603 exit 1> worker=0
I, [2013-09-27T11:13:08.148688 #83109]  INFO -- : worker=0 spawning...
I, [2013-09-27T11:13:08.150216 #83604]  INFO -- : worker=0 spawned pid=83604
I, [2013-09-27T11:13:08.150534 #83604]  INFO -- : Refreshing Gem list
config.ru:1:in `require': cannot load such file -- app (LoadError)
        from config.ru:1:in `block in <main>'

I think this is weird becauseconfig.ru:1 just requires app, which should be able to pull in app.rb, which is in the same directory as config.ru.

My environment:

  • Mac OS 10.8.5
  • ruby 1.9.3p448 (2013-06-27 revision 41675) [x86_64-darwin12.4.0]
  • rvm 1.21.19 (stable)

Problem with gems

I'm trying to run the example, but I'm getting this error when trying to run unicorn:

Renzos-MacBook-Pro:REST-API-example ruenzuo$ unicorn -p 7000
I, [2013-10-22T18:57:24.939197 #763]  INFO -- : listening on addr=0.0.0.0:7000 fd=3
I, [2013-10-22T18:57:24.940242 #763]  INFO -- : worker=0 spawning...
I, [2013-10-22T18:57:24.941458 #763]  INFO -- : master process ready
I, [2013-10-22T18:57:24.942852 #765]  INFO -- : worker=0 spawned pid=765
I, [2013-10-22T18:57:24.943437 #765]  INFO -- : Refreshing Gem list
/Users/ruenzuo/.rvm/rubies/ruby-1.9.2-p320/lib/ruby/site_ruby/1.9.1/rubygems/core_ext/kernel_require.rb:55:in `require': no such file to load -- app (LoadError)
    from /Users/ruenzuo/.rvm/rubies/ruby-1.9.2-p320/lib/ruby/site_ruby/1.9.1/rubygems/core_ext/kernel_require.rb:55:in `require'
    from config.ru:1:in `block in <main>'
    from /Users/ruenzuo/.rvm/rubies/ruby-1.9.2-p320/lib/ruby/gems/1.9.1/gems/rack-1.2.1/lib/rack/builder.rb:46:in `instance_eval'
    from /Users/ruenzuo/.rvm/rubies/ruby-1.9.2-p320/lib/ruby/gems/1.9.1/gems/rack-1.2.1/lib/rack/builder.rb:46:in `initialize'
    from config.ru:1:in `new'
    from config.ru:1:in `<main>'
    from /Users/ruenzuo/.rvm/rubies/ruby-1.9.2-p320/lib/ruby/gems/1.9.1/gems/unicorn-3.4.0/lib/unicorn.rb:30:in `eval'
    from /Users/ruenzuo/.rvm/rubies/ruby-1.9.2-p320/lib/ruby/gems/1.9.1/gems/unicorn-3.4.0/lib/unicorn.rb:30:in `block in builder'
    from /Users/ruenzuo/.rvm/rubies/ruby-1.9.2-p320/lib/ruby/gems/1.9.1/gems/unicorn-3.4.0/lib/unicorn/http_server.rb:696:in `call'
    from /Users/ruenzuo/.rvm/rubies/ruby-1.9.2-p320/lib/ruby/gems/1.9.1/gems/unicorn-3.4.0/lib/unicorn/http_server.rb:696:in `build_app!'
    from /Users/ruenzuo/.rvm/rubies/ruby-1.9.2-p320/lib/ruby/gems/1.9.1/gems/unicorn-3.4.0/lib/unicorn/http_server.rb:563:in `init_worker_process'
    from /Users/ruenzuo/.rvm/rubies/ruby-1.9.2-p320/lib/ruby/gems/1.9.1/gems/unicorn-3.4.0/lib/unicorn/http_server.rb:578:in `worker_loop'
    from /Users/ruenzuo/.rvm/rubies/ruby-1.9.2-p320/lib/ruby/gems/1.9.1/gems/unicorn-3.4.0/lib/unicorn/http_server.rb:495:in `block (2 levels) in spawn_missing_workers'
    from /Users/ruenzuo/.rvm/rubies/ruby-1.9.2-p320/lib/ruby/gems/1.9.1/gems/unicorn-3.4.0/lib/unicorn/http_server.rb:492:in `fork'
    from /Users/ruenzuo/.rvm/rubies/ruby-1.9.2-p320/lib/ruby/gems/1.9.1/gems/unicorn-3.4.0/lib/unicorn/http_server.rb:492:in `block in spawn_missing_workers'
    from /Users/ruenzuo/.rvm/rubies/ruby-1.9.2-p320/lib/ruby/gems/1.9.1/gems/unicorn-3.4.0/lib/unicorn/http_server.rb:488:in `each'
    from /Users/ruenzuo/.rvm/rubies/ruby-1.9.2-p320/lib/ruby/gems/1.9.1/gems/unicorn-3.4.0/lib/unicorn/http_server.rb:488:in `spawn_missing_workers'
    from /Users/ruenzuo/.rvm/rubies/ruby-1.9.2-p320/lib/ruby/gems/1.9.1/gems/unicorn-3.4.0/lib/unicorn/http_server.rb:502:in `maintain_worker_count'
    from /Users/ruenzuo/.rvm/rubies/ruby-1.9.2-p320/lib/ruby/gems/1.9.1/gems/unicorn-3.4.0/lib/unicorn/http_server.rb:161:in `start'
    from /Users/ruenzuo/.rvm/rubies/ruby-1.9.2-p320/lib/ruby/gems/1.9.1/gems/unicorn-3.4.0/lib/unicorn.rb:13:in `run'
    from /Users/ruenzuo/.rvm/rubies/ruby-1.9.2-p320/lib/ruby/gems/1.9.1/gems/unicorn-3.4.0/bin/unicorn:120:in `<top (required)>'
    from /Users/ruenzuo/.rvm/rubies/ruby-1.9.2-p320/bin/unicorn:23:in `load'
    from /Users/ruenzuo/.rvm/rubies/ruby-1.9.2-p320/bin/unicorn:23:in `<main>'
    from /Users/ruenzuo/.rvm/gems/ruby-1.9.2-p320/bin/ruby_executable_hooks:15:in `eval'
    from /Users/ruenzuo/.rvm/gems/ruby-1.9.2-p320/bin/ruby_executable_hooks:15:in `<main>'
E, [2013-10-22T18:57:25.040304 #763] ERROR -- : reaped #<Process::Status: pid 765 exit 1> worker=0

When I run bundle install I have no problems and this is the output of gem query --local:

*** LOCAL GEMS ***

addressable (2.2.4)
bundler (1.3.5)
bundler-unload (1.0.2)
data_objects (0.10.3)
datamapper (1.0.2)
dm-aggregates (1.0.2)
dm-constraints (1.0.2)
dm-core (1.0.2)
dm-do-adapter (1.0.2)
dm-is-state_machine (1.0.2)
dm-migrations (1.0.2)
dm-mysql-adapter (1.0.2)
dm-redis-adapter (0.2.3)
dm-serializer (1.0.2)
dm-sqlite-adapter (1.0.2)
dm-timestamps (1.0.2)
dm-transactions (1.0.2)
dm-types (1.0.2)
dm-validations (1.0.2)
do_mysql (0.10.3)
do_sqlite3 (0.10.3)
executable-hooks (1.2.5)
extlib (0.9.15)
fastercsv (1.5.4)
json (1.5.1)
json_pure (1.5.1)
kgio (2.3.2)
minitest (1.6.0)
mysql2 (0.3.13)
rack (1.2.1)
rack-cache (1.0)
rack-contrib (1.1.0)
rake (0.8.7)
rdoc (2.5.8)
redis (2.1.1)
rubygems-bundler (1.4.0)
rvm (1.11.3.8)
sinatra (1.1.3)
stringex (1.1.0)
systemu (1.2.0)
tilt (1.2.2)
unicorn (3.4.0)
uuidtools (2.1.2)

It seems that everything is alright. Any ideas? Thanks for the help.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.