ficon 0.2 → 0.3
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/lib/ficon/version.rb +1 -1
- data/lib/ficon.rb +43 -27
- 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: 6b300144548c846bb2d309ca04ca78198f9e37682e50f08ac2f60e95f54e84f5
|
4
|
+
data.tar.gz: 90e5b7804b8e6c44e8c361c585e03a2553724322cebf84846c66c73c1652a234
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6781246417b9d5cc5e1791acae5232dcd13ea9a934f0f977929fd57a5fa348dff96143b2acf327bd3ea7626e0eea65c92b585f4bd544c3fcd7764de76eee121b
|
7
|
+
data.tar.gz: 0a2b83a50bdbc340e8877028f77f99de6ca23e9bdfa15b237f0d47390394ba3f9165ebfdf2a625bffdbebca40789cac8ce2cc3b6d9c6cf11c79ae2f23f8aca19
|
data/lib/ficon/version.rb
CHANGED
data/lib/ficon.rb
CHANGED
@@ -9,25 +9,28 @@ require_relative "ficon/image"
|
|
9
9
|
require_relative "ficon/cache"
|
10
10
|
|
11
11
|
class Ficon
|
12
|
-
attr_reader :site
|
12
|
+
attr_reader :site, :final_uri
|
13
13
|
attr_accessor :user_agent
|
14
14
|
|
15
15
|
def initialize(uri, user_agent: nil)
|
16
16
|
@uri = Addressable::URI.heuristic_parse(uri)
|
17
|
+
@final_uri = @uri
|
17
18
|
@site = {}
|
18
19
|
@user_agent = user_agent || "Ficon/#{VERSION} (Ruby icon finder; https://github.com/dkam/ficon)"
|
19
20
|
process
|
20
21
|
end
|
21
22
|
|
22
23
|
def doc
|
23
|
-
|
24
|
+
# First try to fetch to determine final URL
|
25
|
+
response = fetch_url(@uri) unless @data
|
26
|
+
return nil if response.nil? && @data.nil?
|
24
27
|
|
25
|
-
|
28
|
+
# Use final URL for caching
|
29
|
+
cache = Cache.new(@final_uri)
|
26
30
|
|
27
|
-
|
28
|
-
response = fetch_url(@uri)
|
29
|
-
return nil unless response
|
31
|
+
@data ||= cache.data
|
30
32
|
|
33
|
+
if @data.nil? && response
|
31
34
|
@data = response.body.force_encoding("UTF-8")
|
32
35
|
cache.data = @data
|
33
36
|
cache.etag = response["etag"] if response["etag"]
|
@@ -60,30 +63,27 @@ class Ficon
|
|
60
63
|
end
|
61
64
|
|
62
65
|
def report
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
66
|
+
report_lines = []
|
67
|
+
report_lines << "Site icon: #{@site[:images].first}"
|
68
|
+
report_lines << "Page icon: #{@site[:page_images].first}"
|
69
|
+
report_lines << "Page title: #{@site[:title]}"
|
70
|
+
report_lines << "Page description: #{@site[:description]}"
|
71
|
+
report_lines << "Final URL: #{@final_uri}" if @final_uri.to_s != @uri.to_s
|
72
|
+
report_lines << "Canonical URL: #{@site[:canonical]}" if @site[:canonical]
|
73
|
+
report_lines.join("\n") + "\n"
|
70
74
|
end
|
71
75
|
|
72
|
-
def site_icons
|
73
|
-
@site[:images]
|
74
|
-
end
|
76
|
+
def site_icons = @site[:images]
|
75
77
|
|
76
|
-
def
|
77
|
-
@site[:page_images]
|
78
|
-
end
|
78
|
+
def site_icon = site_icons&.first
|
79
79
|
|
80
|
-
def
|
81
|
-
@site[:title]
|
82
|
-
end
|
80
|
+
def page_images = @site[:page_images]
|
83
81
|
|
84
|
-
def
|
85
|
-
|
86
|
-
|
82
|
+
def page_image = page_images&.first
|
83
|
+
|
84
|
+
def title = @site[:title]
|
85
|
+
|
86
|
+
def description = @site[:description]
|
87
87
|
|
88
88
|
def other_page_data
|
89
89
|
@site[:title] = doc.at_xpath("//meta[@property='og:title']/@content")&.value || @doc.at_xpath("//title")&.text&.strip
|
@@ -126,15 +126,31 @@ class Ficon
|
|
126
126
|
|
127
127
|
private
|
128
128
|
|
129
|
-
def fetch_url(uri)
|
129
|
+
def fetch_url(uri, redirect_limit = 5)
|
130
130
|
uri = URI(uri) unless uri.is_a?(URI)
|
131
131
|
|
132
|
+
raise "Too many redirects" if redirect_limit <= 0
|
133
|
+
|
132
134
|
Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https") do |http|
|
133
135
|
http.read_timeout = 10
|
134
136
|
http.open_timeout = 5
|
135
137
|
request = Net::HTTP::Get.new(uri)
|
136
138
|
request["User-Agent"] = @user_agent
|
137
|
-
http.request(request)
|
139
|
+
response = http.request(request)
|
140
|
+
|
141
|
+
case response
|
142
|
+
when Net::HTTPRedirection
|
143
|
+
location = response["location"]
|
144
|
+
if location
|
145
|
+
new_uri = URI.join(uri.to_s, location)
|
146
|
+
@final_uri = Addressable::URI.parse(new_uri.to_s)
|
147
|
+
return fetch_url(new_uri, redirect_limit - 1)
|
148
|
+
end
|
149
|
+
else
|
150
|
+
@final_uri = Addressable::URI.parse(uri.to_s)
|
151
|
+
end
|
152
|
+
|
153
|
+
response
|
138
154
|
end
|
139
155
|
rescue Net::HTTPError, SocketError, Timeout::Error => e
|
140
156
|
puts "Failed to fetch #{uri}: #{e.inspect}"
|