class RuboCop::Schema::CachedHTTPClient

Public Class Methods

new(cache_dir, &event_handler) click to toggle source
# File lib/rubocop/schema/cached_http_client.rb, line 11
def initialize(cache_dir, &event_handler)
  @cache_dir     = Pathname(cache_dir)
  @event_handler = event_handler
end

Public Instance Methods

get(url) click to toggle source
# File lib/rubocop/schema/cached_http_client.rb, line 16
def get(url)
  url = URI(url)
  validate_url url

  path = path_for_url(url)
  return path.read if path.readable?

  path.parent.mkpath
  Event.dispatch type: :request, &@event_handler

  http_get(url).tap(&path.method(:write))
end

Private Instance Methods

path_for_url(url) click to toggle source

@param [URI::HTTP] url

# File lib/rubocop/schema/cached_http_client.rb, line 39
def path_for_url(url)
  @cache_dir + url.scheme + url.hostname + url.path[1..-1]
end
validate_url(url) click to toggle source
# File lib/rubocop/schema/cached_http_client.rb, line 31
def validate_url(url)
  return if url.nil?

  raise ArgumentError, 'Expected an absolute URL' unless url.absolute?
  raise ArgumentError, 'Expected an HTTP URL' unless url.is_a? URI::HTTP
end