ficon 0.1 → 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 +52 -40
- data/test/ficon_test.rb +15 -0
- 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,22 +9,28 @@ require_relative "ficon/image"
|
|
9
9
|
require_relative "ficon/cache"
|
10
10
|
|
11
11
|
class Ficon
|
12
|
-
attr_reader :site
|
13
|
-
|
12
|
+
attr_reader :site, :final_uri
|
13
|
+
attr_accessor :user_agent
|
14
|
+
|
15
|
+
def initialize(uri, user_agent: nil)
|
14
16
|
@uri = Addressable::URI.heuristic_parse(uri)
|
17
|
+
@final_uri = @uri
|
15
18
|
@site = {}
|
19
|
+
@user_agent = user_agent || "Ficon/#{VERSION} (Ruby icon finder; https://github.com/dkam/ficon)"
|
16
20
|
process
|
17
21
|
end
|
18
22
|
|
19
23
|
def doc
|
20
|
-
|
24
|
+
# First try to fetch to determine final URL
|
25
|
+
response = fetch_url(@uri) unless @data
|
26
|
+
return nil if response.nil? && @data.nil?
|
27
|
+
|
28
|
+
# Use final URL for caching
|
29
|
+
cache = Cache.new(@final_uri)
|
21
30
|
|
22
31
|
@data ||= cache.data
|
23
32
|
|
24
|
-
if @data.nil?
|
25
|
-
response = fetch_url(@uri)
|
26
|
-
return nil unless response
|
27
|
-
|
33
|
+
if @data.nil? && response
|
28
34
|
@data = response.body.force_encoding("UTF-8")
|
29
35
|
cache.data = @data
|
30
36
|
cache.etag = response["etag"] if response["etag"]
|
@@ -57,30 +63,27 @@ class Ficon
|
|
57
63
|
end
|
58
64
|
|
59
65
|
def report
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
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"
|
67
74
|
end
|
68
75
|
|
69
|
-
def site_icons
|
70
|
-
@site[:images]
|
71
|
-
end
|
76
|
+
def site_icons = @site[:images]
|
72
77
|
|
73
|
-
def
|
74
|
-
@site[:page_images]
|
75
|
-
end
|
78
|
+
def site_icon = site_icons&.first
|
76
79
|
|
77
|
-
def
|
78
|
-
@site[:title]
|
79
|
-
end
|
80
|
+
def page_images = @site[:page_images]
|
80
81
|
|
81
|
-
def
|
82
|
-
|
83
|
-
|
82
|
+
def page_image = page_images&.first
|
83
|
+
|
84
|
+
def title = @site[:title]
|
85
|
+
|
86
|
+
def description = @site[:description]
|
84
87
|
|
85
88
|
def other_page_data
|
86
89
|
@site[:title] = doc.at_xpath("//meta[@property='og:title']/@content")&.value || @doc.at_xpath("//title")&.text&.strip
|
@@ -91,7 +94,7 @@ class Ficon
|
|
91
94
|
|
92
95
|
def self.site_images(uri, doc)
|
93
96
|
results = []
|
94
|
-
|
97
|
+
|
95
98
|
# Get tile color for Windows tiles
|
96
99
|
tile_color = doc.at_xpath("//meta[@name='msapplication-TileColor']/@content")&.value
|
97
100
|
|
@@ -100,7 +103,7 @@ class Ficon
|
|
100
103
|
|
101
104
|
results.collect { |result| normalise(uri, result) }.uniq.collect do |url|
|
102
105
|
# Check if this is a tile image to pass the color
|
103
|
-
is_tile = doc.at_xpath("//meta[@name='msapplication-TileImage' and @content='#{url}' or @content='#{url.sub(uri.to_s,
|
106
|
+
is_tile = doc.at_xpath("//meta[@name='msapplication-TileImage' and @content='#{url}' or @content='#{url.sub(uri.to_s, "")}']")
|
104
107
|
Image.new(url, is_tile ? tile_color : nil)
|
105
108
|
end.sort_by(&:area).reverse
|
106
109
|
end
|
@@ -123,25 +126,34 @@ class Ficon
|
|
123
126
|
|
124
127
|
private
|
125
128
|
|
126
|
-
def fetch_url(uri)
|
129
|
+
def fetch_url(uri, redirect_limit = 5)
|
127
130
|
uri = URI(uri) unless uri.is_a?(URI)
|
128
|
-
|
129
|
-
|
131
|
+
|
132
|
+
raise "Too many redirects" if redirect_limit <= 0
|
133
|
+
|
134
|
+
Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https") do |http|
|
130
135
|
http.read_timeout = 10
|
131
136
|
http.open_timeout = 5
|
132
137
|
request = Net::HTTP::Get.new(uri)
|
133
|
-
request[
|
134
|
-
http.request(request)
|
138
|
+
request["User-Agent"] = @user_agent
|
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
|
135
154
|
end
|
136
155
|
rescue Net::HTTPError, SocketError, Timeout::Error => e
|
137
156
|
puts "Failed to fetch #{uri}: #{e.inspect}"
|
138
157
|
nil
|
139
158
|
end
|
140
|
-
|
141
|
-
def other_page_data
|
142
|
-
@site[:title] = doc.at_xpath("//meta[@property='og:title']/@content")&.value || @doc.at_xpath("//title")&.text&.strip
|
143
|
-
@site[:description] = doc.at_xpath("//meta[@property='og:description']/@content")&.value
|
144
|
-
canonical = doc.at_xpath("//link[@rel='canonical']/@href")&.value
|
145
|
-
@site[:canonical] = canonical unless canonical == @url
|
146
|
-
end
|
147
159
|
end
|
data/test/ficon_test.rb
CHANGED
@@ -54,4 +54,19 @@ class FiconTest < Minitest::Test
|
|
54
54
|
assert_equal 'https://site.com/win8-tile-144.png', result.url
|
55
55
|
assert_equal '#00aced', result.tile_color
|
56
56
|
end
|
57
|
+
|
58
|
+
def test_custom_user_agent
|
59
|
+
# Test default user agent
|
60
|
+
ficon_default = Ficon.new('https://example.com')
|
61
|
+
assert_match(/^Ficon\/0\.2/, ficon_default.user_agent)
|
62
|
+
|
63
|
+
# Test custom user agent
|
64
|
+
custom_agent = 'MyApp/1.0 (Custom Bot)'
|
65
|
+
ficon_custom = Ficon.new('https://example.com', user_agent: custom_agent)
|
66
|
+
assert_equal custom_agent, ficon_custom.user_agent
|
67
|
+
|
68
|
+
# Test user agent can be changed after initialization
|
69
|
+
ficon_custom.user_agent = 'Changed/2.0'
|
70
|
+
assert_equal 'Changed/2.0', ficon_custom.user_agent
|
71
|
+
end
|
57
72
|
end
|