helper:
require 'sinatra'
require 'json'
require 'haml'
helpers do
def get_xml(*args)
# your code here…
end
end
get '/processed' do
# getxml is available here
status 200
meeting_dir="/home/default"
Dir.entries(meeting_dir)
end
Теперь, я предполагаю, что вы хотите, чтобы вывод в XML, так что маршрут не будет работать, вы получите оценку последнего выражения, которое в настоящее время Dir.entries(meeting_dir)
.
Настройка view, и с помощью Haml's list_of
helper мы получим, что готов вывести список значений:
#views/contents.haml
<folders>
= list_of(@metas) do |meta|
= "<#{meta.name}>"
= meta.content
= "</#{meta.name}>"
<\folders>
Теперь вам нужен @metas
объект:
helpers do
def get_xml(meeting_dir)
dirs = Dir.entries(meeting_dir).reject{|e| e.start_with? "." }
meta = Struct.new(:name, :content)
dirs.each_with_object([]) do |d,metas|
meta.name = d # it's up to you whether you
# want the absolute or relative path here
meta.content = File.read File.expand_path(File.join d, "meta.xml")
metas << meta
end
# the return value of each_with_object
# is the object, so no need for a return expression
end
end
get '/processed', :provides => :xml do
# status 200 <- this is not needed
meeting_dir="/home/default"
@metas = get_xml(meeting_dir)
haml :contents, :layout => false
end
Также смотрите на http://www.sinatrarb.com/intro.html#Conditions и http://www.sinatrarb.com/intro.html#Named%20Templates. Это должно дать вам кое-что, что можно было бы продолжить.