|
|
Question : Retrieve title from URL in Ruby on Rails
|
|
I'm currently developing a web application in Ruby on Rails. How can I retrieve website information such as title given a remote URL? Examples of this can be seen in many places such as del.icio.us or reddit.com.
Thank you,
WJ
|
Answer : Retrieve title from URL in Ruby on Rails
|
|
Install all what is needed for mechanize or hypricot. a gems install mechanize should do the work. On debian you also have to install libopenssl-ruby Then you just can write: irb(main):002:0> require "rubygems" true irb(main):003:0> require "mechanize" true irb(main):004:0> agent = WWW:Mechanize.new NoMethodError: undefined method `new' for :Mechanize:Symbol from (irb):4 from /usr/local/lib/site_ruby/1.8/rubygems/user_interaction.rb:252 irb(main):005:0> agent = WWW::Mechanize.new #b96b0 @keep_alive=true, @redirect_ok=true, @key=nil, @conditional_requests=true, @proxy_port=nil, @password=nil, @log=nil, @pluggable_parser=#echanize::PluggableParser:0x2ad7e34b95c0 @parsers={"text/html"=>WWW::Mechanize::Page}, @default=WWW::Mechanize::File>, @keep_alive_time=300, @pass=nil, @ca_file=nil, @proxy_addr=nil, @auth_hash={}, @user=nil, @cert=nil, @read_timeout=nil, @connection_cache={}, @proxy_pass=nil, @watch_for_set=nil, @digest=nil, @cookie_jar=#ze::CookieJar:0x2ad7e34b9660 @jar={}>, @history=[], @open_timeout=nil, @follow_meta_refresh=false, @proxy_user=nil, @user_agent="WWW-Mechanize/0.6.10 (http://rubyforge.org/projects/mechanize/)"> irb(main):006:0> page = agent.get("http://www.q-software-solutions.de/index.shtml") # {url #HTTP:0x156bf1a57950 URL:http://www.q-software-solutions.de/index.shtml>} {meta} {title "Q Software Solutions"} {iframes} {frames} {links # # # # "Store" "https://secure.q-software-solutions.de/shop/shop"> # #monials.shtml"> # # "Terms and\n Conditions" "http://www.q-software-solutions.de/terms.shtml">} {forms}>
irb(main):007:0> page.title "Q Software Solutions" irb(main):008:0>
The last one is the title. It's easy to integrate that in any software you like. Quite handy, isn't it ;-)
Regards Friedrich
|
|
|
|
|