www-favicon 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/Rakefile +14 -0
  2. data/lib/www/favicon.rb +23 -18
  3. metadata +22 -2
data/Rakefile CHANGED
@@ -58,6 +58,8 @@ spec = Gem::Specification.new do |s|
58
58
  s.test_files = Dir["test/test_*.rb"]
59
59
 
60
60
  s.add_dependency('hpricot', '>= 0')
61
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
62
+ s.add_development_dependency(%q<fakeweb>, [">= 1.2.7"])
61
63
  #s.required_ruby_version = '>= 1.8.2'
62
64
 
63
65
  s.files = %w(README.rdoc ChangeLog Rakefile) +
@@ -103,3 +105,15 @@ desc 'Show information about the gem.'
103
105
  task :debug_gem do
104
106
  puts spec.to_ruby
105
107
  end
108
+
109
+ desc 'Test against the provided URL'
110
+ task :try do
111
+ url = ENV['URL']
112
+ raise "URL is required." unless url
113
+
114
+ require 'rubygems'
115
+ require 'www/favicon'
116
+
117
+ favicon_url = WWW::Favicon.new.find(url)
118
+ puts "Favicon URL for #{url} is #{favicon_url.inspect}"
119
+ end
data/lib/www/favicon.rb CHANGED
@@ -5,11 +5,11 @@ require 'hpricot'
5
5
 
6
6
  module WWW
7
7
  class Favicon
8
- VERSION = '0.0.5'
8
+ VERSION = '0.0.6'
9
9
 
10
10
  def find(url)
11
- html = request(url).body
12
- find_from_html(html, url)
11
+ response = request(url)
12
+ find_from_html(response.body, response.request_url)
13
13
  end
14
14
 
15
15
  def find_from_html(html, url)
@@ -20,20 +20,14 @@ module WWW
20
20
  favicon_url
21
21
  end
22
22
 
23
- def valid_favicon_url?(url, limit = 10)
24
- return false if limit == 0
25
-
23
+ def valid_favicon_url?(url)
26
24
  response = request(url)
27
25
 
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
26
+ (
27
+ response.code =~ /\A2/ &&
28
+ response.body.to_s != '' &&
29
+ response.content_type =~ /image/i
30
+ ) ? true : false
37
31
  end
38
32
 
39
33
  private
@@ -66,7 +60,10 @@ module WWW
66
60
  uri.to_s
67
61
  end
68
62
 
69
- def request(url, method = 'get')
63
+ def request(url, options = {})
64
+ method = options[:method] || :get
65
+ redirection_limit = options[:redirection_limit] || 10
66
+
70
67
  uri = URI(url)
71
68
  http = Net::HTTP.new(uri.host, uri.port)
72
69
 
@@ -75,12 +72,20 @@ module WWW
75
72
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
76
73
  end
77
74
 
78
- http.start do |http|
75
+ response = http.start do |http|
79
76
  path =
80
77
  (uri.path.empty? ? '/' : uri.path) +
81
78
  (uri.query ? '?' + uri.query : '') +
82
79
  (uri.fragment ? '#' + uri.fragment : '')
83
- response = http.send(method, path)
80
+ http.send(method, path)
81
+ end
82
+
83
+ if response.kind_of?(Net::HTTPRedirection) && redirection_limit > 0
84
+ request(response['Location'], :redirection_limit => redirection_limit - 1)
85
+ else
86
+ response.instance_variable_set('@request_url', url)
87
+ def response.request_url; @request_url; end
88
+ response
84
89
  end
85
90
  end
86
91
  end
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.5
4
+ version: 0.0.6
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-07 00:00:00 +09:00
12
+ date: 2009-11-16 00:00:00 +09:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -22,6 +22,26 @@ dependencies:
22
22
  - !ruby/object:Gem::Version
23
23
  version: "0"
24
24
  version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.2.9
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: fakeweb
37
+ type: :development
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 1.2.7
44
+ version:
25
45
  description: find favicon url
26
46
  email: youpy@buycheapviagraonlinenow.com
27
47
  executables: []