www-favicon 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/www/favicon.rb +26 -32
  2. metadata +2 -2
data/lib/www/favicon.rb CHANGED
@@ -5,39 +5,40 @@ require 'hpricot'
5
5
 
6
6
  module WWW
7
7
  class Favicon
8
- VERSION = '0.0.4'
9
-
10
- def initialize(options = {})
11
- @options = options
12
- end
8
+ VERSION = '0.0.5'
13
9
 
14
10
  def find(url)
15
- html = request(URI(url)).body
11
+ html = request(url).body
16
12
  find_from_html(html, url)
17
13
  end
18
14
 
19
15
  def find_from_html(html, url)
20
- uri = URI(url)
21
- favicon_url = find_from_link(html, uri) || try_default_path(uri)
22
- if @options[:verify]
23
- favicon_url = nil unless valid_favicon_url?(favicon_url)
24
- end
16
+ favicon_url = find_from_link(html, url) || default_path(url)
17
+
18
+ return nil unless valid_favicon_url?(favicon_url)
19
+
25
20
  favicon_url
26
21
  end
27
22
 
28
- def valid_favicon_url?(url)
29
- response = request(URI.parse(url))
23
+ def valid_favicon_url?(url, limit = 10)
24
+ return false if limit == 0
25
+
26
+ response = request(url)
30
27
 
31
- (
32
- response.code =~ /\A2/ &&
33
- response.body.to_s != '' &&
34
- response.content_type =~ /image/i
35
- ) ? true : false
28
+ if response.kind_of?(Net::HTTPRedirection)
29
+ valid_favicon_url?(response['Location'], limit - 1)
30
+ else
31
+ (
32
+ response.code =~ /\A2/ &&
33
+ response.body.to_s != '' &&
34
+ response.content_type =~ /image/i
35
+ ) ? true : false
36
+ end
36
37
  end
37
38
 
38
39
  private
39
40
 
40
- def find_from_link(html, uri)
41
+ def find_from_link(html, url)
41
42
  doc = Hpricot(html)
42
43
 
43
44
  doc.search('//link').each do |link|
@@ -47,7 +48,7 @@ module WWW
47
48
  if favicon_url_or_path =~ /^http/
48
49
  return favicon_url_or_path
49
50
  else
50
- return URI.join(uri.to_s, favicon_url_or_path).to_s
51
+ return URI.join(url, favicon_url_or_path).to_s
51
52
  end
52
53
  end
53
54
  end
@@ -55,25 +56,18 @@ module WWW
55
56
  nil
56
57
  end
57
58
 
58
- def try_default_path(uri)
59
+ def default_path(url)
60
+ uri = URI(url)
59
61
  uri.path = '/favicon.ico'
60
62
  %w[query fragment].each do |element|
61
63
  uri.send element + '=', nil
62
64
  end
63
65
 
64
- response = request(uri, 'head')
65
-
66
- case response.code.split('').first
67
- when '2'
68
- return uri.to_s
69
- when '3'
70
- return response['Location']
71
- end
72
-
73
- nil
66
+ uri.to_s
74
67
  end
75
68
 
76
- def request(uri, method = 'get')
69
+ def request(url, method = 'get')
70
+ uri = URI(url)
77
71
  http = Net::HTTP.new(uri.host, uri.port)
78
72
 
79
73
  if uri.scheme == 'https'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: www-favicon
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - youpy
@@ -9,7 +9,7 @@ autorequire: ""
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-11-06 00:00:00 +09:00
12
+ date: 2009-11-07 00:00:00 +09:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency