Class: I18n::Backend::Http::EtagHttpClient

Inherits:
Object
  • Object
show all
Defined in:
lib/i18n/backend/http/etag_http_client.rb

Constant Summary collapse

STATS_NAMESPACE =
'i18n-backend-http.etag_client'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ EtagHttpClient

Returns a new instance of EtagHttpClient.



9
10
11
# File 'lib/i18n/backend/http/etag_http_client.rb', line 9

def initialize(options)
  @options = options
end

Instance Method Details

#download(path, etag:) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/i18n/backend/http/etag_http_client.rb', line 13

def download(path, etag:)
  @client ||= Faraday.new(@options[:host])
  start     = Time.now

  response = @client.get(path) do |request|
    request.headers.merge!(@options[:headers]) if @options[:headers]
    request.headers["If-None-Match"] = etag if etag
    request.options[:timeout]        = @options[:http_read_timeout]
    request.options[:open_timeout]   = @options[:http_open_timeout]
  end

  tags = ["status_code:#{response.status}", "path:#{path}"]

  record :timing, time: (Time.now - start).to_f * 1000, tags: tags

  case response.status
  when 200
    record :success, tags: tags
    [response.body, response['ETag']]
  when 304
    record :success, tags: tags
    nil
  else
    record :failure, tags: tags
    raise "Failed request: #{response.inspect}"
  end
end