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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/ficon/version.rb +1 -1
  3. data/lib/ficon.rb +43 -27
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 97c762adf3cdb126d047b66c426c3f388a3a8c61e2c6c009cff2f0dca3c7f219
4
- data.tar.gz: 53a177388f199f7133991f1067ad1a71d747c5d84c99b88d379d7878a3196eb5
3
+ metadata.gz: 6b300144548c846bb2d309ca04ca78198f9e37682e50f08ac2f60e95f54e84f5
4
+ data.tar.gz: 90e5b7804b8e6c44e8c361c585e03a2553724322cebf84846c66c73c1652a234
5
5
  SHA512:
6
- metadata.gz: 2af1238cf43c03e84081ddfeeb69a545613e0348b33dab4aa3670d8e152e9ce4089f9b8c892764ecb143cf2aaa90b6afa4fd7b844bb611e98a8b18b27ce161b6
7
- data.tar.gz: 6eac859ed1529c3f22fcdaf5634a8e66ae1c625da7fbc0a1bce80afe53fc2a2a15145ac827cf65de414e68b7ea86be6cf25b808102606ab44f77def6a20124c4
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.2"
2
+ VERSION = "0.3"
3
3
  end
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
- 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?
24
27
 
25
- @data ||= cache.data
28
+ # Use final URL for caching
29
+ cache = Cache.new(@final_uri)
26
30
 
27
- if @data.nil?
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
- <<~REPORT
64
- Site icon: #{@site[:images].first}
65
- Page icon: #{@site[:page_images].first}
66
- Page title: #{@site[:title]}
67
- Page description: #{@site[:description]}
68
- Canonical URL: #{@site[:canonical]}
69
- 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"
70
74
  end
71
75
 
72
- def site_icons
73
- @site[:images]
74
- end
76
+ def site_icons = @site[:images]
75
77
 
76
- def page_images
77
- @site[:page_images]
78
- end
78
+ def site_icon = site_icons&.first
79
79
 
80
- def title
81
- @site[:title]
82
- end
80
+ def page_images = @site[:page_images]
83
81
 
84
- def description
85
- @site[:description]
86
- end
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}"
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.2'
4
+ version: '0.3'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dan Milne