require 'sinatra/base' require 'erb' require 'rewritten' require 'rewritten/version' require 'time' module Rewritten class Server < Sinatra::Base dir = File.dirname(File.expand_path(__FILE__)) set :views, "#{dir}/server/views" set :public, "#{dir}/server/public" set :static, true helpers do include Rack::Utils alias_method :h, :escape_html def current_section url_path request.path_info.sub('/','').split('/')[0].downcase end def current_page url_path request.path_info.sub('/','') end def url_path(*path_parts) [ path_prefix, path_parts ].join("/").squeeze('/') end alias_method :u, :url_path def path_prefix request.env['SCRIPT_NAME'] end def class_if_current(path = '') 'class="current"' if current_page[0, path.size] == path end def tab(name) dname = name.to_s.downcase path = url_path(dname) "
#{text}
" end end # enf of helpers def show(page, layout = true) begin erb page.to_sym, {:layout => layout}, :rewritten => Rewritten rescue Errno::ECONNREFUSED erb :error, {:layout => false}, :error => "Can't connect to Redis! (#{Rewritten.redis_id})" end end def show_for_polling(page) content_type "text/html" @polling = true show(page.to_sym, false).gsub(/\s{1,}/, ' ') end ################################################################################ get "/?" do redirect url_path(:translations) end get "/translations" do show 'translations' end get "/new" do show "new" end post "/translations" do if params[:from]!='' && params[:to]!='' Rewritten.add_translation(params[:from], params[:to]) redirect u('translations') else show "new" end end get "/to" do translations = Rewritten.list_range(params[:to], 0, -1) show "to" end get "/delete" do @from = params[:from] @to = params[:to] show "delete" end post '/delete' do from = params[:from] to = params[:to] Rewritten.remove_translation(from, to) if Rewritten.num_translations(to) > 0 redirect u("/to?to=#{escape(to)}") else redirect u("/") end end get "/hits" do show "hits" end get "/hits/clear" do show "clear_hits" end post "/hits/clear" do Rewritten.redis.del("hits") redirect u("/hits") end def self.tabs #@tabs ||= ["Overview", "Working", "Failed", "Queues", "Workers", "Stats"] @tabs ||= ["Translations", "Hits"] end end end