Friday, April 19, 2013

Extract Domain name from URL and validate it

require 'rubygems'
require 'public_suffix'
require 'uri'

urls = [
        'http://google.com/sdsd/sdasd.html',
        'https://www.google.com/dsf/dsfds',
        'http://sda.google.com/dfsdfs',
        'http://sdfsf.google.co.in/dfsd/dfds',
        'http://google.dhjdf/sdas/sdada',
        'http://sd.dfsd.google.co.uk/dsfs/dsfs'
        ]
 
urls.each do |url|

        domainname = URI.parse(url).host
        if(PublicSuffix.valid?(domainname))
        domain = PublicSuffix.parse(domainname)
        puts domain.domain
        else
        puts "invalid domain : #{domainname}"
        end

end

Output :
google.com
google.com
google.com
google.co.in
invalid domain : google.dhjdf
google.co.uk

Monday, April 18, 2011

Password Generator

x=1
print "How much password do you want? "
n=gets
print "How much long password do you want? "
l=gets

if l.to_i<4
puts "You can do it your self, you looser!"
exit()
end
if l.to_i>6
y=l.to_i-6
x=x+y
end

n.to_i.times do
pa1=('a'..'z').to_a.shuffle[0..x].join
pa2=('A'..'Z').to_a.shuffle[0..0].join
pa3=['@','#','$','&','^'].shuffle[0..0].join
pa4=(0..9).to_a.shuffle[0..1].join

if l.to_i>=6
a=(pa1+pa2+pa3+pa4).split('')
elsif l.to_i==5
a=(pa1+pa3+pa4).split('')
elsif l.to_i==4
a=(pa1+pa2+pa3).split('')
end

puts a.shuffle.join
end

Tuesday, March 8, 2011

reader and writer / getters and setters methods in ruby on rails

class Car
# A writer method. Sets the value of the @make attribute
def make=(text)
@make=text
end

# A reader method. Returns the value of the @make attribute
def make
@make
end
end
==================
#create object
my_car=Car.new

#read object
my_car.make #=> nil

#write
my_car.make='Toyota'

#read
my_car.make #=> 'Toyota'
--------------
#write
my_car.make='Mazda'

#read
my_car.make #=> 'Mazda

Sunday, February 20, 2011

rails error: no such file to load -- libssl

Yesterday I install rails 3 on my pc (Ubuntu 10.10) & create new application
when i run this application i got this error "no such file to load -- libssl"

I spend whole day to fix this error search, read lot of stuff on net & finally
i got simple solution i run only following 4 commands & error gone.

1) $ sudo apt-get install libopenssl-ruby

2) $ cd /usr/local/src/ruby-1.9.2-p0/ext/openssl/

3) $ sudo ruby extconf.rb

4) $ sudo make install

that set

Tuesday, December 14, 2010

How to connect Mysql via ruby

require 'mysql'

#my = Mysql.new(hostname, username, password, databasename)
con = Mysql.new('localhost', '', '', 'student')
rs = con.query('select * from student')
rs.each_hash { |h| puts h['name']}
con.close

Wednesday, December 1, 2010

Delete elements in Hash

i=0
j=0

x=%w[a b c d e f g h]
y=%w[o p q r s t u v]

b=%w[a c e]

a={}

until j==x.length
a.store(x[j],y[j])
j+=1
end

until i==b.length

a.delete_if {|key,value| key==b[i]}

i+=1
end


a.each {|key,value| puts "#{key} #{value}"}

Monday, November 29, 2010

How to handel JavaScript Popups with Ruby/Watir

require 'watir'
require 'watir\contrib\enabled_popup'
include Watir
require 'test/unit'
require 'win32ole'

your codes here till pop-up coming

ie.button(:value, ).click_no_wait

hwnd = ie.enabled_popup(5)
if (hwnd)
w = WinClicker.new
w.makeWindowActive(hwnd)
w.clickWindowsButton_hwnd( hwnd, "OK" )
sleep 5
end

your reaming codes