Created
November 8, 2011 19:25
-
-
Save xaviervia/1348828 to your computer and use it in GitHub Desktop.
Steps towards RESTful Piano, semantics provided with RDF... next logic step!
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
# Instructions | |
# ============ | |
# | |
# Put some files with no extension in /data and see the server | |
# read them (in stdout). | |
# | |
# > gem 'piano' | |
# > gem 'addressable' | |
# > gem 'mime-types' | |
require "piano" | |
require "addressable/uri" | |
require "addressable/template" | |
require "mime/types" | |
module Piano | |
class Resource | |
TEMPLATES = [ | |
Addressable::Template.new( "http://{host}/{identifier}{-prefix|.|type}" ), | |
Addressable::Template.new( "http://{host}/{identifier}" ), | |
Addressable::Template.new( "{identifier}{-prefix|.|type}" ), | |
Addressable::Template.new( "{identifier}" ) | |
] | |
attr_accessor :identifier, :type, :content, :uri | |
def initialize identifier | |
parse identifier | |
# Esto es una lectura elemental del contenido. Deberia hacerse en | |
# un criterio relacionado con, sea el formato pedido en la extension | |
# o sea el formato pedido en el Accept | |
if File.exist? "#{Base.data}/#{@identifier}" | |
@content = File.read "#{Base.data}/#{@identifier}" | |
end | |
end | |
def parse identifier | |
@uri = Addressable::URI.parse identifier | |
extracted = nil | |
Resource::TEMPLATES.each do |template| | |
extracted = template.extract @uri | |
break unless extracted.nil? | |
end | |
raise "The URI templates for Resource are broken, please check them" if extracted.nil? | |
@identifier = extracted["identifier"] | |
# MIME type is the best | |
if extracted.has_key? "type" | |
# Load from extension... | |
mimes = MIME::Types.type_for extracted["type"] | |
# ...or from MIME in URI | |
mimes = MIME::Types[extracted["type"]] if mimes.empty? | |
@type = mimes.first | |
# Total fallback: plain URI type, MIME or whatever | |
@type = extracted["type"] if @type.nil? | |
end | |
end | |
def to_s | |
"#{@identifier} [#{@type}]: #{@content}" | |
end | |
end | |
class Base | |
enable :sessions | |
helpers do | |
# Returns nil if no flash message was setted | |
# otherwise returns the message | |
def flash? | |
session[:flash] | |
end | |
# Sets the flash message or returns it in a non-destructive manner | |
def flash data = nil | |
if data | |
session[:flash] = data | |
else | |
session[:flash] | |
end | |
end | |
# Destructively returns the flash message | |
def flash! | |
flash_text = session[:flash] | |
session.delete :flash | |
return flash_text | |
end | |
def resource name = nil | |
if name | |
@resource = Resource.new name | |
else | |
@resource | |
end | |
end | |
end | |
before "*" do | |
resource request.path[1..-1] | |
puts resource | |
end | |
end | |
end | |
puts Piano::Base.data | |
Piano::Base.play! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment