Class: BrandLogo::RealHttpClient

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

Overview

Concrete HTTP client using the ‘http` gem. Centralizes timeout and redirect configuration previously hardcoded across ScrapingStrategy, DuckduckgoStrategy, and UrlNormalizer.

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ RealHttpClient

Returns a new instance of RealHttpClient.



33
34
35
36
# File 'lib/brand_logo/http_client.rb', line 33

def initialize(config)
  @timeout  = T.let(config.timeout, Integer)
  @max_hops = T.let(config.max_hops, Integer)
end

Instance Method Details

#get_body(url) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/brand_logo/http_client.rb', line 39

def get_body(url)
  response = HTTP
             .timeout(connect: @timeout, read: @timeout, write: @timeout)
             .follow(max_hops: @max_hops)
             .get(url)
  response.status.success? ? response.body.to_s : nil
rescue StandardError
  nil
end

#head_success?(url) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
53
54
55
56
57
# File 'lib/brand_logo/http_client.rb', line 50

def head_success?(url)
  response = HTTP
             .timeout(connect: @timeout, read: @timeout, write: @timeout)
             .head(url)
  response.status.success?
rescue StandardError
  false
end