Class: BrandLogo::Fetcher

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/brand_logo/fetcher.rb

Overview

Entry point for brand_logo retrieval.

Composes a chain of strategies tried in order until one finds a valid icon. Dependencies (HTTP client, HTML parser, image analyzer) are instantiated once and shared across all strategies.

Usage:

# Default configuration
icon = BrandLogo::Fetcher.new.fetch('github.com')

# Custom config
config = BrandLogo::Config.new(min_dimensions: { width: 32, height: 32 }, timeout: 5)
icon = BrandLogo::Fetcher.new(config: config).fetch('github.com')

# Custom strategy chain (OCP)
fetcher = BrandLogo::Fetcher.new(strategies: [MyCustomStrategy.new(config: config)])

# All icons from all strategies
icons = BrandLogo::Fetcher.new.fetch_all('github.com')

Constant Summary collapse

DOMAIN_PATTERN =
T.let(/\A[a-z0-9\-.]+\.[a-z]{2,}\z/i, Regexp)

Instance Method Summary collapse

Constructor Details

#initialize(config: nil, strategies: nil) ⇒ Fetcher

Returns a new instance of Fetcher.



37
38
39
40
41
42
43
# File 'lib/brand_logo/fetcher.rb', line 37

def initialize(config: nil, strategies: nil)
  @config         = T.let(config || Config.new, Config)
  @http_client    = T.let(RealHttpClient.new(@config), HttpClient)
  @image_analyzer = T.let(FastimageAnalyzer.new, ImageAnalyzer)
  @html_parser    = T.let(NokogiriParser.new, HtmlParser)
  @strategies     = T.let(strategies || build_default_strategies, T::Array[Strategies::BaseStrategy])
end

Instance Method Details

#fetch(domain) ⇒ Object

Raises:



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/brand_logo/fetcher.rb', line 48

def fetch(domain)
  validate_domain!(domain)
  BrandLogo::Logging.logger.debug("Fetching brand_logo for: #{domain}")

  @strategies.each do |strategy|
    BrandLogo::Logging.logger.debug("Trying #{strategy.class.name}")
    icon = strategy.fetch(domain)
    return icon if icon
  end

  raise NoIconFoundError, "No brand_logo found for #{domain}"
end

#fetch_all(domain) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/brand_logo/fetcher.rb', line 63

def fetch_all(domain)
  validate_domain!(domain)

  @strategies
    .flat_map { |strategy| strategy.fetch_all(domain) }
    .uniq(&:url)
end