Archive for September, 2007

To get the favicon from the url using ruby

The following ruby code helps us to get the favicon from the given url:

For this I have written a icon.rhtml file which contains the input field to get the url from the user

icon.rhtml

<form>
URL: <input type=”text” name=”main_url” />
<br/>
Favicon URI: <%=@url%>
<br/>
Favicon: <img src= <%=@url%> style=”width: 16px; height: 16px; border: 0; vertical-align: -4px;” alt=””>
</form>

in the corresponding controller file icon_controller.rb

def icon
require ‘open-uri’
begin
main_url = params[:main_url];
f = open(main_url)
rescue
err = $!
err_s = err.to_s
end
if err_s == nil
a = f.read
s= a.slice(/link.*rel\=.*shortcut.*\”/)
if s != nil
exp = s.split(“href=”)
exp1 = exp[1].split(‘”‘)
exp2 = exp1[1].split(“http:”)
if exp2[0] != “”
@url = main_url+exp1[1]
else
@url = exp1[1]
end
else
@url = main_url+”/favicon.ico”
end

else

end

end

September 21, 2007 at 1:37 pm Leave a comment

Get the title of the given url using ruby

We can use the following ruby code snippets to get the title of given url:

require ‘open-uri’

f = open(“http://www.google.com&#8221;)

a = f.read

s = a.split(“title>”)

url = s[1]

results in:

Google

September 4, 2007 at 12:59 pm 1 comment


Pages

Blog Stats

  • 33,750 hits
September 2007
M T W T F S S
    Oct »
 12
3456789
10111213141516
17181920212223
24252627282930
religion and spirituality