ficon 0.1 → 0.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f38f205039b510b47be5f9a226ebc8680dbd2c97a38cae867c597e02ce6805b4
4
- data.tar.gz: edefb06cd24a6091df0e92fe7ac68974cc649e0b4e3b22e73243bbcfe352a838
3
+ metadata.gz: 97c762adf3cdb126d047b66c426c3f388a3a8c61e2c6c009cff2f0dca3c7f219
4
+ data.tar.gz: 53a177388f199f7133991f1067ad1a71d747c5d84c99b88d379d7878a3196eb5
5
5
  SHA512:
6
- metadata.gz: 7d04d8330715b5194af238accadb7ab5513f3a27423ab9feb5634c03b6e5d847841eb29d4ceb85cc71faf6526b16aeaf80c561293f97b76e483789329fe5236f
7
- data.tar.gz: 818b27e4318f1c587ed29a9925b66f846644d2cd82378cf1400b1b2e9a9298fc7f091b1981abdbbe51dd598f7b0475b3cef1e1e941ce80b7c14da65a9738bc83
6
+ metadata.gz: 2af1238cf43c03e84081ddfeeb69a545613e0348b33dab4aa3670d8e152e9ce4089f9b8c892764ecb143cf2aaa90b6afa4fd7b844bb611e98a8b18b27ce161b6
7
+ data.tar.gz: 6eac859ed1529c3f22fcdaf5634a8e66ae1c625da7fbc0a1bce80afe53fc2a2a15145ac827cf65de414e68b7ea86be6cf25b808102606ab44f77def6a20124c4
data/lib/ficon/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  class Ficon
2
- VERSION = "0.1"
2
+ VERSION = "0.2"
3
3
  end
data/lib/ficon.rb CHANGED
@@ -10,9 +10,12 @@ require_relative "ficon/cache"
10
10
 
11
11
  class Ficon
12
12
  attr_reader :site
13
- def initialize(uri)
13
+ attr_accessor :user_agent
14
+
15
+ def initialize(uri, user_agent: nil)
14
16
  @uri = Addressable::URI.heuristic_parse(uri)
15
17
  @site = {}
18
+ @user_agent = user_agent || "Ficon/#{VERSION} (Ruby icon finder; https://github.com/dkam/ficon)"
16
19
  process
17
20
  end
18
21
 
@@ -24,7 +27,7 @@ class Ficon
24
27
  if @data.nil?
25
28
  response = fetch_url(@uri)
26
29
  return nil unless response
27
-
30
+
28
31
  @data = response.body.force_encoding("UTF-8")
29
32
  cache.data = @data
30
33
  cache.etag = response["etag"] if response["etag"]
@@ -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
@@ -125,23 +128,16 @@ class Ficon
125
128
 
126
129
  def fetch_url(uri)
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
+ Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https") do |http|
130
133
  http.read_timeout = 10
131
134
  http.open_timeout = 5
132
135
  request = Net::HTTP::Get.new(uri)
133
- request['User-Agent'] = "Ficon/#{VERSION} (Ruby icon finder; https://github.com/dkam/ficon)"
136
+ request["User-Agent"] = @user_agent
134
137
  http.request(request)
135
138
  end
136
139
  rescue Net::HTTPError, SocketError, Timeout::Error => e
137
140
  puts "Failed to fetch #{uri}: #{e.inspect}"
138
141
  nil
139
142
  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
143
  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.2'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dan Milne