Created
October 14, 2011 20:40
-
-
Save xaviervia/1288267 to your computer and use it in GitHub Desktop.
Ready to go rest engine with Piano (refactor and make gem soon)
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
!!! 5 | |
%html | |
%head | |
%title Arena: RESTful Piano | |
= script "https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" | |
= script "jquery.restful.js" | |
%body | |
- if flash? | |
%output= flash | |
%form(method="post" action="/") | |
%input(name="id")/ | |
%input(name="value")/ | |
%button(type="submit") Save | |
- items.each do |file| | |
%dl | |
%dt | |
%a(href="/#{resource}:#{file}")= file | |
%dd | |
%a(method="delete" href="/#{resource}:#{file}") Delete | |
%form(method="put" action="/#{resource}:#{file}") | |
%input(name="value")/ | |
%button(type="submit") Update | |
- if @content | |
= @content |
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
$ -> | |
$("form[method=put]").submit -> | |
$.ajax | |
type: "PUT" | |
url: $(this).attr "action" | |
data: $(this).serialize() | |
statusCode: | |
201: (data, textStatus, xhr) -> | |
window.location = xhr.getResponseHeader "Location" | |
return false | |
$("a[method=delete]").click -> | |
$.ajax | |
type: "DELETE" | |
url: $(this).attr "href" | |
statusCode: | |
201: (data, textStatus, xhr) -> | |
window.location = xhr.getResponseHeader "Location" | |
return false |
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
require "json" | |
require "psych" | |
module Fast | |
class File | |
def append path, content | |
::File.open path, "a" do |handler| | |
handler.write content | |
end | |
end | |
def self.call *args | |
if args.empty? | |
File.new | |
else | |
::File.read args.shift | |
end | |
end | |
end | |
def self.dir path, options = nil | |
mkdir path unless Dir.exist? path | |
if options | |
if options[:extension] | |
filtered = [] | |
Dir.entries(path).each do |entry| | |
if entry.end_with? ".#{options[:extension]}" | |
if options[:strip_extension] | |
filtered << entry[0..-("#{options[:extension]}".length)-2] | |
else | |
filtered << entry | |
end | |
end | |
end | |
return filtered | |
end | |
else | |
Dir.entries path | |
end | |
end | |
class From | |
def yaml source | |
if ::File.exist? source | |
Psych.load_documents( file( source ) ).last | |
else | |
Psych.load source | |
end | |
end | |
end | |
def self.file *args | |
Fast::File.call *args | |
end | |
def self.from | |
Fast::From.new | |
end | |
private | |
def self.mkdir path | |
path.split("/").each do |part| | |
route ||= part | |
Dir.mkdir route unless route == "" || Dir.exist?( route ) | |
route += "/#{path}" | |
end | |
Dir.mkdir path | |
end | |
end | |
delegate Fast, :dir, :file, :from | |
disable :etags | |
enable :sessions | |
post "/" do | |
resource :index | |
save_data params | |
flash "Saved some data" | |
redirect back, params.to_json | |
end | |
put %r{^/(.*?):(.+?)$} do |resource_text, item_text| | |
resource resource_text.to_sym | |
params["id"] = item_text | |
params.delete "captures" | |
save_data params | |
flash "Updated #{item_text}" | |
status 201 | |
headers \ | |
"Location" => "/#{resource}:#{item_text}" | |
body params.to_json | |
end | |
delete %r{^/(.*?):(.+?)$} do |resource_text, item_text| | |
resource_text = "index" if resource_text.nil? | |
resource resource_text.to_sym | |
delete_data item_text | |
flash "Deleted #{item_text}" | |
status 201 | |
headers \ | |
"Location" => "/#{resource}" | |
end | |
get %r{^/(.*?):(.+?)$} do |resource_text, item| | |
resource_text = "index" if resource_text.nil? | |
resource resource_text.to_sym | |
@content = from.yaml "data/#{resource}/#{item}.yaml" | |
try_haml resource | |
end | |
get %r{^/(.+?).js$} do |resource_text| | |
content_type :js | |
coffee resource_text.to_sym | |
end | |
get %r{^/(.+?).css$} do |resource_text| | |
content_type :css | |
sass resource_text.to_sym | |
end | |
get %r{^/(.+?)$} do |resource_text| | |
resource resource_text.to_sym | |
try_haml resource | |
end | |
get "/" do | |
resource :index | |
try_haml resource | |
end | |
before %r{^/(.*?)$} do |resource_text| | |
resource resource_text.to_sym | |
end | |
helpers do | |
def save_data data | |
dir "data/#{resource}" | |
file.append "data/#{resource}/" + data[:id] + ".yaml", data.to_yaml | |
end | |
def delete_data data | |
::File.unlink "data/#{resource}/#{data}.yaml" | |
end | |
def items | |
if Dir.exists? "data/#{resource}" | |
dir "data/#{resource}", :extension => :yaml, :strip_extension => true | |
end | |
end | |
def resource symbol = nil | |
if symbol | |
session[:resource] = symbol | |
else | |
session[:resource] | |
end | |
end | |
def flash? | |
session[:flash] | |
end | |
def flash data = nil | |
if data | |
session[:flash] = data | |
else | |
flash_text = session[:flash] | |
session.delete :flash | |
return flash_text | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment