class RubbyCop::RemoteConfig

Common methods and behaviors for dealing with remote config files.

Constants

CACHE_LIFETIME

Public Class Methods

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

Public Instance Methods

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

  request do |response|
    open cache_path, 'w' do |io|
      io.write response.body
    end
  end

  cache_path
end

Private Instance Methods

cache_name_from_uri() click to toggle source
# File lib/rubbycop/remote_config.rb, line 72
def cache_name_from_uri
  uri = @uri.clone
  uri.query = nil
  uri.to_s.gsub!(/[^0-9A-Za-z]/, '-')
end
cache_path() click to toggle source
# File lib/rubbycop/remote_config.rb, line 55
def cache_path
  File.expand_path(".rubbycop-#{cache_name_from_uri}", @base_dir)
end
cache_path_exists?() click to toggle source
# File lib/rubbycop/remote_config.rb, line 59
def cache_path_exists?
  @cache_path_exists ||= File.exist?(cache_path)
end
cache_path_expired?() click to toggle source
# File lib/rubbycop/remote_config.rb, line 63
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
handle_response(response, limit) { |response| ... } click to toggle source
# File lib/rubbycop/remote_config.rb, line 44
def handle_response(response, limit, &block)
  case response
  when Net::HTTPSuccess
    yield response
  when Net::HTTPRedirection
    request(URI.parse(response['location']), limit - 1, &block)
  else
    response.error!
  end
end
request(uri = @uri, limit = 10, &block) click to toggle source
# File lib/rubbycop/remote_config.rb, line 30
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 = 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

  handle_response(http.request(request), limit, &block)
end