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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f38f205039b510b47be5f9a226ebc8680dbd2c97a38cae867c597e02ce6805b4
4
- data.tar.gz: edefb06cd24a6091df0e92fe7ac68974cc649e0b4e3b22e73243bbcfe352a838
3
+ metadata.gz: 6b300144548c846bb2d309ca04ca78198f9e37682e50f08ac2f60e95f54e84f5
4
+ data.tar.gz: 90e5b7804b8e6c44e8c361c585e03a2553724322cebf84846c66c73c1652a234
5
5
  SHA512:
6
- metadata.gz: 7d04d8330715b5194af238accadb7ab5513f3a27423ab9feb5634c03b6e5d847841eb29d4ceb85cc71faf6526b16aeaf80c561293f97b76e483789329fe5236f
7
- data.tar.gz: 818b27e4318f1c587ed29a9925b66f846644d2cd82378cf1400b1b2e9a9298fc7f091b1981abdbbe51dd598f7b0475b3cef1e1e941ce80b7c14da65a9738bc83
6
+ metadata.gz: 6781246417b9d5cc5e1791acae5232dcd13ea9a934f0f977929fd57a5fa348dff96143b2acf327bd3ea7626e0eea65c92b585f4bd544c3fcd7764de76eee121b
7
+ data.tar.gz: 0a2b83a50bdbc340e8877028f77f99de6ca23e9bdfa15b237f0d47390394ba3f9165ebfdf2a625bffdbebca40789cac8ce2cc3b6d9c6cf11c79ae2f23f8aca19
data/lib/ficon/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  class Ficon
2
- VERSION = "0.1"
2
+ VERSION = "0.3"
3
3
  end
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
- def initialize(uri)
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
- cache = Cache.new(@uri)
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
- <<~REPORT
61
- Site icon: #{@site[:images].first}
62
- Page icon: #{@site[:page_images].first}
63
- Page title: #{@site[:title]}
64
- Page description: #{@site[:description]}
65
- Canonical URL: #{@site[:canonical]}
66
- REPORT
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 page_images
74
- @site[:page_images]
75
- end
78
+ def site_icon = site_icons&.first
76
79
 
77
- def title
78
- @site[:title]
79
- end
80
+ def page_images = @site[:page_images]
80
81
 
81
- def description
82
- @site[:description]
83
- end
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
- Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
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['User-Agent'] = "Ficon/#{VERSION} (Ruby icon finder; https://github.com/dkam/ficon)"
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ficon
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.1'
4
+ version: '0.3'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dan Milne