Class: RemoteTranslationLoader::Fetchers::HttpFetcher

Inherits:
BaseFetcher
  • Object
show all
Defined in:
lib/remote_translation_loader/fetchers/http_fetcher.rb

Constant Summary collapse

DEFAULT_RETRIES =
3
DEFAULT_TIMEOUT =

seconds

10
RETRYABLE_ERRORS =
[HTTP::Error, HTTP::TimeoutError, HTTP::ConnectionError].freeze

Instance Method Summary collapse

Methods inherited from BaseFetcher

#format_for, #parse

Constructor Details

#initialize(retries: DEFAULT_RETRIES, timeout: DEFAULT_TIMEOUT) ⇒ HttpFetcher

Returns a new instance of HttpFetcher.



12
13
14
15
# File 'lib/remote_translation_loader/fetchers/http_fetcher.rb', line 12

def initialize(retries: DEFAULT_RETRIES, timeout: DEFAULT_TIMEOUT)
  @retries = retries
  @timeout = timeout
end

Instance Method Details

#fetch(url) ⇒ Object

Raises:



17
18
19
20
21
22
# File 'lib/remote_translation_loader/fetchers/http_fetcher.rb', line 17

def fetch(url)
  response = with_retries(url) { HTTP.timeout(@timeout).get(url) }
  raise FetchError, "Failed to fetch data from #{url} (status #{response.status})" unless response.status.success?

  parse(response.body.to_s, format: format_for(url))
end