ruby - Helper function in Sinatra -


i'm using sinatra monitor server. need read list of folders in master folder, , content of meta.xml inside each folder. desired output:

<folders>     <folder 1>         <meta.xml content>     <folder 2>         <meta.xml content> <\folders> 

this current code:

require 'sinatra' require 'json' require 'haml'  '/processed'     status 200     meeting_dir="/home/default"     dir.entries(meeting_dir) end 

i'm confused sinatra printing values of variable inside block. if want write subfunction getxml generate xml, should put it?

the helper:

require 'sinatra' require 'json' require 'haml'  helpers   def get_xml( *args )     # code here…   end end  '/processed'   # getxml available here   status 200   meeting_dir="/home/default"   dir.entries(meeting_dir) end 

now, i'm assuming want output xml, route won't work, you'll evaluation of last expression, dir.entries(meeting_dir).

set view, , using haml's list_of helper we'll ready output list of values:

#views/contents.haml  <folders>   = list_of(@metas) |meta|    = "<#{meta.name}>"      = meta.content    = "</#{meta.name}>" <\folders> 

now need @metas object:

helpers    def get_xml( meeting_dir )     dirs = dir.entries(meeting_dir).reject{|e| e.start_with? "." }     meta = struct.new(:name, :content)     dirs.each_with_object([]) |d,metas|       meta.name = d # it's whether                      # want absolute or relative path here       meta.content = file.read file.expand_path(file.join d, "meta.xml")       metas << meta     end     # return value of each_with_object     # object, no need return expression   end end  '/processed', :provides => :xml   # status 200 <- not needed   meeting_dir="/home/default"   @metas = get_xml(meeting_dir)   haml :contents, :layout => false end 

also @ http://www.sinatrarb.com/intro.html#conditions , http://www.sinatrarb.com/intro.html#named%20templates. should give going on with.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -