何时在过滤器之前使用以及即将组织代码(DRY)

时间:2010-04-08 15:14:36

标签: sinatra

我编写了一些Ruby脚本来废弃一些网络数据;然后我将该脚本与一个小的Sinatra应用程序合并,以“发布”一些其他方法来获取报告......

我的Sinatra应用程序正在向Nagios3发出请求并浏览一些表格(实际上是3个步骤),这只是工作正常。
然后在第3步,我“按下”提交按钮,几秒钟后,我有一个巨大的HTML报告,其中包含服务组的可用性。此时我使用Nokogiri只提取一个字段,这对任何服务组都很好(它可以管理任何服务组维度)。

要让/ index包含服务组列表,我只需要转到步骤1和步骤2 Step3仅用于/ check(构建报告和获取/打印可用性)

嗯,一切正常,所以我正在制作的问题很奇怪,因为事情是我不知道如何正确地干它。
我正在编写#Initialize Mechanize的东西,Step1和Step2之前做过滤器。 / index和/ check都需要它们,但事实上我不知道这是否适合这样做。

任何代码架构提示:)

提前谢谢。
旧金山

require 'rubygems'
require 'sinatra'
require 'mechanize'
require 'nokogiri'
require 'logger'

configure :production do
    enable :logging, :dump_errors
    enable :inline_templates
    disable :run
end

error do
    e = request.env['sinatra.error']
    puts e.to_s
    puts e.backtrace.join("\n")
    "Application Error!"
end

not_found do
    "Page not found!"
end

before do
    content_type 'text/html', :charset => 'utf-8'

    # Initialize Mechanize
    @agent = Mechanize.new
    @agent.keep_alive = false
    @agent.open_timeout = 3600
    @agent.read_timeout = 3600
    @agent.max_history = 0
    @agent.log = Logger.new('mechanize.log')
    @agent.auth('myusername', 'mysecretpass')
    @page = @agent.get('http://nagios3/nagios/cgi-bin/avail.cgi')

    # Step1 and Step2 are use in both /index and /check  

    # Step1 - Form1 (Select Report Type[hostgroups,hosts,hostgroups,servicegroups*,services])
    @form1 = @page.forms.first
    @form1.field_with(:name => 'report_type').options[2].select
    @page = @agent.submit(@form1)

    # Step2 - Form2 (Select Servicegroup)
    @form2 = @page.forms.first
    @total_services_list = @form2.field_with(:name => 'servicegroup').options  
end

get '/' do
    # When under /index we don't go further to Step3 - Form3 (generate report)
    erb :index
end

get '/check/:servicegroup' do

    @servicegroup = params[:servicegroup].to_i

    # Step3 - Form3 (Select Report Options)
    @form2.field_with(:name => 'servicegroup').options[@servicegroup].select
    @page = @agent.submit(@form2)

    @form3 = @page.forms.first
    @form3.field_with(:name => 'timeperiod').options[7].select
    @page = @agent.submit(@form3)

    # Step4 - Extract Average from computed data
    page_html = Nokogiri::HTML.parse(@page.body)
    @total_hostservices_size = page_html.xpath("html/body/div[3]/table/tr[*]/td[2]").to_a.size
    puts @average = page_html.xpath("html/body/div[3]/table/tr[#{@total_hostservices_size}]/td[2]").inner_text

    erb :check, :layout => false
end


__END__


@@layout
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head><title>Nagios Availability Checker</title></head>
    <body>
        <strong>Nagios Availability Checker</strong>
        <hr />
        <div>
            <%= yield %>
        </div>
        <hr />
    </body>
</html>

@@index
<h3>List Service Groups</h3>
<% @total_services_list.each_with_index do |name,id| %>
    <a href="/check/<%= id %>"><%= id %> - <%= name %></a><br />
<% end %>

@@check
    <%= @average %>

1 个答案:

答案 0 :(得分:0)

您可以使用帮助者:

helpers do
  def select_report_type
    # ...
  end

  def something_else
    # ...
  end
end

before { select_report_type }
get "/" do
  select_report_type
  # ...
  haml :index
end
相关问题