79b4825afe5c22c7d6226037ac81ee69

Hi Everyone,

I've created a ruby script that checks several (fake/local) websites whether it's up-to-date or not by just doing a comparison on the date listed on the site.

The condition is: each site must not be older then 1 day.

If however it's a "special" site (by simply looking if the text "special" is present on the site) then it shouldn't be older then 7 days.

Now the thing is, I've created a ruby script to check all of this, and it works perfectly fine though. However I really don't like the way the code looks. Also I think it's quite a bit slow, I believe it can be much faster if the code is well optimized.

As you can see in my script below it does the following things:

1. It does two regular expressions, one for the date that is on the site, and one to see wheater it's a "special" site of not.
2. It compares the date on the site with the current date (Date.today)
3. And some If and else statements to see if a site is up-to-date.

Any idea how to optimize this? Any suggestions will be very appreciated.

Many thanks in advance....

#!/usr/bin/ruby
puts "Content-Type: text/html\n\n"

require 'net/http'
require 'uri'
require 'open-uri'
require 'date'

@@begin_time = Time.now

def check_urls(url, iterate)
    
    puts "<h3>Checking URLS</h3>"
    puts "<ul>"
    iterate.times do |i|
    uri_output = Net::HTTP.get(URI.parse(url[i])).scan(/\d{2}\.\d{2}\.\d{4}/)[0]
    site_kind = Net::HTTP.get(URI.parse(url[i])).include? "SPECIAL"

    if uri_output == nil
      puts "<li>[NOTHING FOUND] <a href='" + url[i] + "' target='_blank'>" + url[i] + "</a></li>"
    else

    site_date = Date.strptime(uri_output, "%d.%m.%Y")
    	 current_date = Date.today
    	 difference = current_date - chart_date

    if site_kind == true && difference > 7
  	  puts "<li>[NOT UPDATED] #{site_date} <a href='" + url[i] + "' target='_blank'>" + url[i] + "</a></li>"
    else
    if site_kind == false && difference > 1
  	  puts "<li>[NOT UPDATED] #{site_date} <a href='" + url[i] + "' target='_blank'>" + url[i] + "</a></li>"
    else
  	  puts "<li>[OK] #{site_date} <a href='" + url[i] + "' target='_blank'>" + url[i] + "</a></li>"
    end
  end
  end
  end
    puts "</ul>"
    
    puts "</ul> <strong>ALL DONE</strong>"
    executed_time = Time.now - @@begin_time
    to_minutes(executed_time)
end

def to_minutes(seconds)
  min = (seconds/60).floor
  sec = (seconds - (min * 60)).round
 
  if min < 10
   min = "0#{min}"
  end
  
  if sec < 10
   sec = "0#{sec}"
   end
   
  puts "in <strong>#{min}:#{sec}</strong>"
end

websites = IO.read('websites.txt').split
number_of_lines = websites.length
check_urls(websites, number_of_lines)

Refactorings

No refactoring yet !

A8d3f35baafdaea851914b17dae9e1fc

Adam

March 4, 2009, March 04, 2009 19:45, permalink

No rating. Login to rate!
require 'rubygems'
require 'uri'
require 'net/http'
require 'markaby'
require 'activesupport'

require File.dirname(__FILE__) + '/lib/website.rb'
require File.dirname(__FILE__) + '/lib/website_view.rb'
require File.dirname(__FILE__) + '/lib/website_controller.rb'

if __FILE__ == $0
  controller = WebsiteController.new(File.readlines('websites.txt'))
  puts controller.render_results
end
class Website
  attr_reader :source, :uri
  
  def initialize(uri)
    @uri = uri.is_a?(URI) ? uri : URI.parse(uri)
    @source = Net::HTTP.get(@uri)
  end
  
  def success?
    source
  end
  
  def last_update
    Date.parse(source[/\d{2}\.\d{2}\.\d{4}/])
  rescue
    Date.today
  end
  
  def distance_of_last_update
    Date.today - last_update
  end
  
  def special?
    source.include?('SPECIAL')
  end
  
  def url
    uri.to_s
  end
end
module WebsiteView
  def render
    html do
      body do
        h3 'Checking URLS'
        ul do
          @websites.each do |website|
            li do
              if website.success?
                if (website.special? && website.distance_of_last_update > 7) ||
                  (!website.special? && website.distance_of_last_update > 1)
                  
                  span '[NOT UPDATED]', :class => 'status'
                else
                  span '[OK]', :class => 'status'
                end
                
                span website.last_update, :class => 'last_update'
              else
                span '[FAILED]', :class => 'last_update'
              end
              a website.url, :href => website.url, :target => '_blank'
            end
          end
        end
      end
    end
  end
end
class WebsiteController
  attr_reader :urls, :render_results
  
  def initialize(urls)
    @websites = urls.map { |url| Website.new(url) }
    @render_results = render
  end
  
  def render(view = WebsiteView)
    builder = Markaby::Builder.new
    builder.extend(view)
    
    attach_instance_variables!(builder)
    builder.render
  end
  
  protected
    def attach_instance_variables!(object)
      instance_variables.each do |variable_name|
        object.instance_variable_set(variable_name, instance_variable_get(variable_name))
      end
    end
end
A20c63b83708e1438a6ec65d0fe6c859

Jonathan Parker

March 6, 2009, March 06, 2009 04:20, permalink

No rating. Login to rate!

You will need to make the code multi-threaded in order to get any significant performance gain.
See here fore details: http://www.tutorialspoint.com/ruby/ruby_multithreading.htm

C90ec5b3e8c20d80422443b78edbe51f

Krist0ff

March 7, 2009, March 07, 2009 00:25, permalink

No rating. Login to rate!
#!/usr/bin/ruby
puts "Content-Type: text/html\n\n"

require 'net/http'
require 'uri'
require 'open-uri'
require 'date'

def check_urls(websites)
  begin_time = Time.now    
  
  puts "<h3>Checking URLS</h3>
<ul>"

  websites.each do |url|
    puts "<li>#{result_for(url)}<a href='#{url}' target='_blank'>#{url}</a></li>"
  end

  puts "</ul> 
<strong>ALL DONE</strong> in <strong>#{to_minutes(Time.now - begin_time)}</strong>"

end

def result_for(url)
  output = Net::HTTP.get(URI.parse(url))
  
  uri_output = output.scan(/\d{2}\.\d{2}\.\d{4}/)[0]
  site_kind = output.include? "SPECIAL"

  if uri_output.nil?
    "[NOTHING FOUND]"
  else
    site_date = Date.strptime(uri_output, "%d.%m.%Y")
    current_date = Date.today
    difference = current_date - site_date

    if (site_kind && difference > 7) || (!site_kind && difference > 1)
  	  status = "[NOT UPDATED]"
    else
      status = "[OK]"
    end
    "#{status} #{site_date}"
  end
end

def to_minutes(seconds)
  min = (seconds/60).floor
  sec = (seconds - (min * 60)).round

  "#{"%02d" % min}:#{"%02d" % sec}"
end

websites = IO.read('websites.txt').split
check_urls(websites)

Your refactoring





Format Copy from initial code

or Cancel