Class: SourceMonitor::Scrapers::Fetchers::HttpFetcher

Inherits:
Object
  • Object
show all
Defined in:
lib/source_monitor/scrapers/fetchers/http_fetcher.rb

Defined Under Namespace

Classes: Result

Instance Method Summary collapse

Constructor Details

#initialize(http: SourceMonitor::HTTP) ⇒ HttpFetcher

Returns a new instance of HttpFetcher.



11
12
13
14
# File 'lib/source_monitor/scrapers/fetchers/http_fetcher.rb', line 11

def initialize(http: SourceMonitor::HTTP)
  @http = http
  @aia_attempted = false
end

Instance Method Details

#fetch(url:, settings: nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/source_monitor/scrapers/fetchers/http_fetcher.rb', line 16

def fetch(url:, settings: nil)
  response = connection(settings).get(url)

  if success_status?(response.status)
    Result.new(status: :success, body: response.body, headers: response.headers, http_status: response.status)
  else
    Result.new(
      status: :failed,
      http_status: response.status,
      error: "http_error",
      message: "Non-success HTTP status"
    )
  end
rescue Faraday::SSLError => error
  result = attempt_aia_recovery(url, settings)
  return result if result

  Result.new(status: :failed, error: error.class.name, message: error.message)
rescue Faraday::ClientError => error
  Result.new(
    status: :failed,
    http_status: extract_status(error),
    error: error.class.name,
    message: error.message
  )
rescue Faraday::Error => error
  Result.new(status: :failed, error: error.class.name, message: error.message)
end