Class: RuboCop::RemoteConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/rubocop/remote_config.rb

Overview

Common methods and behaviors for dealing with remote config files.

Constant Summary collapse

CACHE_LIFETIME =
24 * 60 * 60

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ RemoteConfig

Returns a new instance of RemoteConfig.



11
12
13
# File 'lib/rubocop/remote_config.rb', line 11

def initialize(url)
  @uri = URI.parse(url)
end

Instance Method Details

#fileObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rubocop/remote_config.rb', line 15

def file
  return cache_path unless cache_path_expired?

  http = Net::HTTP.new(@uri.hostname, @uri.port)
  http.use_ssl = true if @uri.instance_of? URI::HTTPS

  request = Net::HTTP::Get.new(@uri.request_uri)
  if cache_path_exists?
    request['If-Modified-Since'] = File.stat(cache_path).mtime.rfc2822
  end
  response = http.request(request)

  cache_path.tap do |f|
    if response.is_a?(Net::HTTPSuccess)
      open f, 'w' do |io|
        io.write response.body
      end
    end
  end
end