Class: SourceMonitor::Favicons::Fetcher

Inherits:
Object
  • Object
show all
Defined in:
lib/source_monitor/favicons/fetcher.rb

Overview

Coordinates favicon fetching for a source: checks prerequisites (ActiveStorage, config, cooldown), delegates to Discoverer, and handles attachment or failure recording. Extracted from FaviconFetchJob.

Constant Summary collapse

TRANSIENT_ERRORS =
[
  Timeout::Error, Errno::ETIMEDOUT,
  Faraday::TimeoutError, Faraday::ConnectionFailed,
  Net::OpenTimeout, Net::ReadTimeout
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ Fetcher

Returns a new instance of Fetcher.



15
16
17
# File 'lib/source_monitor/favicons/fetcher.rb', line 15

def initialize(source)
  @source = source
end

Instance Method Details

#callObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/source_monitor/favicons/fetcher.rb', line 19

def call
  return unless defined?(ActiveStorage)
  return unless SourceMonitor.config.favicons.enabled?
  return if source.website_url.blank?
  return if source.favicon.attached?
  return if within_cooldown?

  result = SourceMonitor::Favicons::Discoverer.new(source.website_url).call

  if result
    attach_favicon(result)
  else
    record_failed_attempt
  end
rescue ActiveRecord::Deadlocked
  raise
rescue *TRANSIENT_ERRORS => error
  log_error("Transient error", error)
  raise
rescue StandardError => error
  record_failed_attempt
  log_error("Failed", error)
end