class RuboCop::RemoteConfig

Common methods and behaviors for dealing with remote config files. @api private

Constants

CACHE_LIFETIME

Attributes

uri[R]

Public Class Methods

new(url, base_dir) click to toggle source
# File lib/rubocop/remote_config.rb, line 14
def initialize(url, base_dir)
  @uri = URI.parse(url)
  @base_dir = base_dir
end

Public Instance Methods

file() click to toggle source
# File lib/rubocop/remote_config.rb, line 19
def file
  return cache_path unless cache_path_expired?

  request do |response|
    next if response.is_a?(Net::HTTPNotModified)
    next if response.is_a?(SocketError)

    File.write(cache_path, response.body)
  end

  cache_path
end
inherit_from_remote(file, path) click to toggle source
# File lib/rubocop/remote_config.rb, line 32
def inherit_from_remote(file, path)
  new_uri = @uri.dup
  new_uri.path.gsub!(%r{/[^/]*$}, "/#{file.delete_prefix('./')}")
  RemoteConfig.new(new_uri.to_s, File.dirname(path))
end

Private Instance Methods

cache_name_from_uri() click to toggle source
# File lib/rubocop/remote_config.rb, line 95
def cache_name_from_uri
  uri = cloned_url
  uri.query = nil
  uri.to_s.gsub!(/[^0-9A-Za-z]/, '-')
end
cache_path() click to toggle source
# File lib/rubocop/remote_config.rb, line 78
def cache_path
  File.expand_path(".rubocop-#{cache_name_from_uri}", @base_dir)
end
cache_path_exists?() click to toggle source
# File lib/rubocop/remote_config.rb, line 82
def cache_path_exists?
  @cache_path_exists ||= File.exist?(cache_path)
end
cache_path_expired?() click to toggle source
# File lib/rubocop/remote_config.rb, line 86
def cache_path_expired?
  return true unless cache_path_exists?

  @cache_path_expired ||= begin
    file_age = (Time.now - File.stat(cache_path).mtime).to_f
    (file_age / CACHE_LIFETIME) > 1
  end
end
cloned_url() click to toggle source
# File lib/rubocop/remote_config.rb, line 101
def cloned_url
  uri = @uri.clone
  uri.user = nil if uri.user
  uri.password = nil if uri.password
  uri
end
generate_request(uri) { |request| ... } click to toggle source
# File lib/rubocop/remote_config.rb, line 53
def generate_request(uri)
  request = Net::HTTP::Get.new(uri.request_uri)

  request.basic_auth(uri.user, uri.password) if uri.user
  request['If-Modified-Since'] = File.stat(cache_path).mtime.rfc2822 if cache_path_exists?

  yield request
end
handle_response(response, limit) { |response| ... } click to toggle source
# File lib/rubocop/remote_config.rb, line 62
def handle_response(response, limit, &block)
  case response
  when Net::HTTPSuccess, Net::HTTPNotModified, SocketError
    yield response
  when Net::HTTPRedirection
    request(URI.parse(response['location']), limit - 1, &block)
  else
    begin
      response.error!
    rescue StandardError => e
      message = "#{e.message} while downloading remote config file #{cloned_url}"
      raise e, message
    end
  end
end
request(uri = @uri, limit = 10, &block) click to toggle source
# File lib/rubocop/remote_config.rb, line 40
def request(uri = @uri, limit = 10, &block)
  raise ArgumentError, 'HTTP redirect too deep' if limit.zero?

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

  generate_request(uri) do |request|
    handle_response(http.request(request), limit, &block)
  rescue SocketError => e
    handle_response(e, limit, &block)
  end
end