Skip to content

Instantly share code, notes, and snippets.

@davidvandusen
Created March 26, 2015 18:02
Show Gist options
  • Save davidvandusen/4459b4494df5645fb416 to your computer and use it in GitHub Desktop.
Save davidvandusen/4459b4494df5645fb416 to your computer and use it in GitHub Desktop.
require 'sinatra'
require 'sinatra/contrib'
DB = [{
id: '1',
username: 'admin',
password: '12345',
realname: 'Admin'
}, {
id: '2',
username: 'david',
password: '12345',
realname: 'David'
}]
enable :sessions
get '/' do
cookies[:lang] = 'en'
html = "<h1>Freemason Society</h1>\n<p>All the secrets, at your fingertips</p>\n"
if session[:user_id]
user = DB.find { |u| u[:id] == session[:user_id] }
html += "<h2>Hi, #{user[:realname]}</h2>\n"
html += "<p>All the Freemasons' secrets</p>\n"
end
html
end
get '/user_sessions/new' do
"<form action='/user_sessions' method='post'><input name='username'><input name='password'><button>Sign in</button></form>\n"
end
post '/user_sessions' do
user = DB.find { |u| u[:username] == params[:username] }
if user and user[:password] == params[:password]
session[:user_id] = user[:id]
redirect '/'
else
redirect '/user_sessions/new'
end
end
delete '/user_sessions' do
session.clear
redirect '/'
end
DELETE /user_sessions HTTP/1.1
Cookie: rack.session=BAh7CUkiD3Nlc3Npb25faWQGOgZFVEkiRTU3NTM0M2FiYjkzOTZmNDczZGRi%0AZGVlMDZiZDViZjAyZDMyOThkY2ZiMDc2NDJkOGVjYjgzNmY4OWM1ZTA5NWYG%0AOwBGSSIJY3NyZgY7AEZJIiVjZDg0ZTFjY2NlZGFiMjRkYmY1ZWZjMzA0OGUz%0AYjYwMQY7AEZJIg10cmFja2luZwY7AEZ7B0kiFEhUVFBfVVNFUl9BR0VOVAY7%0AAFRJIi1kYTM5YTNlZTVlNmI0YjBkMzI1NWJmZWY5NTYwMTg5MGFmZDgwNzA5%0ABjsARkkiGUhUVFBfQUNDRVBUX0xBTkdVQUdFBjsAVEkiLWRhMzlhM2VlNWU2%0AYjRiMGQzMjU1YmZlZjk1NjAxODkwYWZkODA3MDkGOwBGSSIMdXNlcl9pZAY7%0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A%3D%3D%0A
Content-Type: application/x-www-form-urlencoded
Content-Length: 29
username=admin&password=12345
# REST Resources
## Articles Resrouce
GET http://example.com/articles
- Gets all the articles (probably HTML)
POST http://example.com/articles
- Creates a new article (the article that was created, e.g. articles/100)
GET http://example.com/articles/99
- Gets article 99 (probably HTML)
PUT http://example.com/articles/99
- Replaces article 99 (the new version, probably HTML)
DELETE http://example.com/articles/99
- Deletes article 99 (send to index)
GET http://example.com/articles/new
- The "new article" form
GET http://example.com/articles/99/edit
- The "edit article" form
## Comments Resource
### Option 1 (makes more sense)
GET http://example.com/comments?article_id=1&limit=10&offset=10&order=published_at
- Gets all the comments (probably HTML)
POST http://example.com/comments
- Creates a new comment (the comment that was created, e.g. comments/100)
GET http://example.com/comments/99
- Gets comment 99 (probably HTML)
PUT http://example.com/comments/99
- Replaces comment 99 (the new version, probably HTML)
DELETE http://example.com/comments/99
- Deletes comment 99 (send to index)
GET http://example.com/comments/new
- The "new comment" form
GET http://example.com/comments/99/edit
- The "edit comment" form
### Option 2 ("some" people)
GET http://example.com/articles/1/comments?limit=10&offset=10&order=published_at
- The comments for article 1
## Singular Resources
GET http://example.com/app-config
- Get's the app config
PUT http://example.com/app-config
- Updates the app config
GET http://example.com/app-config/edit
- App config edit form
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment