Retrieve the page title for a given url using redirect_follower and hpricot
Ruby
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib
test
.document
.gitignore
LICENSE
README.rdoc
Rakefile
VERSION
html_page_title.gemspec

README.rdoc

HtmlPageTitle

gem install html_page_title

A simple class for finding the title of a given http url by fetching the url, following all eventual redirects (with redirect_follower Rubygem) and finally parsing it through hpricot.

You can either use the shorthand form or initialize the instance properly:

* HtmlPageTitle('http://github.com')
* HtmlPageTitle.new('http://github.com')

Those calls are equivalent, except for one subtle difference: The shorthand form will swallow SocketErrors and return nil (i.e. this will happen for invalid urls), while the regular instantiation via new will throw that error.

After that you can either get the title, the heading (which will be the content of the first h1 tag in the body) or the label, which will be (in the following order by availability) either the heading, or the title, or the target url after redirecting. Note that if the title or the heading can not be found (e.g. a non-HTML document), both methods will return nil, so the label method is the only one that will always return some kind of string.

HTML entities inside the title and heading are being automatically decoded using the great htmlentities ruby gem.

Basic usage

>> require 'html_page_title'
>> t=HtmlPageTitle('http://tinyurl.com/2lekkm')
>> t.url
=> "http://github.com"
>> t.title
=> "Secure source code hosting and collaborative development - GitHub"
>> t.heading
=> "Unleash Your Code"
>> t.label
=> "Unleash Your Code"

You can also have a look at the unit test to find out about the behaviour.

A more complex example

This little (ugly) script will retrieve the page title and destination url for tweets pulled in from the search api using the twitter gem and insert the page title alongside the url.

require 'rubygems'
require 'twitter'
require 'uri'
require 'html_page_title'

s = Twitter::Search.new('github')

s.each do |tweet|
  puts tweet['text']
  urls = URI.extract(tweet["text"]).select {|uri| uri =~ /^http/ }
  text = tweet["text"]
  urls.each do |url|
    hpt = HtmlPageTitle(url)
    text.gsub!(url, "'#{hpt.title}' (#{hpt.url})")
  end
  puts text, ""
end

When run, the output will look somewhat like this:

$ ruby example.rb
@igrigorik avait déjà proposé un serveur WebSocket basé sur EventMachine github: http://bit.ly/crHaJ9  ; le post http://bit.ly/dtD8Km
@igrigorik avait déjà proposé un serveur WebSocket basé sur EventMachine github: 'igrigorik's em-websocket at master - GitHub' (http://github.com/igrigorik/em-websocket)  ; le post 'Ruby & WebSockets: TCP for the Browser - igvita.com' (http://www.igvita.com/2009/12/22/ruby-websockets-tcp-for-the-browser/)

RT @rubyfrance: Rev-WebSocket 0.1.2 est un serveur WebSocket en Ruby basé sur Rev (qui se base sur libevent ie epoll/kqueue) http://bit.ly/9c1yzi
RT @rubyfrance: Rev-WebSocket 0.1.2 est un serveur WebSocket en Ruby basé sur Rev (qui se base sur libevent ie epoll/kqueue) 'frsyuki's rev-websocket at master - GitHub' (http://github.com/frsyuki/rev-websocket)

RT @zevarito: I just released a simple gem for track events with Mixpanel, take a look http://github.com/zevarito/mixpanel #ruby #rack #mixpanel
RT @zevarito: I just released a simple gem for track events with Mixpanel, take a look 'zevarito's mixpanel at master - GitHub' (http://github.com/zevarito/mixpanel) #ruby #rack #mixpanel

TODO:

  • Remove arbitrary html from the h1 content

Note on Patches/Pull Requests

  • Fork the project.

  • Make your feature addition or bug fix.

  • Add tests for it. This is important so I don't break it in a future version unintentionally.

  • Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)

  • Send me a pull request. Bonus points for topic branches.

Copyright

Copyright © 2010 Christoph Olszowka. See LICENSE for details.