Note that there are some explanatory texts on larger screens.

plurals
  1. POwhen to use before filters and about to organize code (DRY)
    primarykey
    data
    text
    <p>I have coded some Ruby script to scrap some web data; then I merged that script with a small Sinatra application to "publish" some rest methods to fetch reports...</p> <p>My Sinatra app is making a request to Nagios3 and navigate through some forms (3 steps in fact), this is just working fine.<br> Then under step3, I "press" the submit button and after a few seconds I have a huge html report with servicegroups availabilities. At this point I am using Nokogiri to extract just one field, this is working fine too for any servicegroup (it can manage any servicegroup dimension).</p> <p>To have the /index with a list of servicegroups, I need to goto Step1 and Step2 only.<br> The Step3 is needed just for /check (build the report and fetch / print availability) </p> <p>Well, all is just working fine so the question I'm making is like weird because the thing is I don't know how to dry it correctly.<br> I'm writting the #Initialize Mechanize stuff, Step1 and Step2 under before do filters. They are needed in both /index and /check, but in fact I don't know if this is the right place to do it.</p> <p>Any code architectural tip :)</p> <p>thanks in advance.<br> Francisco</p> <pre><code>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 =&gt; '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 =&gt; 'report_type').options[2].select @page = @agent.submit(@form1) # Step2 - Form2 (Select Servicegroup) @form2 = @page.forms.first @total_services_list = @form2.field_with(:name =&gt; '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 =&gt; 'servicegroup').options[@servicegroup].select @page = @agent.submit(@form2) @form3 = @page.forms.first @form3.field_with(:name =&gt; '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 =&gt; false end __END__ @@layout &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"&gt; &lt;head&gt;&lt;title&gt;Nagios Availability Checker&lt;/title&gt;&lt;/head&gt; &lt;body&gt; &lt;strong&gt;Nagios Availability Checker&lt;/strong&gt; &lt;hr /&gt; &lt;div&gt; &lt;%= yield %&gt; &lt;/div&gt; &lt;hr /&gt; &lt;/body&gt; &lt;/html&gt; @@index &lt;h3&gt;List Service Groups&lt;/h3&gt; &lt;% @total_services_list.each_with_index do |name,id| %&gt; &lt;a href="/check/&lt;%= id %&gt;"&gt;&lt;%= id %&gt; - &lt;%= name %&gt;&lt;/a&gt;&lt;br /&gt; &lt;% end %&gt; @@check &lt;%= @average %&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload