Sha256: 8c1d425629fe663cb445057cd32c898fa55f24c3209ad6d122499f5ff6fbff06
Contents?: true
Size: 1.18 KB
Versions: 16
Compression:
Stored size: 1.18 KB
Contents
require "http/client" class RemoteDataFile BASE_URL = "https://raw.githubusercontent.com/exercism/x-common/master/exercises" DATA_FILE = "canonical-data.json" def initialize(@test_name : String); end def path if File.exists?(cache_file) cache_file else remote_file end end private def remote_file File.write(cache_file, body) cache_file end private def cache_file File.join(cache_dir, DATA_FILE) end private def cache_dir dirpath = File.expand_path(File.join("cache", @test_name), generator_root) unless Dir.exists?(dirpath) Dir.mkdir_p(dirpath) end dirpath end private def generator_root File.expand_path(File.join("..", ".."), __FILE__) end private def body response = HTTP::Client.get(url) case response.status_code when 200 response.body when 404 raise "A #{DATA_FILE} doesn't exist for #{@test_name} in x-common. " + "Go make one!" else raise "Error while requesting the #{@test_name} data file from GitHub... " + "Status was #{response.status_code}" end end private def url [BASE_URL, @test_name, DATA_FILE].join("/") end end
Version data entries
16 entries across 16 versions & 1 rubygems