Created
June 5, 2009 01:49
-
-
Save cqr/123990 to your computer and use it in GitHub Desktop.
Please provide some feedback.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# so I like sinatra. a lot. but I feel like it needs to do a little bit more. | |
# so I am working on something based on coercive methods to your models which | |
# makes more sense to me. I am trying to implement it as Rack middleware, but | |
# not completely sure if I am going to be able to do it without an actual, no | |
# kidding fork of the awesome sinatra project. hoping to avoid that. | |
# | |
# this code does not work, but it's a little mock-up of a simple blog that I | |
# _wish_ would work. when a request comes in with the accept headers set, it | |
# makes sure that the format is set properly. it will also accept .format style | |
# requests, in my mind. and, heavy heavy caching stuff built in. so really for | |
# RESTful APIs, and only thinking about your HTML formatted stuff as one REST- | |
# ful format which is available. | |
# | |
# so then, if a request comes in for /posts.rss, it gets back the object which | |
# is returned by `Post.all(:order_by=>[:created_at.desc])`, which it then calls | |
# to_rss on. very, very skinny controller. potentially fat model. not certainly | |
# MVC, but sometimes MVC isn't the best way in my mind, and this is very OO. | |
# | |
# I don't know, trying to properly wrap my mind around REST and how to really, | |
# actually implement it properly, and I think this makes the most sense to me. | |
# | |
# let me know what you think - chris@chrisrhoden.com | |
# | |
# also, let's be clear. this is structured in the way that I want to have it, | |
# but it's some sort of strange pseudo-pseudo-code, and I know that. I think | |
# that it is safe to say that there is no ORM that acts this way. | |
require 'frankie' | |
helpers do | |
def require_login | |
401, "Login Required" unless is_logged_in? | |
end | |
def is_logged_in? | |
# yeah, so I don't feel the need to write out something | |
# real here. | |
true | |
end | |
end | |
get '/' do | |
redirect '/posts' | |
end | |
get '/posts' do | |
Post.all(:order_by=>[:created_at.desc]) | |
end | |
# so, maybe you might need to set up special cases | |
# where you only know a few formats. but you shouldn't | |
# need to explicitly set this. oh, and returning strings | |
# is totally legitimate, provided that you have the :format | |
# parameter set. | |
get '/posts/:id/edit', :format => :html do | |
require_login | |
Post.find_by_id(params[:id]).edit_html | |
end | |
# this is more for demonstration purposes than useful. | |
# you would probably just use :format => :html | |
get '/posts/new', :formats => [:html, :xml, :json] do | |
require_login | |
post = Post.find_by_id(params[:id]) | |
format :html { post.new_html } | |
format :xml { post.new_xml } | |
format :json { post.new_json } | |
end | |
get '/posts/:id' do | |
Post.find_by_id(params[:id]) | |
end | |
put '/posts/:id' do | |
require_login | |
p = Post.find_or_create_by_id(params[:id]) | |
p.udpate(params[:post]).save if p | |
end | |
post '/posts' do | |
require_login | |
p = Post.new(params[:post]).save | |
redirect p.uri | |
end | |
# I think I've got it the way I want. | |
delete '/posts/:id' do | |
require_login | |
p = Post.find_by_id(params[:id]) | |
p.delete if p | |
format :html { redirect '/posts' } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment