favicon_extractor 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -0
- data/README.md +3 -3
- data/lib/favicon_extractor/extracts_favicons_from_html.rb +5 -4
- data/lib/favicon_extractor/makes_http_requests.rb +17 -8
- data/lib/favicon_extractor/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6b210353c57dfa9e3b19e59b39ee1a6b88d77f13865549fc9d5b7c2c49ebde07
|
4
|
+
data.tar.gz: 48256d53afb5288b90e53107e889adca043606ef097f00673d446e439d713446
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8a8246dbfcdbe3e545b2fb68127c3463c43bbbd1f00b71284cc6a79f43a5129f813509741f1b63bf98fabf1a83531009b5f8d2c27be5008aef02d703a9ad0e55
|
7
|
+
data.tar.gz: 50c7ae274fcdf35a7e10e207c9d627e3d4256d93216e99c8dde467bd0ced9db5a346aa2006e42934d5a93dcbd14215b94c489a7eafc24b5376f00f2dc35060bf
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -14,7 +14,7 @@ If bundler is not being used to manage dependencies, install the gem by executin
|
|
14
14
|
|
15
15
|
## Usage
|
16
16
|
|
17
|
-
```
|
17
|
+
```ruby
|
18
18
|
require 'favicon_extractor'
|
19
19
|
|
20
20
|
FaviconExtractor.extract("http://google.com")
|
@@ -29,7 +29,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
29
29
|
|
30
30
|
## Contributing
|
31
31
|
|
32
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
32
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/leeourand/favicon_extractor. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/leeourand/favicon_extractor/blob/master/CODE_OF_CONDUCT.md).
|
33
33
|
|
34
34
|
## License
|
35
35
|
|
@@ -37,4 +37,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
|
|
37
37
|
|
38
38
|
## Code of Conduct
|
39
39
|
|
40
|
-
Everyone interacting in the FaviconExtractor project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/
|
40
|
+
Everyone interacting in the FaviconExtractor project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/leeourand/favicon_extractor/blob/master/CODE_OF_CONDUCT.md).
|
@@ -2,11 +2,12 @@ module FaviconExtractor
|
|
2
2
|
class ExtractsFaviconsFromHtml
|
3
3
|
class << self
|
4
4
|
def extract(html, url)
|
5
|
+
@url = url
|
5
6
|
html.scan(/<link.*icon.*>/).flatten.map do |str|
|
6
7
|
str.match(/href="(.*?)"/).captures.first
|
7
8
|
end
|
8
9
|
.map { |str| handle_relative_paths(str) }
|
9
|
-
.append(
|
10
|
+
.append(default_favicon)
|
10
11
|
end
|
11
12
|
|
12
13
|
private
|
@@ -15,7 +16,7 @@ module FaviconExtractor
|
|
15
16
|
|
16
17
|
def handle_relative_paths(str)
|
17
18
|
if str.start_with?("/")
|
18
|
-
uri = URI(url)
|
19
|
+
uri = URI(@url)
|
19
20
|
uri.path = str
|
20
21
|
uri.to_s
|
21
22
|
else
|
@@ -23,8 +24,8 @@ module FaviconExtractor
|
|
23
24
|
end
|
24
25
|
end
|
25
26
|
|
26
|
-
def
|
27
|
-
uri = URI(url)
|
27
|
+
def default_favicon
|
28
|
+
uri = URI(@url)
|
28
29
|
uri.path = DEFAULT_FAVICON_PATH
|
29
30
|
uri.to_s
|
30
31
|
end
|
@@ -1,27 +1,36 @@
|
|
1
1
|
module FaviconExtractor
|
2
2
|
class MakesHttpRequests
|
3
3
|
def self.request(location)
|
4
|
-
new
|
4
|
+
new(location).request
|
5
5
|
end
|
6
6
|
|
7
|
-
def initialize
|
7
|
+
def initialize(url)
|
8
8
|
@redirect_limit = 10
|
9
9
|
@redirects = 0
|
10
|
+
@url = url
|
10
11
|
end
|
11
12
|
|
12
|
-
def request
|
13
|
-
raise ArgumentError,
|
13
|
+
def request
|
14
|
+
raise ArgumentError, 'too many redirects' if @redirects > redirect_limit
|
14
15
|
|
15
16
|
begin
|
16
|
-
|
17
|
+
uri = URI(url)
|
18
|
+
response = Net::HTTP.start(uri.host,
|
19
|
+
uri.port,
|
20
|
+
read_timeout: 10,
|
21
|
+
open_timeout: 10,
|
22
|
+
ssl_timeout: 10,
|
23
|
+
use_ssl: uri.scheme == 'https') do |http|
|
24
|
+
http.request(Net::HTTP::Get.new(uri))
|
25
|
+
end
|
17
26
|
|
18
27
|
case response
|
19
28
|
when Net::HTTPSuccess
|
20
29
|
response.body
|
21
30
|
when Net::HTTPRedirection
|
22
31
|
@redirects += 1
|
23
|
-
|
24
|
-
request
|
32
|
+
@url = response['location']
|
33
|
+
request
|
25
34
|
else
|
26
35
|
response.value
|
27
36
|
end
|
@@ -30,6 +39,6 @@ module FaviconExtractor
|
|
30
39
|
|
31
40
|
private
|
32
41
|
|
33
|
-
attr_reader :redirect_limit
|
42
|
+
attr_reader :redirect_limit, :url
|
34
43
|
end
|
35
44
|
end
|