Class: I18n::Backend::Jargon::EtagHttpClient

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

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ EtagHttpClient

Returns a new instance of EtagHttpClient.



7
8
9
10
# File 'lib/i18n/backend/jargon/etag_http_client.rb', line 7

def initialize(options)
  @options = options
  @etags = {}
end

Instance Method Details

#download(path) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/i18n/backend/jargon/etag_http_client.rb', line 12

def download(path)
  @client ||= Faraday.new(@options[:host])
  response = @client.get(path) do |request|
    request.headers["If-None-Match"] = @etags[path] if @etags[path]
    request.headers["Accept"] = 'application/json'
    request.options[:timeout] = @options[:http_read_timeout]
    request.options[:open_timeout] = @options[:http_open_timeout]
  end

  @etags[path] = response['ETag']

  case response.status
    when 200 then yield response.body
    when 304
    else
      raise "Failed request: #{response.inspect}"
  end
end