Permalink
Browse files

* added WWW::Favicon#find_from_html

 * version 0.0.2
  • Loading branch information...
1 parent 28f46a6 commit 9adecd1939aa509c08fc909e47d30f17eff71ec0 @youpy committed Jul 3, 2008
Showing with 34 additions and 27 deletions.
  1. +5 −1 Rakefile
  2. +11 −3 lib/www/favicon.rb
  3. +16 −21 spec/www-favicon_spec.rb
  4. +2 −2 www-favicon.gemspec
View
@@ -7,14 +7,18 @@ require 'rake/gempackagetask'
require 'rake/rdoctask'
require 'rake/contrib/sshpublisher'
require 'fileutils'
+
+$LOAD_PATH << File.dirname(__FILE__) + '/lib/'
+require 'www/favicon'
+
include FileUtils
NAME = "www-favicon"
AUTHOR = "youpy"
EMAIL = "youpy@buycheapviagraonlinenow.com"
DESCRIPTION = "find favicon url"
BIN_FILES = %w( )
-VERS = "0.0.1"
+VERS = WWW::Favicon::VERSION
REV = File.read(".svn/entries")[/committed-rev="(d+)"/, 1] rescue nil
CLEAN.include ['**/.*.sw?', '*.gem', '.config']
View
@@ -5,15 +5,23 @@
module WWW
class Favicon
+ VERSION = '0.0.2'
+
def find(url)
uri = URI(url)
- find_from_link(uri) || try_default_path(uri)
+ html = request(uri).body
+
+ find_from_html(html, uri)
+ end
+
+ def find_from_html(html, uri)
+ find_from_link(html, uri) || try_default_path(uri)
end
private
- def find_from_link(uri)
- doc = Hpricot(request(uri).body)
+ def find_from_link(html, uri)
+ doc = Hpricot(html)
doc.search('//link').each do |link|
if link[:rel] =~ /^(shortcut )?icon$/i
View
@@ -7,31 +7,26 @@
describe WWW::Favicon do
before do
@favicon = WWW::Favicon.new
+ @htmls = [
+ '<html><link rel="icon" href="/foo/favicon.ico" /></html>',
+ '<html><link rel="Shortcut Icon" href="/foo/favicon.ico" /></html>',
+ '<html><link rel="shortcut icon" href="/foo/favicon.ico" /></html>',
+ '<html><link rel="shortcut icon" href="./foo/favicon.ico" /></html>',
+ '<html><link rel="shortcut icon" href="http://example.com/foo/favicon.ico" /></html>',
+ ]
end
- it "should find from link element when rel is short" do
- @favicon.stub!(:request).and_return expect(:body => '<html><link rel="icon" href="/favicon_shortrel.ico" /></html>')
- @favicon.find('http://example.com/').should == 'http://example.com/favicon_shortrel.ico'
+ it "should find from url" do
+ @htmls.each do |html|
+ @favicon.stub!(:request).and_return expect(:body => html)
+ @favicon.find('http://example.com/').should == 'http://example.com/foo/favicon.ico'
+ end
end
- it "should find from link element when rel is upcase" do
- @favicon.stub!(:request).and_return expect(:body => '<html><link rel="Shortcut Icon" href="/favicon_shortrel.ico" /></html>')
- @favicon.find('http://example.com/').should == 'http://example.com/favicon_shortrel.ico'
- end
-
- it "should find absolute url from link element" do
- @favicon.stub!(:request).and_return expect(:body => '<html><link rel="shortcut icon" href="/favicon.ico" /></html>')
- @favicon.find('http://example.com/repos/').should == 'http://example.com/favicon.ico'
- end
-
- it "should find relative url from link element" do
- @favicon.stub!(:request).and_return expect(:body => '<html><link rel="shortcut icon" href="./chrome/common/trac.ico" /></html>')
- @favicon.find('http://example.com/repos/').should == 'http://example.com/repos/chrome/common/trac.ico'
- end
-
- it "should find from link element when href starts with http" do
- @favicon.stub!(:request).and_return expect(:body => '<html><link rel="shortcut icon" href="http://example.com/foo/favicon.ico" /></html>')
- @favicon.find('http://example.com/').should == 'http://example.com/foo/favicon.ico'
+ it "should find from html and url" do
+ @htmls.each do |html|
+ @favicon.find_from_html(html, 'http://example.com/').should == 'http://example.com/foo/favicon.ico'
+ end
end
it "should find from default path" do
View
@@ -1,11 +1,11 @@
Gem::Specification.new do |s|
s.name = %q{www-favicon}
- s.version = "0.0.1"
+ s.version = "0.0.2"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["youpy"]
s.autorequire = %q{}
- s.date = %q{2008-07-02}
+ s.date = %q{2008-07-03}
s.description = %q{find favicon url}
s.email = %q{youpy@buycheapviagraonlinenow.com}
s.extra_rdoc_files = ["README", "ChangeLog"]

0 comments on commit 9adecd1

Please sign in to comment.