Sha256: 72343255f2c165e322e66c44bb3c41a642d2315e6073256618305f16d837ad9d
Contents?: true
Size: 1.21 KB
Versions: 133
Compression:
Stored size: 1.21 KB
Contents
require "http/client" class RemoteDataFile BASE_URL = "https://raw.githubusercontent.com/exercism/problem-specifications/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 problem-specifications. " + "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
133 entries across 133 versions & 1 rubygems