tags:

views:

43

answers:

1

Hi,

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...

My Sinatra app is making a request to Nagios3 and navigate through some forms (3 steps in fact), this is just working fine.
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).

To have the /index with a list of servicegroups, I need to goto Step1 and Step2 only.
The Step3 is needed just for /check (build the report and fetch / print availability)

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.
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.

Any code architectural tip :)

thanks in advance.
Francisco

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"&gt;
<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 %>
A: 

You can use helpers:

helpers do
  def select_report_type
    # ...
  end

  def something_else
    # ...
  end
end

before { select_report_type }
get "/" do
  select_report_type
  # ...
  haml :index
end
Konstantin Haase
Hi Konstantin; first thanks for your anwser :)Well I need to read a little bit more about helpers (and also before filters) and make some tests; but at first sight they look like a generic class which then are called under before filter via def; so I think in practical they do the same thing as my hypothesis. I don't know which one is faster or which takes less memory or even which one is "more correct" but I will try out your tip anyway :) hey, newbie here :) 10x
include
There actually is a not so generic class Sinatra::Application. With helpers you just add methods to it. Also, performance and memory wise the helpers approach should give you better results, as closures are a bit more expensive than methods. But it should not matter that much.
Konstantin Haase